用另一个数组键替换一个数组键php
问题描述:
我有一个csv文件。我需要读取和格式化数组才能导入。我的头都这个样子,用另一个数组键替换一个数组键php
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
)
)
在这里,我需要更换相应的标头值我值的每个关键。所以我的最终阵列应该是这样的,
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
)
)
我找不到这个好的解决方案。有人帮我解决这个问题吗?
答
这应该为你工作:
<?php
array_walk($values, function(&$v, $k, $headers){
$v = array_combine($headers, $v);
}, $headers);
print_r($values);
?>
(在这里,我只是去通过每个innerArray与
array_walk()
,并返回其与
$headers
为键和innerArray作为值,这是我与
array_combine()
做联合)
+0
感谢Rizier123 ..!这是有效的..今天我学到了很多..再次感谢..! – Elavarasan 2015-03-31 05:34:27
+1
@Elavarasan不客气!祝你有美好的一天:D(顺便说一句:如果你想要你仍然可以添加你的尝试给你的问题) – Rizier123 2015-03-31 05:35:33
你有没有尝试过或做过一些研究? – Rizier123 2015-03-31 05:05:05
是的,我尝试了一些东西,但它不工作,所以没有在这里发布。 – Elavarasan 2015-03-31 05:06:03
了解'array_combine' – 2015-03-31 05:06:50