-4

我有一个返回这样一个数组的函数

Array
(
    [0] => stdClass Object
        (
            [tid] => 1
            [vid] => 2
            [name] => About Us
            [description] => 
            [format] => wysiwyg_editor
            [weight] => 0
            [depth] => 0
            [parents] => Array
                (
                    [0] => 0
                )

        )

[1] => stdClass Object
    (
        [tid] => 200
        [vid] => 2
        [name] => Stories
        [description] => 
        [format] => wysiwyg_editor
        [weight] => 0
        [depth] => 0
        [parents] => Array
            (
                [0] => 0
            )

    )

)

为了简化它以供进一步使用,我想将此数组转换为简单的数组,其中键为 [tid],值为 [name] 所以它会是这样的:

Array
(
    [1] => About Us
    [200] => Stories

)

任何有关正确代码语法的提示或帮助都会很棒。谢谢

4

2 回答 2

2

试试这个:

$result = array();
foreach ($data as $row) {
    $result[$row->tid] = $row->name;
}
var_dump($result);
于 2012-07-11T10:29:25.883 回答
2

使用这个片段:

function transform($arr){
   $result = array();

   foreach($arr as $obj){
     $result[$obj->tid] = $obj->name;
   }

   return $result;
}
于 2012-07-11T10:30:57.870 回答