MacMan MacMan - 3 months ago 9
PHP Question

php formatting array results

I have an array like the following. This is the results of a query on one of our servers.

Array
(
[count] => 1
[0] => Array
(
[name] => Array
(
[count] => 1
[0] => mac
)

[0] => name
[staffid] => Array
(
[count] => 1
[0] => 1234
)

[1] => staffid
[school] => Array
(
[count] => 1
[0] => western
)

[2] => school
[count] => 3
[dn] => cn=mac,cn=staff
)

)


How do I loop through this array and create a new array as follows.

Array
(
[name] => mac
[staffid] => 1234
[school] => western
)


I've tried a
foreach
loop echoing the key & values, but I'm not sure where to go from there. There will be more results returned as the query is expanded, but original array layout will be the same and the new layout needs to be the same format.

Any ideas ?
Thanks

Answer

Try this:

$result = array();
foreach($yourArray as $element){
    for($i=0;$i<$element['count']; $i++){
        unset($element[$element[$i]]['count']);
        $result[$element[$i]] = implode(', ', $element[$element[$i]]);
    }
}
Comments