0

is there a built-in php function to find a value in a specific column of an array ?

I have an array filled with users data, and I want to check if an ID is present. My problem is that in_array check everything, so per example if the user is 20 years old and I want to check if the user where ID = 20 exists, I will get true even if there is no 20 in ID column.

I know that I can do that easily with foreach, my question is only : Has php a ready to use function to do that ?

Thanks.

FLX
  • 2,626
  • 4
  • 26
  • 57
  • Could you show us array ? – DeiForm Jul 25 '13 at 08:23
  • can you show us code, which you are using right now? – Sumit Bijvani Jul 25 '13 at 08:23
  • you can try `array_key_exists` – insanebits Jul 25 '13 at 08:23
  • No, but [this question][1] might help you. [1]: http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php – JohnnyFaldo Jul 25 '13 at 08:23
  • Check the documentation for these types of questions: http://www.php.net/manual/en/ref.array.php . As far as your intent, you want to check `$someArray['id'] == 20` instead of checking if there is a 20 in the entire array? That approach assumes you know the column name, `id`. – BLaZuRE Jul 25 '13 at 08:23
  • In fact, this is an array of array. Each row contains user's datas, with an ID column that I want to check. Php has a lots of array functions, so I'm a bit surprised that there is nothing to do that. – FLX Jul 25 '13 at 08:29

3 Answers3

7

No. There are a number of workarounds, but nothing that's superior to foreach:

  • You could use array_column or array_map to isolate the column in question and search inside the result, but this preprocesses the input and uses additional memory.
  • You could use array_walk to iterate over the array and search for the element, but this is in essence a worse version of foreach because it involves function calls (which are expensive in PHP) and does not stop immediately after the needle is found in the haystack.
  • You could use array_filter and see if the result is empty or not, but again this is a worse version of foreach (it has a mix of the drawbacks of the above methods).
Jon
  • 428,835
  • 81
  • 738
  • 806
1

How search value in Multi columns array?

this is my idea...

function  is_column_in_array($value,$column,$array){
        $rows = array_column( $array,$column);
        if( in_array($value,$rows)){
            return true;
        }
        return false;
    }

how to use!

$rows = array();
if( ! $this->is_column_in_array( $item->roz,'roz' ,$rows )){
               array_push($rows,
                   array(
                       'roz' => $item->roz,
                       'rozName' => $item->rozName
                   )) ;
           }
hamed hossani
  • 986
  • 2
  • 14
  • 33
0
$adult_count = 0;
$child_count = 0;
foreach($passenger_info as $key => $value) {
    if($value["room_no"] == $selected_room){
        switch ($value["passenger_type"]){
            case "adult":
                $adult_count++;
                break;
            case "children":
                $child_count++;
                break;
            default:
                break;
        }

    }

}

echo "Adults : ".$adult_count;
echo "<br>";
echo "Children : ".$child_count;
Niranjan
  • 43
  • 6