First Array
[0] => Array
(
[name] => Car
[selected] => 1
)
[1] => Array
(
[name] => Boat
[selected] => 1
)
[2] => Array
(
[name] => Bike
)
[3] => Array
(
[name] => Tank
)
[4] => Array
(
[name] => Aircraft
)
第二阵列:
[0] => Array
(
[name] => Boat
[code] => PLA
)
[1] => Array
(
[name] => Car
[code] => GTO
)
所以我需要的是创建包含第一个数组的所有值的新数组,但'selected'条目需要按第二个数组的顺序排列。
所需数组:
[0] => Array
(
[name] => Boat
[selected] => 1
)
[1] => Array
(
[name] => Car
[selected] => 1
)
[2] => Array
(
[name] => Boat
)
[3] => Array
(
[name] => Tank
)
[4] => Array
(
[name] => Aircraft
)
答案 0 :(得分:1)
foreach($second as $k => $v) if(isset($v['selected')) $selected[$v['name']] = $v;
foreach($first as $k => $v){
$Desired[]['name'] = $v['name'];
if(isset($selected[$v['name']])) $Desired[]['selected'] = 1;
}
答案 1 :(得分:1)
$desiredArray = array();
$checkArray = array();
$arrayToAdd =array();
foreach ($secondArray as $item)
{
foreach($firstArray as $item2)
{
if($item2['name'] == $item['name'] && $item2['selected'])
{
$n = count($desiredArray);
$desiredArray[$n]['name'] = $item['name'];
$desiredArray[$n]['selected'] = 1;
$checkArray[] = $item['name'];
}
}
}
foreach($firstArray as $item)
{
if(!in_array($item['name'], $checkArray))
{
$desiredArray[]['name'] = $item['name'];
}
}
试试这个,我不能说我为此感到骄傲,但它可能只是做你需要的。此代码假定第一个和第二个数组都具有相应的名称字段。我没有对此进行过广泛测试,但如果您需要,请随时使用。
答案 2 :(得分:-2)
所以,让我直截了当,以便我真的可以尝试回答你的问题:
您的目标是确保“第一个”数组中的选定项目的排序方式与“第二个”数组相同。所以你想要一个不同顺序的相同数组值,那个顺序是“第二个”数组中指定的顺序?