0

I have one array. I want to find unique value of fk_section_id and other fields(section_name & fk_section_id) associated with it. I also want to sort this array by fk_section_id. How to get this array from the original one?

Array

 Array
(
[0] => Array
    (
        [fk_field_id] => 1
        [fk_section_id] => 1
        [section_order] => 1
        [field_order] => 1            
        [field_name] => Title
        [section_name] => Your Detail
        [fk_field_type_id] => 4
    )

[1] => Array
    (
        [fk_field_id] => 2
        [fk_section_id] => 1
        [section_order] => 1
        [field_order] => 2       
        [field_name] => Name
        [section_name] => Your Detail
        [fk_field_type_id] => 1
    )  

[2] => Array
    (
        [fk_field_id] => 3
        [fk_section_id] => 2
        [section_order] => 2
        [field_order] => 1       
        [field_name] => Road
        [section_name] => Address For Correspondence
        [fk_field_type_id] => 1
    )
)

Expected Output

  Array
(
[0] => Array
    (           
        [fk_section_id] => 1
        [section_order] => 1         
        [section_name] => Your Detail            
    )

[1] => Array
    (           
        [fk_section_id] => 2
        [section_order] => 2           
        [section_name] => Address For Correspondence          
    )
)

I tried below

 $i=0;
    foreach($formFields as $val){
        $i++;
        $allSections[$i]['section_name'] = $val['section_name'];
        $allSections[$i]['fk_section_id'] = $val['fk_section_id'];
        $allSections[$i]['section_order'] = $val['section_order'];
    }

And it gives

Array
(
[0] => Array
    (
        [section_name] => Your Detail
        [fk_section_id] => 1
        [section_order] => 1
    )

[1] => Array
    (
        [section_name] => Your Detail
        [fk_section_id] => 1
        [section_order] => 1
    ) 


[2] => Array
    (
        [section_name] => Address For Correspondence
        [fk_section_id] => 2
        [section_order] => 2
    )

)
2
  • Just make a loop and find duplicates ? Have you tried anything ? Commented Oct 29, 2015 at 9:37
  • You include the expected result, but can you please also include the current result? Or do you admit you haven't tried anything yourself yet? That's bad form, I'm afraid. The solution to me seems trivial, but I think it'd be better handled in the database using 'intelligent' SQL rather than fiddling with the resulting arrays in PHP after doing a dumb query. Commented Oct 29, 2015 at 9:38

1 Answer 1

1

You can try this:

$data = array(
    array(
        'fk_field_id' => 1,
        'fk_section_id' => 1,
        'section_order' => 1,
        'field_order' => 1,        
        'field_name' => 'Title',
        'section_name' => 'Your Detail',
        'fk_field_type_id' => 4,
    ),
    array(
        'fk_field_id' => 2,
        'fk_section_id' => 1,
        'section_order' => 1,
        'field_order' => 2,        
        'field_name' => 'Name',
        'section_name' => 'Your Detail',
        'fk_field_type_id' => 1,
    ),
);

$temp = array();

foreach($data as $key => $value) { //Make an new array using fk_field_id as key
    if (!in_array($value['fk_field_id'], $temp)) {
        $temp[$value['fk_field_id']] = $value;
    }
}
ksort($temp); //Sort the array by key

var_dump($temp);

Result:

array (size=2)
  1 => 
    array (size=7)
      'fk_field_id' => int 1
      'fk_section_id' => int 1
      'section_order' => int 1
      'field_order' => int 1
      'field_name' => string 'Title' (length=5)
      'section_name' => string 'Your Detail' (length=11)
      'fk_field_type_id' => int 4
  2 => 
    array (size=7)
      'fk_field_id' => int 2
      'fk_section_id' => int 1
      'section_order' => int 1
      'field_order' => int 2
      'field_name' => string 'Name' (length=4)
      'section_name' => string 'Your Detail' (length=11)
      'fk_field_type_id' => int 1

Also now you can use:

echo $temp[1]['field_name'];

Result:

Title
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.