合并两个数组以创建关联数组

时间:2021-11-30 12:20:45

I have a php array something like this:

我有一个像这样的PHP数组:

array:7 [▼
  "id" => 13
  "agent_id" => 1
  "reserved_by" => 1
  "vehicle_type" => "["Bus","Car"]"
  "no_of_vehicle" => "["2","1"]"
  "created_at" => "2017-06-13 05:46:49"
  "updated_at" => "2017-06-13 05:46:49"
]

Here, vehicle_type and no_of_vehicle are in json_encode format. In the above case,

这里,vehicle_type和no_of_vehicle是json_encode格式。在上述情况下,

By json_decode I can get two arrays like this

通过json_decode,我可以得到两个这样的数组

vehicle_type

dd(json_decode($data->vehicle_type));

array:2 [▼
  0 => "Bus"
  1 => "Car"
]

no_of_vehicle

dd(json_decode($data->no_of_vehicle));

array:2 [▼
  0 => "2"
  1 => "1"
]

Now, what I want is to create an associative array of vehicle type and number equals 1.

现在,我想要的是创建一个车辆类型和数字等于1的关联数组。

array:3 [▼
  Bus => "1"
  Bus => "1"
  Car => "1"
]

It's impossible as all you say because of unique key. But, Is to possible to make similar array with nested like:

由于独特的关键,所有你说的都是不可能的。但是,是否可以用嵌套的方式制作类似的数组:

array:3 [▼
    array:1 [▼
      Bus => "1"
    ]

    array:1 [▼
      Bus => "1"
    ]

    array:1 [▼
      Bus => "1"
    ]
]

That will be fine for me Any idea, I am using laravel 5.3

这对我来说没问题任何想法,我正在使用laravel 5.3

Thanks

2 个解决方案

#1


2  

array:3 [▼
  Bus => "1"
  Bus => "1"
  Car => "1"
]

It's impossible, because the keys must unique

这是不可能的,因为钥匙必须是独一无二的

you can make it to

你可以做到

["Bus", "Bus", "Car"]

or

["Bus" => 2, "Car" => 1]

Answer to updated question

回答更新的问题

You can do like this:

你可以这样做:

$array = [
            "id" => 13,
            "agent_id" => 1,
            "reserved_by" => 1,
            "vehicle_type" => array("Bus", "Car"),
            "no_of_vehicle" => array("2", "1"),
            "created_at" => "2017-06-13 05:46:49",
            "updated_at" => "2017-06-13 05:46:49",
        ];

        $result = [];

        foreach ($array["vehicle_type"] as $key => $vehicle) {
            $num = intval($array["no_of_vehicle"][$key]);
            for ($i = 1; $i <= $num; $i++) {
                $result[] = array($vehicle => "1");
            }
        }

the $result will be:

$ result将是:

array:3 [▼
  0 => array:1 [▼
    "Bus" => "1"
  ]
  1 => array:1 [▼
    "Bus" => "1"
  ]
  2 => array:1 [▼
    "Car" => "1"
  ]
]

#2


0  

I would create an array like that :

我会创建一个这样的数组:

array[
  {
    vehicle_type => 'Bus',
    no_of_vehicle => 2,
  },
  {
    vehicle_type => 'Car',
    no_of_vehicle => 1,
  }
]

or

array['Bus_1', 'Bus_2', 'Car_1']

#1


2  

array:3 [▼
  Bus => "1"
  Bus => "1"
  Car => "1"
]

It's impossible, because the keys must unique

这是不可能的,因为钥匙必须是独一无二的

you can make it to

你可以做到

["Bus", "Bus", "Car"]

or

["Bus" => 2, "Car" => 1]

Answer to updated question

回答更新的问题

You can do like this:

你可以这样做:

$array = [
            "id" => 13,
            "agent_id" => 1,
            "reserved_by" => 1,
            "vehicle_type" => array("Bus", "Car"),
            "no_of_vehicle" => array("2", "1"),
            "created_at" => "2017-06-13 05:46:49",
            "updated_at" => "2017-06-13 05:46:49",
        ];

        $result = [];

        foreach ($array["vehicle_type"] as $key => $vehicle) {
            $num = intval($array["no_of_vehicle"][$key]);
            for ($i = 1; $i <= $num; $i++) {
                $result[] = array($vehicle => "1");
            }
        }

the $result will be:

$ result将是:

array:3 [▼
  0 => array:1 [▼
    "Bus" => "1"
  ]
  1 => array:1 [▼
    "Bus" => "1"
  ]
  2 => array:1 [▼
    "Car" => "1"
  ]
]

#2


0  

I would create an array like that :

我会创建一个这样的数组:

array[
  {
    vehicle_type => 'Bus',
    no_of_vehicle => 2,
  },
  {
    vehicle_type => 'Car',
    no_of_vehicle => 1,
  }
]

or

array['Bus_1', 'Bus_2', 'Car_1']