在PHP中比较数组并将值从一个分配给另一个

时间:2013-09-16 11:48:20

标签: php excel loops csv multidimensional-array

现在已经在我的一个项目上工作了几天,而且我在这个项目中正在进行的操作是对用户在excel中正常做的csv文件的比较,每天晚上自动在php中进行。 我从CSV的介绍数组中获取了信息,但是我很难将它们组合起来以获得每本书的正确信息,因此下面是例子和问题。

我有一个来自csv文件的下一个数组(array1):

Array
(
[0] => Array
    (
        [0] => book1
        [1] => description1
        [2] => category1
        [3] => code1
        [4] => editor1
        [5] => 0
        [6] => eur
        [7] => out of stoc
    )

[1] => Array
    (
        [0] => book2
        [1] => description2
        [2] => category2
        [3] => code2
        [4] => editor2
        [5] => 0
        [6] => curr2
        [7] => out of stoc
    )

[2] => Array
    (
        [0] => book3
        [1] => description3
        [2] => category3
        [3] => code3
        [4] => editor3
        [5] => 0
        [6] => curr3
        [7] => out of stoc
    )

[3] => 
)

和第二个csv文件中的另一个数组(array2):

Array
(
[0] => Array
    (
        [0] => book1
        [1] => description_from_array2
        [2] => category_from_array2
        [3] => code_from_array2
        [4] => editor_from_array2
        [5] => 12
        [6] => eur
        [7] => in stoc
    )

[1] => Array
    (
        [0] => book2
        [1] => description_from_array2
        [2] => category_from_array2
        [3] => code_from_array2
        [4] => editor_from_array2
        [5] => 13
        [6] => eur
        [7] => in stoc
    )

[2] => Array
    (
        [0] => book4
        [1] => description_from_array2
        [2] => category_from_array2
        [3] => code_from_array2
        [4] => editor_from_array2
        [5] => 14
        [6] => usd
        [7] => in stoc
    )

[3] => Array
    (
        [0] => book5
        [1] => description_from_array2
        [2] => category_from_array2
        [3] => code_from_array2
        [4] => editor_from_array2
        [5] => 16
        [6] => usd
        [7] => in stoc
    )

)

我想知道如何从array2中找到array1的数组,以获取在array2中找到的array1的书籍。 例如:

Array
(
[0] => Array
(
    [0] => book1
    [1] => description2_from_array2
    [2] => category2_from_array2
    [3] => code2_from_array2
    [4] => editor2_from_array2
    [5] => 12
    [6] => eur
    [7] => in stoc
)

[1] => Array
(
    [0] => book2
    [1] => description_from_array2
    [2] => category_from_array2
    [3] => code_from_array2
    [4] => editor_from_array2
    [5] => 13
    [6] => curr_from_array2
    [7] => in stoc
)

[2] => Array
(
    [0] => book3
    [1] => description3
    [2] => category3
    [3] => code3
    [4] => editor3
    [5] => 0
    [6] => curr3
    [7] => out of stoc //because book3 is not found in array2
)

[3] => 
)

非常感谢任何对这个问题的帮助,相信我!

1 个答案:

答案 0 :(得分:1)

//Add keys to array 2 to aid lookup
$array2Tmp = array();
foreach($array2 as $item){
    $array2Tmp[$item[0]] = $item;
}

$array3 = array();
foreach($array1 as $item){
    //Use item from second array if it exists
    if(array_key_exists($item[0],$array2Tmp)){
       $array3[] = $array2Tmp[$item[0]];
     }else{
       $array3[] = $item;
     }
}