您可以尝试使用此代码,如果我正确理解了您的需要,它应该可以工作。
<?php
$phones = [];
$final = [];
foreach($forsend as $key => $record) {
if(!isset($phones[$record['phone']]))
//reversing - now we have an array which is arranged using phone number as a key,
//meaning that only one record can exist with the same number
$phones[$record['phone']] = $key;
else
//if the record is repeated, remove the previous instance, and do not add this one
//to the array
unset($phones[$record['phone']]);
}
foreach($phones as $phone){
//reversing again, getting the format that was used before, from $phone the key of the
//top array is returned and using that key the data is taken from
//$forsend and put to final
$final[$phone] = $forsend[$phone];
}
print_r($final);
要保留具有相同电话的第一个元素,只需删除
else
中的子句
if
这就足够了:
<?php
$phones = [];
$final = [];
foreach($forsend as $key => $record) {
if(!isset($phones[$record['phone']]))
$phones[$record['phone']] = $key;
}
foreach($phones as $phone){
$final[$phone] = $forsend[$phone];
}
print_r($final);