drctyr2869 2018-03-16 12:18
浏览 32
已采纳

在现有数组中添加新数组时,数组形成不正确

I am trying to add an array to exiting array. Its getting added but array inside array is what the out.

Current scenario

         Array
        (
        [t373980] => stdClass Object
            (
                [tid] => 373980
                [name] => Ability
                [depth] => 0
                [startMonday] => 0
                [hidden_name] => selected_agency[373980]
                [parent_tid] => 0
                [full_label] => Ability
                [full_tid] => 373980
                [expanded] => 0
            )

        [t414605] => stdClass Object
            (
                [tid] => 414605
                [name] => Ad Council
                [depth] => 0
                [startMonday] => 0
                [hidden_name] => selected_agency[414605]
                [parent_tid] => 0
                [full_label] => Ad Council
                [full_tid] => 414605
                [expanded] => 0
            )

        [t0] => stdClass Object
            (
                [t] => Array
                    (
                        [tid] => 0
                        [name] => (Blank)
                        [depth] => 0
                        [startMonday] => 0
                        [hidden_name] => selected_agency[0]
                        [parent_tid] => 0
                        [full_label] => (Blank)
                        [full_tid] => 0
                        [expanded] => 0
                    )

            )
         )

** what I want is **

      Array
        (
        [t373980] => stdClass Object
            (
                [tid] => 373980
                [name] => Ability
                [depth] => 0
                [startMonday] => 0
                [hidden_name] => selected_agency[373980]
                [parent_tid] => 0
                [full_label] => Ability
                [full_tid] => 373980
                [expanded] => 0
            )

        [t414605] => stdClass Object
            (
                [tid] => 414605
                [name] => Ad Council
                [depth] => 0
                [startMonday] => 0
                [hidden_name] => selected_agency[414605]
                [parent_tid] => 0
                [full_label] => Ad Council
                [full_tid] => 414605
                [expanded] => 0
            )

       [t] =>  stdClass Object
                    (
                        [tid] => 0
                        [name] => (Blank)
                        [depth] => 0
                        [startMonday] => 0
                        [hidden_name] => selected_agency[0]
                        [parent_tid] => 0
                        [full_label] => (Blank)
                        [full_tid] => 0
                        [expanded] => 0
                    )
         )

         $no_agency_arr =array("tid"=>"0", "name"=>"(Blank)", "depth"=>0, "startMonday"=>0, "hidden_name"=>"selected_agency[0]", 
                  "parent_tid"=>"0", "full_label"=>"(Blank)", "full_tid"=>"0", "expanded"=>0); 
        $no_agency_obj = (object)$no_agency_arr; 
        $final_no_agency_arr["t"] = $no_agency_obj; 
        array_push($out,$final_no_agency_arr);
  • 写回答

2条回答 默认 最新

  • dtsnx44260 2018-03-16 12:33
    关注
    $no_agency_arr =array("tid"=>"0", "name"=>"(Blank)", "depth"=>0, "startMonday"=>0, "hidden_name"=>"selected_agency[0]", 
                      "parent_tid"=>"0", "full_label"=>"(Blank)", "full_tid"=>"0", "expanded"=>0); 
    //you need array so no need to convert it to object. so comment/remove it out.
            //$no_agency_obj = (object)$no_agency_arr; 
            $final_no_agency_arr["t"] = $no_agency_arr; 
    //why are you using it when you already assigned value in that array. so comment it, 
    //if `$out` if the final array which you are printing, so you need to assign value in it instead of `$final_no_agency_arr` like:
     $out["t"] = $no_agency_arr; //don't forget to comment out the above line
            //array_push($out,$final_no_agency_arr);
    

    Check here for reference

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算