duanrang3357 2012-07-11 10:26 采纳率: 0%
浏览 17
已采纳

如何将php数组与对象转换为简单数组?

I have a function that returns an array like this

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
            )

    )

)

To simplify it for further use I would like to convert this array into simple one with keys as [tid] and values as [name] So it would be smth like this:

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

)

Any tips or help with proper code syntax would be great. Thanks

  • 写回答

2条回答 默认 最新

  • douci2516 2012-07-11 10:29
    关注

    Try this:

    $result = array();
    foreach ($data as $row) {
        $result[$row->tid] = $row->name;
    }
    var_dump($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作