Skip to content
Advertisement

search for a value array in array in php

In my program i have arrays in the following format . I need to find whether a value is present in an the array

return [ 
    "affiliates" => [
        "name" => 'Affiliates',
        "value" => 11
               
     ],
     "business" => [
         "name" => 'Business',
         "value" => 12
               
     ],
     "inquiries" => [
         "name" => 'Inquiries',
         "value" => 13
               
     ],
     "students" => [
         "name" => 'Students',
         "value" => 14
               
     ],
     "teachers" => [
         "name" => 'Teachers',
         "value" => 15
               
     ],
     "Personal" => [
         "name" => 'Personal',
         "value" => 3
               
     ],
    
];

I am doing the following. I am expecting that the search will find the value(12) which is a business, but It is returning false.

 $searchcategoryid = '12';
        
        $key = array_search($searchcategoryid, array_column(config('peopletypes.students'), 'value'));

Please note that config(‘peopletypes.students’)will return the array above mentioned. I am using laravel.

Advertisement

Answer

You have some sort of configuration problem, this code prints 1.

<?php

$searchcategoryid = '12';
$key = array_search($searchcategoryid, array_column([ 
    "affiliates" => [
        "name" => 'Affiliates',
        "value" => 11
     ],
     "business" => [
         "name" => 'Business',
         "value" => 12
     ],
     "inquiries" => [
         "name" => 'Inquiries',
         "value" => 13
     ],
     "students" => [
         "name" => 'Students',
         "value" => 14
     ],
     "teachers" => [
         "name" => 'Teachers',
         "value" => 15
     ],
     "Personal" => [
         "name" => 'Personal',
         "value" => 3
     ],
], 'value'));

echo $key;

Try to make sure that your config function actually returns the array you claim it to.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement