0

我有以下数组。

"rent" => array:3 [
        0 => array:1 [
          0 => "5000"
        ]
        1 => array:3 [
          0 => "10000"
          1 => "60000"
          2 => "80000"
        ]
        2 => []
      ]
"house_quantity" => array:3 [
        0 => array:1 [
          0 => "2"
        ]
        1 => array:3 [
          0 => "3"
          1 => "4"
          2 => "6"
        ]
        2 => []
      ]
"property_id" => array:3 [
        0 => 1
        1 => 2
        2 => 3
      ]

"type_of_house" => array:3 [
        0 => array:1 [
          0 => array:1 [
            "type" => "studio_apartment"
          ]
        ]
        1 => array:3 [
          0 => array:1 [
            "type" => "studio_apartment"
          ]
          1 => array:1 [
            "type" => "one_bedroom"
          ]
          2 => array:1 [
            "type" => "two_bedroom"
          ]
        ]
        2 => array:2 [
          0 => array:1 [
            "type" => "studio_apartment"
          ]
          1 => array:1 [
            "type" => "two_bedroom"
          ]
        ]
      ]
    ]

我想组合上面的数组,使其形成一个看起来像这样的数组。

"0" =>  [
        "property_id" => 1
        "type_of_house" => array:3 [
          "type"=> "studio_apartment"
          "rent" => "5000"
          "house_quantity" => "2"
        ]
      ]
"1" =>  [
        "property_id" => 2
        "type_of_house" => array:3 [
          "type"=> "studio_apartment"
          "rent" => "10000"
          "house_quantity" => "3"
        ]
        "type_of_house" => array:3 [
          "type"=> "one_bedroom"
          "rent" => "60000"
          "house_quantity" => "4"
        ]
       "type_of_house" => array:3 [
          "type"=> "two_bedroom"
          "rent" => "80000"
          "house_quantity" => "6"
        ]
      ]

到目前为止,我正在使用 foreach 循环遍历属性并在每个属性中附加房屋类型,如下所示:

  foreach ($request->property_id as $key=> $property_id) {
            $result[$key] = array(
                'property_id' => $property_id,
                'type_of_house' => $request->type_of_house[$key]
            );
            foreach ($result as $property_key => $property) {
                foreach ($property['type_of_house'] as $house_key => $house) {
                    $house[$key][$house_key] = array(
                            'rent' => $request->rent[$key][$house_key],
                            'house_quantity' => $request->house_quantity[$key][$house_key]
                        );
                }
            }
        $merge = array_merge_recursive($result, $house);
        dd($merge);
        }

但是我要回来的数组不太正确。这是我要回来的数组。

array:3 [
  0 => array:2 [
    "property_id" => 1
    "type_of_house" => array:1 [
      0 => array:1 [
        "type" => "studio_apartment"
      ]
    ]
  ]
  "type" => "studio_apartment"
  1 => array:1 [
    0 => array:2 [
      "rent" => "5000"
      "house_quantity" => "2"
    ]
  ]
]

如何正确合并这样的数组,谢谢。

4

1 回答 1

0

好的,假设你有 2 个数组

$array1

Array(2){
    [number] => 1,
    [address] => "Park Ave 273",
    [name] => "Peter Jones"
}

然后是一个干净$array2的,我要输入我的信息的那个要混合它们,我必须指定我希望第一个数组进入的索引,例如:

$array1 = array(
        "number" => 1,
        "address" => "Park Ave 273",
        "name" => "Peter Jones"
    );
    $array2 = array();
    $array2['client'] = $array1;

将返回:

Array
(
    [client] => Array
        (
            [number] => 1
            [address] => Park Ave 273
            [name] => Peter Jones
        )

)

如果您有多个客户端(在此示例中),则必须foreach为每个客户端执行一个循环。

于 2021-03-02T13:18:46.847 回答