M
M
Maxim Osadchy2018-02-28 16:18:15
PHP
Maxim Osadchy, 2018-02-28 16:18:15

How to delete an array by value?

There is such nesting of arrays:

Array
(
    [0] => Array
        (
            [id] => 1
            [prices] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [myprice] => 50
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [myprice] => 1660
                        )

                    [2] => Array
                        (
                            [id] => 8
                            [myprice] => 15
                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [prices] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [myprice] => 80
                        )

                )

        )

)

It is necessary to remove all final subarrays [prices][$n] that have [prices][$n][id] != 2 I don't see the point in this code.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2018-02-28
@waspmax1

well, you do two foreach and delete by condition, post here at least your "attempts", because it's hard to even imagine what could be wrong here.

A
Anton, 2018-02-28
@Eridani

Wrong variant of reassembly

$newarr = Array();
foreach ($arr as $key1 => $item) {
  foreach($item['prices'] as $key2 => $price) {
    if($price['id'] != '2') {
      $newarr[$key1]['prices'][] = $price;
    }
    
  }
  
}
print_r($newarr);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question