-1

数日間私のプロジェクトに取り組んでいますが、このプロジェクトで本質的にやろうとしていることは、ユーザーが通常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] => 
)

および 2 番目の 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 intro 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] => 
)

この質問に対する助けがあれば大歓迎です、信じてください!

4

1 に答える 1

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;
     }
}
于 2013-09-16T12:12:50.503 に答える