1

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,

1

4 Answers 4

5

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

5 Comments

It's worth mentioning that array_column() is available only in PHP 5.5 and newer.
... and that all PHP versions up to 5.5 have end of life status, i.e. they are no longer supported ;-)
Sure, but no longer supported is not the same as no longer in use.
@trincot what if 7 does not exists in the array, this will always show the first item of the multidimentional array
@younes, I added the code to return false if the value is not in the array. Thanks!
1

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.

Comments

0

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

1 Comment

This is array of arrays, so you should check index, not object property: if($value[$category_id] == $cat_id){ ...
0
$val = 7;
$key = 'category_id';

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

Comments

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.