我有3个阵列可以说,
数组1的id = 1 color = blue
数组2的id = 2 color = red
数组3的id = 3 color = red
我有另一张桌子,上面有每种颜色的价格。从另一张桌子我得到
数组1颜色=蓝色价格= 2.00
数组2颜色=红色价格= 3.00
我怎样才能获得
数组1的id = 1,color = blue,price = 2.00
数组2的id = 2,color = red,price = 3.00
数组3的id = 3,color = red,price = 3.00
答案 0 :(得分:0)
创建以下给定的数组结构
$arrFirst[0]['id'] = 1;
$arrFirst[0]['color'] = 'blue';
$arrFirst[1]['id'] = 2;
$arrFirst[1]['color'] = 'red';
$arrFirst[2]['id'] = 3;
$arrFirst[2]['color'] = 'red';
============================
$arrSecond[0]['color'] = 'blue';
$arrSecond[0]['price'] = 2.00;
$arrSecond[1]['color'] = 'red';
$arrSecond[1]['price'] = 3.00;
$arrSecond[2]['color'] = 'red';
$arrSecond[2]['price'] = 3.00;
$arrFinal = array();
for($i=0;$i<count($arrFirst); $i++){
$arrFinal = array_merge($arrFirst, $arrSecond));
}
$array1['id'] = 1;
$array1['color'] = 'blue';
$array2['id'] = 2;
$array2['color'] = 'red';
$array3['id'] = 3;
$array3['color'] = 'red';
从另一张表格中了解每种颜色的价格
$array11['color'] = 'blue';
$array11['price'] = 2.00;
$array22['color'] = 'red';
$array22['price'] = 3.00;
这样做
$array1 = array_merge($array1, $array11));
$array2 = array_merge($array2, $array22));
$array3 = array_merge($array3, $array22));
答案 1 :(得分:0)
我同意@Brian Roach上面的评论。这似乎是一个难以解决的例子,因为它似乎应该采用不同的方式。例如:
I have 3 arrays lets say,
array 1 has id = 1 color = blue
array 2 has id = 2 color = red
array 3 has id = 3 color = red
拥有3个数组,只有一个数值似乎非常低效,因为它有一个包含你列出的值的数组:
$arrayOne = array('blue', 'red', 'red');
或
$arrayOne = array(1 => 'blue', 2 => 'red', 3 => 'red');
上面的第一个例子,颜色将具有它们在字符串0,1,2等中出现的位置的id。