【发布时间】:2020-03-01 08:57:15
【问题描述】:
我有一个这样的数组
Array
(
[id] => 3
[type] => default
[place] => 1
)
Array
(
[id] => 3
[type] => default
[place] => 2
)
Array
(
[id] => 3
[type] => default
[place] => 3
)
这个数组是从这个php创建的
for($count=1;$count <= 3;$count++){
$places_array = array(
"id" => "3",
"type" => "default",
"place" => $count,
);
}
现在我想改变这个数组的结果,如果这个地方被 php mysql 数据找到了。例如我有这个数组。
Array
(
[id] => 7
[type] => 1
[place] => 2
[description] => This is item place 2
[image] => this_is_the_image.png
)
如您所见,第二个数组位于“位置 2”。现在我希望结果是这样的
Array
(
[id] => 3
[type] => default
[place] => 1
)
Array
(
[id] => 7
[type] => 1
[place] => 2
[description] => This is item place 2
[image] => this_is_the_image.png
)
Array
(
[id] => 3
[type] => default
[place] => 3
)
如何做到这一点?我已经完成了 array_search 函数,但没有运气。 任何人请帮助我
========================编辑完整代码===================== ============ 这是代码,我正在显示数据库中的数据并在while循环函数中调用它
for($count=1;$count <= $max_places;$count++){
$array[] = array(
"id" => $res['id'],
"type" => "default",
"place" => $count
);
while($arr = $stmts->fetch()){
$key = array_search($arr['place'], array_column($array, 'place'));
if($key && array_key_exists($key, $array)) {
$array[$key] = [
"id" => $arr['id'],
"type" => $arr['type'],
"place" => $arr['place'],
"url" => $arr['url'],
"image" => $arr['image']
];
}
}
}
==========================交换码==================== ==========
while($arr = $stmts->fetch()){
$array[] = [
"id" => $arr['id'],
"type" => $arr['type'],
"place" => $arr['place'],
"url" => $arr['url'],
"image" => $arr['image']
];
for($count=1;$count <= $max_places;$count++){
$key = array_search($arr['place'], array_column($array, 'place'));
if($key && array_key_exists($key, $array)) {
$array[] = array(
"id" => $res['id'],
"type" => "default",
"place" => $count
);
}
}
}
【问题讨论】: