我有2个数组: 映射的数据:
Array
(
[0] => Array
(
[id] =>
)
[1] => Array
(
[country_code] =>
)
[2] => Array
(
[user_id] =>
)
[3] => Array
(
[category_id] =>
)
[4] => Array
(
[post_type_id] =>
)
[5] => Array
(
[make] => make
)
[6] => Array
(
[description] => description
)
}
现在我还有另一个数组,它是我的CSV数据:
Array
(
[0] => Array
(
[status] => used
[type] => Trailer
[category] => Storage
[location] => Calgary AB
[make] => Strick
[model] => Storage Van
[year] => 1997
[last_update] => 8/30/2019
[unit_number] => VE420900U
[vin] => 1S11E8283VE420900
[price] => 2900
[currency] => C
[body_style] => Storage
[colour] => Grey
[suspension] => Spring Ride
[suspension_type] =>
[axle_config] => Single
[trailer_length] => 28
[width] => 0
[height] => 0
[description] => Storage, suspension, Single axle, Steel rims, Wood floor, Roll up rear door rear door, Length: 28ft
[main_photo] => https://www.maximinc.com/images/trailer/1997-strick-storage-van-40192.jpg
[thumbnail] => https://www.maximinc.com/images/trailer/t-1997-strick-storage-van-40192.jpg
[main_photo_date] => 10/18/2017 21:57
[url] => View Online
)
[1] => Array
(
[status] => used
[type] => Trailer
[category] => Storage
[location] => Calgary AB
[make] => Roussy
[model] => Storage Van
[year] => 1987
[last_update] => 8/30/2019
[unit_number] => H1004175U
[vin] => 2R183M3C1H1004175
[price] => 4900
[currency] => C
[body_style] => Storage
[colour] => White
[suspension] => Spring Ride
[suspension_type] =>
[axle_config] => Tandem
[trailer_length] => 48
[width] => 102
[height] => 0
[description] => Storage, suspension, Tandem axle, Steel rims, Wood floor, Swing rear door, Width: 102in, Length: 48ft
[main_photo] => https://www.maximinc.com/images/trailer/1987-roussy-storage-van-40238.jpg
[thumbnail] => https://www.maximinc.com/images/trailer/t-1987-roussy-storage-van-40238.jpg
[main_photo_date] => 10/18/2017 22:07
[url] => View Online
)
)
现在,如果CSV数组键与“映射数据值”匹配,我想获取CSV数组值。
我创建了逻辑并尝试使用array_key_exists,但是失败了,我尝试了我的逻辑代码,但是它的作用是在dbs中插入错误的条目
如果匹配,我想要一个像final这样的数组
Array
(
[0]=> Array
(
[description] =>Storage, suspension, Single axle, Steel rims, Wood floor, Roll up rear door rear door, Length: 28ft
[make] => Strick
)
[1]=> Array
(
[description] =>Storage, suspension, Tandem axle, Steel rims, Wood floor, Swing rear door, Width: 102in, Length: 48ft
[make] => Roussy
)
)
这是我的逻辑,
foreach ($json_decode as $key => $value)
{
/*if(!empty($value))
{
$jsonvalue = (array)$value;
foreach ($jsonvalue as $jkey=> $jvalue)
{
foreach ($mapped_data as $mkey => $mvalue)
{
$mvalue = (array)$mvalue;
foreach ($mvalue as $mappedkey=> $mappedvalue)
{
if($jkey == $mappedvalue)
{
if(!empty($jvalue))
{
// echo "Matched one====".'<br>';
// print_r($jkey).'<br>';
// print_r($mappedvalue).'<br>';
// print_r($jvalue).'<br>';
$post->$mappedvalue = $jvalue;
$post->title = 'test';
$post->category_id = '0';
$post->city_id = '0';
//$post->save();
}
}
}
}
}
}
我的$json_decode
是我的CSV数据数组。
有人可以帮我吗?
答案 0 :(得分:1)
您的任务的完整简化脚本:
// an example of mapped array
$mapped = [
[
'key1' => false,
],
[
'key2' => 'value',
],
];
// now you need to flat your array so as to use advantages of flat arrays
// `array_filter` will also remove keys with empty values.
$flatMapped = array_filter(array_merge(...$mapped));
// here you see that you have a flat array, and not array of arrays
print_r($flatMapped);
// sample csv data
$csvData = [
[
'key1' => 42,
'key2' => 142,
'key3' => 242,
],
[
'key1' => 342,
'key2' => 442,
'key3' => 542,
],
];
// here I modify $csvData array in place, but you
// can create new array and put values there
foreach ($csvData as &$item) {
// `array_intersect_key` returns keys with values from
// first array which also exist in second array
$item = array_intersect_key($item, $flatMapped);
}
print_r($csvData);
演示here。