[英]php Update multidimensional array based on values from another multidimensional array
I have 2 different multidimensional array that are structured in the following way 我有2种不同的多维数组,它们的结构如下
ARRAY 1 阵列1
Array
(
[0] => Array
(
[0] => john
[1] => AUTHOR
[2] => PUBLISHER
)
[0] => Array
(
[0] => Smith
[1] => AUTHOR
[2] => PUBLISHER
)
)
ARRAY 2 阵列2
Array
(
[0] => Array
(
[0] => John
[1] => www.john.com
[2] => D
)
[1] => Array
(
[0] => Smith
[1] => smith.com
[2] => D
)
)
I would like two check if the value "john" from Array1 is available in array2. 我想两次检查Array1中的值“ john”是否在array2中可用。 If it's the case, i retrieve the value of "www.john.com" in array2 and insert it in array 1 如果是这样,我在array2中检索“ www.john.com”的值并将其插入数组1中
This is what i would like to achieve 这就是我想要实现的
FINAL ARRAY 最终阵列
ARRAY 1 阵列1
Array
(
[0] => Array
(
[0] => john
[1] => www.john.com
[2] => AUTHOR
[3] => PUBLISHER
)
[0] => Array
(
[0] => Paul
[1] => AUTHOR
[2] => PUBLISHER
)
)
The array is dynamic, i put static values just to show what i would like to acheive. 该数组是动态的,我将静态值只是为了显示我想要达到的目标。
Any help on this will be appreciated 任何帮助,将不胜感激
This will work for you, but see comment below: 这将为您工作,但请参阅下面的评论:
$arrayFirst = array(
arraey('John', 'AUTHOR', 'PUBLISHER',),
array('Smith', 'AUTHOR', 'PUBLISHER')) ;
$arraySecond = array(
array('John', 'www.john.com', 'D'),
array('Smith', 'smith.com', 'D'));
foreach ($arrayFirst as $kye => $subArrayFirst){
foreach ($arraySecond as $subArraySecond){
if(in_array($subArrayFirst[0], $subArraySecond)) {
$arrayFirst[$kye][] = $subArraySecond[1];
}
}
}
var_dump($arrayFirst);
But in this case it is important the position of authow name and address 但是在这种情况下,重要的是,授权名称和地址的位置
$s1 = [
['John', 'AUTHOR', 'PUBLISHER'],
['Smith', 'AUTHOR', 'PUBLISHER']
];
$s2 = [
['John', 'www.john.com', 'D'],
['Smith', 'smith.com', 'D']
];
foreach ($s1 as &$info) {
$name = $info[0];
foreach ($s2 as $info2) {
if (in_array($name, $info2)) {
$url = $info2[1];
$info[] = $url;
}
}
This is a simple and fast way. 这是一种简单快捷的方法。
for($i=0;i<=Array1.length;i++){
for($j=0;j<=Array2.length;j++){
if (Array1[0][0] == Array2[i][j]){
echo Array2[i][j];
Array1[0][1] = Array2[i][j];
break;
break;
}
}
}
I hope that helps 希望对您有所帮助
You can write a function which checks if one item from array1 is avaiable in array2: 您可以编写一个函数来检查array2中是否有来自array1的项:
function arrayTwoContains($element)
{
foreach($array2 as $elemFrom2)
{
if(strtolower($element[0])==strtolower($elemFrom2[0])) //if the names from array1 and array 2 are equal: return element two
{
return $elemFrom2;
}
}
return false;
}
Then you can build a loop where you run through array1 and if arrayTwoContains() returns not false, you put the values in a new array. 然后,您可以构建一个循环,在该循环中遍历array1,如果arrayTwoContains()返回的结果不是false,则将值放在新数组中。
$fused = array(); // your new array
$key = 0; //current index of new array
foreach($array1 as $element)
{
if($elemFromTwo = arrayTwoContains($element))
{
$elementToInsert = array();
$elementToInsert[0] = $element[0];
$elementToInsert[1] = $elemFromTwo[1];
$elementToInsert[2] = $element[1];
$elementToInsert[3] = $element[2];
$fused[$key] = $elementToInsert;
$key++;
}
}
I hope this works, I haven't done anything in PHP since months ;) 我希望这行得通,自几个月以来我一直没有用PHP做任何事情;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.