如何在php中基于第二个数组更改数组位置
我有两个多维数组,我想在第二个数组的基础上改变数组元素的位置。我从来没有这样做过,所以不知道如何用数组来做。这里我发布了我的数组结果 第一(主)数组:如何在php中基于第二个数组更改数组位置,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有两个多维数组,我想在第二个数组的基础上改变数组元素的位置。我从来没有这样做过,所以不知道如何用数组来做。这里我发布了我的数组结果 第一(主)数组: Array ( [0] => stdClass Object ( [user_type] => U [user_id] => 156 [property_id] => 1201 ) [1] =>
Array
(
[0] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1201
)
[1] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1200
)
[2] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1196
)
[3] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1193
)
[4] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1191
)
[5] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1145
)
[6] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1144
)
[7] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1139
)
[8] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1135
)
[9] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1053
)
)
Array
(
[0] => stdClass Object
(
[bounced_id] => 2
[user_id] => 156
[property_id] => 1193
)
[1] => stdClass Object
(
[bounced_id] => 1
[user_id] => 156
[property_id] => 1191
)
[2] => stdClass Object
(
[bounced_id] => 26
[user_id] => 156
[property_id] => 1200
)
)
Array
(
[0] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1193
)
[1] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1191
)
[2] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1200
)
[3] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1201
)
[4] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1196
)
[5] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1145
)
[6] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1144
)
[7] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1139
)
[8] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1135
)
[9] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1053
)
)
第二个数组:
Array
(
[0] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1201
)
[1] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1200
)
[2] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1196
)
[3] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1193
)
[4] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1191
)
[5] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1145
)
[6] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1144
)
[7] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1139
)
[8] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1135
)
[9] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1053
)
)
Array
(
[0] => stdClass Object
(
[bounced_id] => 2
[user_id] => 156
[property_id] => 1193
)
[1] => stdClass Object
(
[bounced_id] => 1
[user_id] => 156
[property_id] => 1191
)
[2] => stdClass Object
(
[bounced_id] => 26
[user_id] => 156
[property_id] => 1200
)
)
Array
(
[0] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1193
)
[1] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1191
)
[2] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1200
)
[3] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1201
)
[4] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1196
)
[5] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1145
)
[6] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1144
)
[7] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1139
)
[8] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1135
)
[9] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1053
)
)
我想要此数组结果:
Array
(
[0] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1201
)
[1] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1200
)
[2] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1196
)
[3] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1193
)
[4] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1191
)
[5] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1145
)
[6] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1144
)
[7] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1139
)
[8] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1135
)
[9] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1053
)
)
Array
(
[0] => stdClass Object
(
[bounced_id] => 2
[user_id] => 156
[property_id] => 1193
)
[1] => stdClass Object
(
[bounced_id] => 1
[user_id] => 156
[property_id] => 1191
)
[2] => stdClass Object
(
[bounced_id] => 26
[user_id] => 156
[property_id] => 1200
)
)
Array
(
[0] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1193
)
[1] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1191
)
[2] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1200
)
[3] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1201
)
[4] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1196
)
[5] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1145
)
[6] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1144
)
[7] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1139
)
[8] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1135
)
[9] => stdClass Object
(
[user_type] => U
[user_id] => 156
[property_id] => 1053
)
)
如果我理解正确的话,您需要找到第一个和第二个数组之间的差异,然后将这些元素附加到第二个数组中
$array_diff = array_diff($first,$second);
$array_result = array_merge($second, $array_diff);
一种方法是循环数组和容器。在循环下,如果您的特定键匹配,则将其第一个匹配的项放入(某种过滤),然后在完成后,合并其余项
$result = array(); // container
foreach($array2 as $k2 => $val2) {
foreach($array1 as $k1 => $val1) {
if($val2->property_id == $val1->property_id) { // if it matches
$result[] = $val1; // put it inside
unset($array2[$k2], $array1[$k1]); // remove to continue next set
break;
}
}
}
$result = array_merge($result, $array1); // merge the rest
甚至连foreach都没有尝试过?对不起,我不知道怎么做:(@Mr.Happy)只要你不尝试,你就不会被卡住!你能用代码发布你的数组吗format@User这个数组是我从sql结果中得到的结果。