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

时间:2018-03-16 12:18:27

标签: php foreach

我正在尝试将数组添加到退出数组中。它被添加但数组内部的数组就是out。

当前情况

         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
                    )

            )
         )

**我想要的是**

      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 个答案:

答案 0 :(得分:1)

$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 :(得分:-1)

在您的代码中替换此

$no_agency_obj = (object)$no_agency_arr; 
$final_no_agency_arr["t"] = $no_agency_obj; 

用这个

$final_no_agency_arr["t"] = $no_agency_arr;  

如果您只需要一个数组,则无需创建对象!