简体   繁体   中英

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

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]]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM