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,

Jixone
  • 190
  • 11
rjcode
  • 1,193
  • 1
  • 25
  • 56
  • 1
    Possible duplicate of [PHP multidimensional array search by value](https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value) – yoeunes Aug 21 '17 at 08:57

4 Answers4

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];
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    It's worth mentioning that [`array_column()`](http://php.net/manual/en/function.array-column.php) is available only in PHP 5.5 and newer. – axiac Aug 21 '17 at 08:33
  • ... and that all PHP versions up to 5.5 have [end of life status](http://php.net/eol.php), i.e. they are no longer supported ;-) – trincot Aug 21 '17 at 08:41
  • 1
    Sure, but no longer supported is not the same as no longer in use. – axiac Aug 21 '17 at 08:45
  • @trincot what if 7 does not exists in the array, this will always show the first item of the multidimentional array – yoeunes Aug 21 '17 at 08:56
  • @younes, I added the code to return `false` if the value is not in the array. Thanks! – trincot Aug 21 '17 at 08:59
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.

axiac
  • 68,258
  • 9
  • 99
  • 134
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

Tan Bui
  • 137
  • 3
  • 11
  • This is array of arrays, so you should check index, not object property: `if($value[$category_id] == $cat_id){ ...` – aslawin Aug 21 '17 at 08:27
0
$val = 7;
$key = 'category_id';

foreach ($array as $item){
    if (isset($item[$key]) && $item[$key] === $val)
            print_r($item);
   }
MrChux
  • 110
  • 1
  • 10