用php中的另一个数组键替换一个数组键

时间:2015-03-31 05:04:16

标签: php arrays multidimensional-array

我有一个csv文件。我需要读取并格式化数组才能导入。我的标题看起来像这样,

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
            )
    )

我找不到好的解决方案。 Ca有人帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

这应该适合你:

(这里我只使用array_walk()浏览每个innerArray并将其与$headers组合作为键并将innerArray作为值返回,我使用array_combine()进行返回

<?php

    array_walk($values, function(&$v, $k, $headers){
        $v = array_combine($headers, $v);
    }, $headers);

    print_r($values);

?>