Posted By

iloveitaly on 12/17/09


Tagged

php array index using flatten


Versions (?)

Flatten Array Using Index


 / Published in: PHP
 

Before: ` Array ( [0] => Array ( [totalsessions] => 24 [studytype_id] => 1 )

[1] => Array
    (
        [total_sessions] => 24
        [study_type_id] => 2
    )

[2] => Array
    (
        [total_sessions] => 20
        [study_type_id] => 3
    )

[3] => Array
    (
        [total_sessions] => 10
        [study_type_id] => 4
    )

[4] => Array
    (
        [total_sessions] => 10
        [study_type_id] => 5
    )

[5] => Array
    (
        [total_sessions] => 10
        [study_type_id] => 6
    )

[6] => Array
    (
        [total_sessions] => 10
        [study_type_id] => 7
    )

[7] => Array
    (
        [total_sessions] => 8
        [study_type_id] => 10
    )

[8] => Array
    (
        [total_sessions] => 8
        [study_type_id] => 12
    )

) After: Array ( [1] => Array ( [total_sessions] => 24 )

[2] => Array
    (
        [total_sessions] => 24
    )

[3] => Array
    (
        [total_sessions] => 20
    )

[4] => Array
    (
        [total_sessions] => 10
    )

[5] => Array
    (
        [total_sessions] => 10
    )

[6] => Array
    (
        [total_sessions] => 10
    )

[7] => Array
    (
        [total_sessions] => 10
    )

[10] => Array
    (
        [total_sessions] => 8
    )

[12] => Array
    (
        [total_sessions] => 8
    )

) `

  1. function flatten_array($arrayList, $key) {
  2. $newArray = array();
  3.  
  4. foreach($arrayList as $array) {
  5. $index = $array[$key];
  6. unset($array[$key]);
  7. $newArray[$index] = $array;
  8. }
  9.  
  10. return $newArray;
  11. }

Report this snippet  

You need to login to post a comment.