我一直在试着这几个小时。 我有两个多维数组。
$newData (
[0] => Array(
[id] => 1
[name] => John
[sex] => male
)
[1] => Array(
[id] => 2
[name] => Kenny
[sex] => male
)
[2] => Array(
[id] => 3
[name] => Sarah
[sex] => female
)
[3] => Array(
[id] => 4
[name] => George
[sex] => male
)
)
$oldData (
[0] => Array(
[id] => 3
[name] => Sarah
[sex] => female
)
[1] => Array(
[id] => 4
[name] => George
[sex] => male
)
[2] => Array(
[id] => 5
[name] => Peter
[sex] => male
)
[3] => Array(
[id] => 6
[name] => Lexi
[sex] => female
)
)
如果$oldData
中的名称与$newData
匹配,我需要在不使用更多循环的情况下获取该数组的ID
答案 0 :(得分:0)
答案 1 :(得分:0)
我认为循环不是那么复杂。如果我正确地理解了您的问题,代码看起来会像这样:
$matches = array();
foreach($newData as $index => $newItem)
foreach($oldData as $oldItem)
if($newData['name'] === $oldItem['name'])
$matches[] = $index;
答案 2 :(得分:0)
使用两个循环可以获得数组键。
$oldDataCount = count($oldData);
$result = array();
for($i = 0; $i < count($newData); $i++)
for($j =0; $j < $oldDataCount; $j++)
if ($oldData[$j]['name'] == $newData[$i]['name'])
$result[$newData[$i]['name']] = $j;
print_r($result);
现在,数组保存结果Array ( [Sarah] => 0 [George] => 1 )