我正在制作一个使用 foreach 循环输出的网站。输出是一堆数组,但不太正确。我使用了 foreach 两次,因为它需要 2 个数组作为输出。我得到了我想要的数组,但我无法合并它们。我需要将它们合并,因为我想根据值对数组进行排序。
这是我使用的代码:
$counter_saw_wp = 0;
foreach($v as $key => $value){
foreach($v_wp as $k => $val){
$v_saw_wp[$k]["phone_id"] = $val["phone_id"];
$v_saw_wp[$k]["phone_name"] = $val["phone_name"];
$v_saw_wp[$k]["phone_ram"] = $val["phone_ram"];
$v_saw_wp[$k]["phone_memory"] = $val["phone_memory"];
$v_saw_wp[$k]["phone_img"] = $val["phone_img"];
$v_saw_wp[$k]["v_saw"] = $value ["v"];
$v_saw_wp[$k]["v_wp"] = $val["v_wp"];
$v_saw_wp[$k]["v_saw_wp"] = (($value["v"] + $val["v_wp"])/2);
}
$v_total_saw_wp = array();
$v_total_saw_wp[] = array_merge($v_total_saw_wp, $v_saw_wp[$counter_saw_wp]);
$counter_saw_wp++;
var_dump($v_total_saw_wp);
}
这是我
var_dump
变量时的输出:
array(1) {
[0]=>
array(8) {
["phone_id"]=>
string(2) "26"
["phone_name"]=>
string(19) "Samsung Galaxy S23+"
["phone_ram"]=>
string(1) "8"
["phone_memory"]=>
string(3) "256"
["phone_img"]=>
string(11) "s23plus.png"
["v_saw"]=>
float(45)
["v_wp"]=>
float(2.7588120104614227)
["v_saw_wp"]=>
float(23.879406005230713)
}
}
array(1) {
[0]=>
array(8) {
["phone_id"]=>
string(2) "27"
["phone_name"]=>
string(24) "Samsung Galaxy S23 Ultra"
["phone_ram"]=>
string(2) "12"
["phone_memory"]=>
string(3) "256"
["phone_img"]=>
string(12) "s23ultra.png"
["v_saw"]=>
float(41.25)
["v_wp"]=>
float(2.5434238896587567)
["v_saw_wp"]=>
float(21.896711944829377)
}
}
array(1) {
[0]=>
array(8) {
["phone_id"]=>
string(2) "28"
["phone_name"]=>
string(24) "Samsung Galaxy S23 Ultra"
["phone_ram"]=>
string(2) "12"
["phone_memory"]=>
string(3) "512"
["phone_img"]=>
string(12) "s23ultra.png"
["v_saw"]=>
int(40)
["v_wp"]=>
float(2.475319476936885)
["v_saw_wp"]=>
float(21.23765973846844)
}
}
但是如果我使用
$v_total_saw_wp = $v_saw_wp[$counter_saw_wp];
而不是
$v_total_saw_wp = array();
$v_total_saw_wp[] = array_merge($v_total_saw_wp, $v_saw_wp[$counter_saw_wp]);
这是我得到的输出:
array(8) {
["phone_id"]=>
string(2) "26"
["phone_name"]=>
string(19) "Samsung Galaxy S23+"
["phone_ram"]=>
string(1) "8"
["phone_memory"]=>
string(3) "256"
["phone_img"]=>
string(11) "s23plus.png"
["v_saw"]=>
float(45)
["v_wp"]=>
float(2.7588120104614227)
["v_saw_wp"]=>
float(23.879406005230713)
}
array(8) {
["phone_id"]=>
string(2) "27"
["phone_name"]=>
string(24) "Samsung Galaxy S23 Ultra"
["phone_ram"]=>
string(2) "12"
["phone_memory"]=>
string(3) "256"
["phone_img"]=>
string(12) "s23ultra.png"
["v_saw"]=>
float(41.25)
["v_wp"]=>
float(2.5434238896587567)
["v_saw_wp"]=>
float(21.896711944829377)
}
array(8) {
["phone_id"]=>
string(2) "28"
["phone_name"]=>
string(24) "Samsung Galaxy S23 Ultra"
["phone_ram"]=>
string(2) "12"
["phone_memory"]=>
string(3) "512"
["phone_img"]=>
string(12) "s23ultra.png"
["v_saw"]=>
int(40)
["v_wp"]=>
float(2.475319476936885)
["v_saw_wp"]=>
float(21.23765973846844)
}
但它应该看起来像这样:(所以我可以对它进行排序)
array(3) {
[0]=>
array(8) {
["phone_id"]=>
string(2) "26"
["phone_name"]=>
string(19) "Samsung Galaxy S23+"
["phone_ram"]=>
string(1) "8"
["phone_memory"]=>
string(3) "256"
["phone_img"]=>
string(11) "s23plus.png"
["v_saw"]=>
float(45)
["v_wp"]=>
float(2.7588120104614227)
["v_saw_wp"]=>
float(23.879406005230713)
}
[1]=>
array(8) {
["phone_id"]=>
string(2) "27"
["phone_name"]=>
string(24) "Samsung Galaxy S23 Ultra"
["phone_ram"]=>
string(2) "12"
["phone_memory"]=>
string(3) "256"
["phone_img"]=>
string(12) "s23ultra.png"
["v_saw"]=>
float(41.25)
["v_wp"]=>
float(2.5434238896587567)
["v_saw_wp"]=>
float(21.896711944829377)
}
[2]=>
array(8) {
["phone_id"]=>
string(2) "28"
["phone_name"]=>
string(24) "Samsung Galaxy S23 Ultra"
["phone_ram"]=>
string(2) "12"
["phone_memory"]=>
string(3) "512"
["phone_img"]=>
string(12) "s23ultra.png"
["v_saw"]=>
int(40)
["v_wp"]=>
float(2.475319476936885)
["v_saw_wp"]=>
float(21.23765973846844)
}
}
我试过在 foreach 循环中使用
array_merge
但它不起作用。它只是将每个数组放入另一个数组(如上所示)。我也试过在 foreach 循环外使用 array_merge
但它只是从循环中获取最后一个数组。