用 php 中的另一个数组键替换一个数组键
Replace one arrays keys with another arrays keys in php
我有一个 csv 文件。我需要读取数组格式并进行格式化才能导入。我的 header 是这样的,
Array
(
[0] => id
[1] => name
[2] => shortDescription
[3] => description
[4] => productType
[5] => sku
[6] => styleCode
)
我的价值观是这样的,
Array
(
[0] => Array
(
[0] => 185
[1] => T-shirts
[2] => this is tshirt short desc
[3] => This is tshirt desc
[4] => simple
[5] => 4585
[6] => 5292++
)
[1] => Array
(
[0] => 186
[1] => test name
[2] => test short desc
[3] => test desc
[4] => configurable
[5] => 525
[6] => 555
)
)
这里我需要替换我的值对应的每个键 header 值。所以我的最终数组应该是这样的,
Array
(
[0] => Array
(
[id] => 185
[name => T-shirts
[shortdescription] => this is tshirt short desc
[description] => This is tshirt desc
[producttype] => simple
[sku] => 4585
[stylecode] => 5292++
)
[1] => Array
(
[id] => 186
[name] => test name
[shortdescription] => test short desc
[description] => test desc
[producttype] => configurable
[sku] => 525
[stylecode] => 555
)
)
我找不到好的解决方案。有人帮我解决这个问题吗?
这应该适合你:
(这里我只是用 array_walk()
and return it combined with the $headers
as keys and the innerArray as values, which I do with array_combine()
遍历每个 innerArray)
<?php
array_walk($values, function(&$v, $k, $headers){
$v = array_combine($headers, $v);
}, $headers);
print_r($values);
?>
我有一个 csv 文件。我需要读取数组格式并进行格式化才能导入。我的 header 是这样的,
Array
(
[0] => id
[1] => name
[2] => shortDescription
[3] => description
[4] => productType
[5] => sku
[6] => styleCode
)
我的价值观是这样的,
Array
(
[0] => Array
(
[0] => 185
[1] => T-shirts
[2] => this is tshirt short desc
[3] => This is tshirt desc
[4] => simple
[5] => 4585
[6] => 5292++
)
[1] => Array
(
[0] => 186
[1] => test name
[2] => test short desc
[3] => test desc
[4] => configurable
[5] => 525
[6] => 555
)
)
这里我需要替换我的值对应的每个键 header 值。所以我的最终数组应该是这样的,
Array
(
[0] => Array
(
[id] => 185
[name => T-shirts
[shortdescription] => this is tshirt short desc
[description] => This is tshirt desc
[producttype] => simple
[sku] => 4585
[stylecode] => 5292++
)
[1] => Array
(
[id] => 186
[name] => test name
[shortdescription] => test short desc
[description] => test desc
[producttype] => configurable
[sku] => 525
[stylecode] => 555
)
)
我找不到好的解决方案。有人帮我解决这个问题吗?
这应该适合你:
(这里我只是用 array_walk()
and return it combined with the $headers
as keys and the innerArray as values, which I do with array_combine()
遍历每个 innerArray)
<?php
array_walk($values, function(&$v, $k, $headers){
$v = array_combine($headers, $v);
}, $headers);
print_r($values);
?>