rjcode
rjcode

Reputation: 1349

Check if value exist in array and get array from multidimensional array

I have below PHP array:

Array ( 
       [0] => Array ( [policy_id] => 1 [category_id] => 5 [limit_amount] => 11.00 [limit_amount2] => 23.00 ), 
       [1] => Array ( [policy_id] => 1 [category_id] => 7 [limit_amount] => 32.00 [limit_amount2] => 23.00 ), 
       [2] => Array ( [policy_id] => 1 [category_id] => 4 [limit_amount] => 12.00 [limit_amount2] => 12.00 ) )

Now i want to do two things:

  1. Want to check if category_id = 7 exists in this array or not, and if it is there.
  2. then i would like to get that complete array from this multidimensional array,

Example, if category_id = 7 is in array then it should output

Array ([policy_id] => 1 ,
       [category_id] => 7 ,
       [limit_amount] => 32.00,
       [limit_amount2] => 23.00 )

I tried to use in_array(), but could not get required values,

Thanks for help,

Upvotes: 1

Views: 216

Answers (4)

axiac
axiac

Reputation: 72186

There are multiple ways to achieve your goal. This is a solution that uses array_filter():

$input = array(
    array('policy_id' => 1 , 'category_id' => 5 , 'limit_amount' => 11.00 , 'limit_amount2' => 23.00, ),
    array('policy_id' => 1 , 'category_id' => 7 , 'limit_amount' => 32.00 , 'limit_amount2' => 23.00, ),
    array('policy_id' => 1 , 'category_id' => 4 , 'limit_amount' => 12.00 , 'limit_amount2' => 12.00, ),
);

$categoryId = 7;
$output = array_filter(
    $input,
    function (array $item) use ($categoryId) {
        return $item['category_id'] == $categoryId;
    }
);
print_r($output);

The output is:

Array
(
    [1] => Array
        (
            [policy_id] => 1
            [category_id] => 7
            [limit_amount] => 32
            [limit_amount2] => 23
        )
)

The code above finds all entries from $input that have 7 in category_id, associated to the original keys in $input.

You can enumerate $output using foreach ($output as item) { ... } or, if you need only the first match you can get it using current():

print_r(current($output));

produces:

Array
(
    [policy_id] => 1
    [category_id] => 7
    [limit_amount] => 32
    [limit_amount2] => 23
)

Update:

If the item cannot be found in the input list (f.e. when $categoryId = 1;), $output is an empty array (array()). It can still be enumerated using foreach but current($output) returns FALSE.

All in all, the code:

$categoryId = 7;
$output = current(array_filter(
    $input,
    function (array $item) use ($categoryId) {
        return $item['category_id'] == $categoryId;
    }
));

puts in $output the first item found or FALSE if there is none.

Upvotes: 1

trincot
trincot

Reputation: 350137

You can use array_column before doing the array_search (I assume your data is in $array):

$match = $array[array_search(7, array_column($array, "category_id"))];

If you need to first check whether the entry really exists, then first check the return value of array_search, and return something distinctive when the value is not found, e.g. false:

$index = array_search(7, array_column($array, "category_id"));
$match = $index === false ? false : $array[$index];

Upvotes: 5

MrChux
MrChux

Reputation: 120

$val = 7;
$key = 'category_id';

foreach ($array as $item){
    if (isset($item[$key]) && $item[$key] === $val)
            print_r($item);
   }

Upvotes: 0

Tan Bui
Tan Bui

Reputation: 137

With that problem u can use foreach array to check exists it! I had make a function for that:

function checkArray($cat_id){
  foreach($arr as $index => $value){
    if($value->category_id == $cat_id){
      return $arr[$index];
    }
    return false;
  }
}

if $cat_id exists return array u need, and if not exists return false

Upvotes: 0

Related Questions