比较php中的数组并将值从一个数组分配给另一个数组
我已经在我的一个项目上工作了几天了,我在这个项目中要做的基本上是比较用户通常在excel中使用的csv文件,每天晚上用php自动执行。 我从CSV的简介数组中获得了这些信息,但我很难将它们组合起来,以便为每本书获得正确的信息,因此下面的示例和问题如下 我有csv文件中的以下数组(array1):比较php中的数组并将值从一个数组分配给另一个数组,php,excel,loops,csv,multidimensional-array,Php,Excel,Loops,Csv,Multidimensional Array,我已经在我的一个项目上工作了几天了,我在这个项目中要做的基本上是比较用户通常在excel中使用的csv文件,每天晚上用php自动执行。 我从CSV的简介数组中获得了这些信息,但我很难将它们组合起来,以便为每本书获得正确的信息,因此下面的示例和问题如下 我有csv文件中的以下数组(array1): Array ( [0] => Array ( [0] => book1 [1] => description1 [2] =>
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] =>
)
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
)
)
以及第二个csv文件中的另一个阵列(array2):
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] =>
)
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中获取array1书籍的值。
例:
请相信我,对于这个问题的任何帮助都将不胜感激 @Rasvan没问题,很高兴我能帮上忙。我能再问你一个问题吗?@Rasvan当然,如果不是一个简短的问题,它可能更适合作为一个新问题发布。但是,因为我不是程序员,我也没有完成这个项目的逻辑技能,我真的需要帮助这个项目。我能让你对一个涉及你的商业提案感兴趣吗?这真的不是一个复杂的项目,但因为我缺乏必要的开发技能,这是非常累累,而且很难。
//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;
}
}