Capture only one element of an array

Asked

Viewed 100 times

2

I got the following array, how could I store in a new array only the "producto_id" of all "loja_id"?

array(2) { 
      [0]=> array(2) { 
          ["loja_id"]=> string(3) "286" 
               [0]=> array(2) { 
                   ["produto_id"]=> string(4) "7224" ["qtd"]=> int(1) 
                } 
               [1]=> array(2) { 
                   ["produto_id"]=> string(4) "7167" ["qtd"]=> int(1) 
               }
      }
      [1]=> array(2) { 
           ["loja_id"]=> string(3) "133" 
               [0]=> array(2) { 
                    ["produto_id"]=> string(4) "1078" ["qtd"]=> int(1) }
               } 
       }
  • 1

    Var_dump the array and show it in the question, please. The current representation seems wrong to me.

  • Okay, I changed my question.

1 answer

2


Can use array_map() to modify/reorganize the output array and array_column() to capture key data produto_id at once.

The keyword use imports the variable $produtos for the scope of the anonymous function note that it is changed by reference (&), array_push() combined with the operator ... add each element of the array returned by array_column() in $produtos

$str = '[{"loja_id":"286",  "0":{"produto_id":"7224","qtd":1},  "1":{"produto_id":"7235","qtd":1}},{"loja_id":"133","0":{"produto_id":"1078","qtd":1}}]';

$json = json_decode($str, true);

$produtos = array();
array_map(function($item) use(&$produtos){array_push($produtos, ...array_column($item, 'produto_id')); }, $json);

echo "<pre>";
print_r($produtos);

Return:

Array
(
    [0] => 7224
    [1] => 7235
    [2] => 1078
)
  • How would I look to extract only the products? For example: [7224, 7235, 1078]

  • @Diegovieira looks something like foreach($novo as $itens){&#xA; echo 'loja: '. $itens['id_loja'] .'<br>produtos: <br>';&#xA; foreach($itens['produtos'] as $item){&#xA; echo $item .'<br>';&#xA; }&#xA;}

  • @Diegovieira you don’t need the loja_id? if you can’t change the format in array_map() I can edit the answer.

  • For the current function I will not need no. It is only in the original array pq I will use further in the project. OBS: This array is in a Session.

  • I just need the products to make a select in the bank.

  • @Diegovieira modified the answer.

  • It returns me an empty array: Array ( [0] => [1] => )

  • @Diegovieira vc has to use the array $produtos the $novo you won’t get anything at all.

  • One thing I forgot to ask. Do you think I mounted the array correctly? There would be some way to mount where I can access the elements only with foreach, that is, without using the array_map?

  • @Diegovieira if you take the array_map() will need other 2 or 3 foreachs and check the keys to hence extract the values.

  • I had tried this way here, but it does not recognize the index: foreach($array as $a){ foreach($a as $product){ $p[] = $product['producto_id']; } }

  • @Diegovieira if you’re gonna do it that way you need one isset() to check if the key exists, as you have different indices happens this.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.