我有数组
`array(
[0] =>array(
[id] => 1,
[item] => ring,
[total] => 1000
),
[1] =>array(
[id] => 1,
[item] => book,
[total] => 1000
),
[2] =>array(
[id] => 1,
[item] => pen,
[total] => 400
)
);`
当[id]
仅是相同值时,我需要结果才能首先显示[total]
,而其他需要显示-
的情况如下。
`array(
[0] =>array(
[id] => 1,
[item] => ring,
[total] => 1000
),
[1] =>array(
[id] => 1,
[item] => book,
[total] => -
),
[2] =>array(
[id] => 1,
[item] => pen,
[total] => 400
)
);`
谢谢您的帮助。
答案 0 :(得分:0)
此脚本可能会帮助您做到这一点:
$arr = [
"0" => [
"id" => "1",
"item" => "ring",
"total" => "1000",
],
"1" => [
"id" => "1",
"item" => "book",
"total" => "1000",
],
"2" => [
"id" => "1",
"item" => "pen",
"total" => "400",
],
"3" => [
"id" => "1",
"item" => "pen",
"total" => "400",
],
"4" => [
"id" => "1",
"item" => "pen",
"total" => "400",
],
"5" => [
"id" => "1",
"item" => "pen",
"total" => "500",
],
"6" => [
"id" => "1",
"item" => "ring",
"total" => "1000",
],
];
$out_arr = array();
foreach ($arr as $key => $value) {
array_push($out_arr, $value);
if ($arr[(int) $key + 1]["total"] && $arr[(int) $key + 1]["id"]) {
foreach ($arr as $key2 => $value2) {
if ($value["id"] == $arr[(int) $key2 + 1]["id"] && $value2["total"] == $arr[(int) $key2 + 1]["total"]) {
$arr[(int) $key + 1]["total"] = '-';
}
}
}
}
var_dump($arr);
array(7) {
[0]=>
array(3) {
["id"]=>
string(1) "1"
["item"]=>
string(4) "ring"
["total"]=>
string(4) "1000"
}
[1]=>
array(3) {
["id"]=>
string(1) "1"
["item"]=>
string(4) "book"
["total"]=>
string(1) "-"
}
[2]=>
array(3) {
["id"]=>
string(1) "1"
["item"]=>
string(3) "pen"
["total"]=>
string(1) "-"
}
[3]=>
array(3) {
["id"]=>
string(1) "1"
["item"]=>
string(3) "pen"
["total"]=>
string(1) "-"
}
[4]=>
array(3) {
["id"]=>
string(1) "1"
["item"]=>
string(3) "pen"
["total"]=>
string(1) "-"
}
[5]=>
array(3) {
["id"]=>
string(1) "1"
["item"]=>
string(3) "pen"
["total"]=>
string(1) "-"
}
[6]=>
array(3) {
["id"]=>
string(1) "1"
["item"]=>
string(4) "ring"
["total"]=>
string(1) "-"
}
}