0

I have array

`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
   )
);`

I need result when [id] is same value only first to show [total] and other need to show - like below please.

 `array(
  [0] =>array(
     [id] => 1,
     [item] => ring,
     [total] => 1000
   ),
  [1] =>array(
     [id] => 1,
     [item] => book,
     [total] => -
   ),
  [2] =>array(
     [id] => 1,
     [item] => pen,
     [total] => 400
   )
  );`

Thank you for help please.

2
  • The same with the previeus element you mean? Or the first element of the array? What have you done so far? Commented May 4, 2019 at 4:52
  • sorry my miss take at [2] =>array( [id] => 2, [item] => pen, [total] => 400 ) Commented May 4, 2019 at 5:14

1 Answer 1

1

This script might help you to do so:

$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);

Output

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) "-"
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

excuse me @ line if ($value["id"] == $arr[(int) $key + 1]["id"] && $value["total"] == $arr[(int) $key + 1]["total"]) { $arr[(int) $key + 1]["total"] = '-'; } $key+1 Is "Undefined offset: 3" i think index 3 Undefined sir.
your old code it's work just add isset for check array before if ($arr Thank you for solve @Emma (^^) You safe my time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.