简体   繁体   English

PHP格式化数组结果

[英]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. 我已经试过了一个foreach循环来回显键和值,但是我不确定从那里去。 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]]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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