Rumah >pembangunan bahagian belakang >tutorial php >javascript - 【算法】数组中随机给定的键值对, 置首位, 如何实现?
举个栗子:
<code>$arr = array( '175' => '金针菇' '100' => '银针菇' ); $arr2 = array( '65' => '白萝卜' '67' => '黑萝卜' '84' => '黄萝卜' '100' => '银针菇' '90' => '大萝卜' '175' => '金针菇' ); </code>
最后的效果是这样子滴:
<code>$arr3 = array( '175' => '金针菇' '100' => '银针菇' '65' => '白萝卜' '67' => '黑萝卜' '84' => '黄萝卜' '90' => '大萝卜' );</code>
请教,这样的算法该怎么实现?
举个栗子:
<code>$arr = array( '175' => '金针菇' '100' => '银针菇' ); $arr2 = array( '65' => '白萝卜' '67' => '黑萝卜' '84' => '黄萝卜' '100' => '银针菇' '90' => '大萝卜' '175' => '金针菇' ); </code>
最后的效果是这样子滴:
<code>$arr3 = array( '175' => '金针菇' '100' => '银针菇' '65' => '白萝卜' '67' => '黑萝卜' '84' => '黄萝卜' '90' => '大萝卜' );</code>
请教,这样的算法该怎么实现?
这个问题已关闭,自己写出来了,提问的时候想复杂了,缓和了之后才知道原来这么简单,贴下代码:
<code>foreach ($arr2 as $key => $value) { if(!in_array($value, $arr)) { $arr[$key] = $value; } } var_dump($arr);</code>
<code>return($arr + $arr2);</code>