Search PHP array value

Asked

Viewed 57 times

-1

How can I search for the key value of "item_a" and return all the key values of "valor_a" in the array below? Example: I want to search by item_a = 3 and return the values 396, 1514 and 2106.

array (size=6)
  0 => 
    array (size=2)
      'item_a' => string '3' (length=1)
      'valor_a' => string '396' (length=3)
  1 => 
    array (size=2)
      'item_a' => string '3' (length=1)
      'valor_a' => string '1514' (length=4)
  2 => 
    array (size=2)
      'item_a' => string '3' (length=1)
      'valor_a' => string '2106' (length=4)
  3 => 
    array (size=2)
      'item_a' => string '6' (length=1)
      'valor_a' => string '1020' (length=4)
  4 => 
    array (size=2)
      'item_a' => string '4' (length=1)
      'valor_a' => string '1319' (length=4)
  5 => 
    array (size=2)
      'item_a' => string '11' (length=2)
      'valor_a' => string '1912' (length=4)

2 answers

0

Following the logic you need to:

1 - Array with values

$items = [
  ["item_a" =>3,"valor_a" =>396],
  ["item_a" =>3,"valor_a" =>1514],
  ["item_a" =>3,"valor_a" =>2106],
  ["item_a" =>6,"valor_a" =>1020],
  ["item_a" =>4,"valor_a" =>1319],
  ["item_a" =>11,"valor_a" =>1912]
];

2 - run a cycle to check at each position of the array($items) within it the position item_a is 3 , if yes you take the value and store it in another array

foreach( $items as $key => $value ){
  if($value["item_a"] == 3){
     $b[] = $value["valor_a"];
  }
}

3 - Now you need to show these values so you make a new cycle in the new array

foreach( $b as $key => $value ){
  echo $value;
}

Complete code

<?php
$items = [
  ["item_a" =>3,"valor_a" =>396],
  ["item_a" =>3,"valor_a" =>1514],
  ["item_a" =>3,"valor_a" =>2106],
  ["item_a" =>6,"valor_a" =>1020],
  ["item_a" =>4,"valor_a" =>1319],
  ["item_a" =>11,"valor_a" =>1912]
];
$b = [];    
foreach( $items as $key => $value ){
  if($value["item_a"] == 3){
    $b[] = $value["valor_a"];
  }
}
foreach( $b as $key => $value ){
  echo $value;
}
?>
  • Wouldn’t it be better to already display on the screen in the first foreach? if($value["item_a"] == 3) { echo $value['valor_a']; }

  • If you made one function to filter and return a new array and another to display it would be the right one, but since it is not a function, it is not necessary to create another array. I printed it straight! But it works.

0

Here are two examples that can give you an overview of how to solve your problem. Test here https://3v4l.org/uPRHp

<?php

$items = [
  ["item_a" =>3,"valor_a" =>396],
  ["item_a" =>3,"valor_a" =>1514],
  ["item_a" =>3,"valor_a" =>2106],
  ["item_a" =>6,"valor_a" =>1020],
  ["item_a" =>4,"valor_a" =>1319],
  ["item_a" =>11,"valor_a" =>1912]
];
$search = 3;
$newArray = array_filter($items, function($item) use($search) {
    return $item['item_a'] == $search;
});

print_r($newArray);

$newArray2 = array_map(function($item) use($search) {
    if ($item['item_a'] == $search) {
      return $item['valor_a'];
    }
},$items);


print_r(array_filter($newArray2));

Exit

    Array
(
    [0] => Array
        (
            [item_a] => 3
            [valor_a] => 396
        )

    [1] => Array
        (
            [item_a] => 3
            [valor_a] => 1514
        )

    [2] => Array
        (
            [item_a] => 3
            [valor_a] => 2106
        )

)
Array
(
    [0] => 396
    [1] => 1514
    [2] => 2106
)
  • It worked!! Tanks

  • But you’ve managed to understand what each function does?

  • Oops... I got it... logic is cool to understand!!

Browser other questions tagged

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