【问题标题】:comparing arrays in php and assign values from one to another比较php中的数组并将值从一个分配到另一个
【发布时间】:2013-09-20 13:22:32
【问题描述】:

我的一个项目已经工作了几天了,基本上我在这个项目中要做的是比较用户通常在 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 intro 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] => 
)

对于这个问题的任何帮助将不胜感激,相信我!

【问题讨论】:

    标签: php excel loops csv multidimensional-array


    【解决方案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;
         }
    }
    

    【讨论】:

    • @Rasvan 没问题,很高兴我能帮上忙。
    • 我能再问你一个问题吗?
    • @Rasvan 当然,如果它不是一个简短的问题,它可能更适合作为一个新问题发布。
    • 因为我不是程序员,我也没有完成这个项目的逻辑技能,我真的需要这个项目的帮助,我可以让你对我的这个项目中涉及你的业务提议感兴趣吗?
    • 这真的不是一个复杂的项目,但是因为我缺乏必要的技能来开发这个非常累,而且很难。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    相关资源
    最近更新 更多