1

I have a multidimensional array like this:

$a=Array
(
     Array
        (
             Array
                (
                    'id' => 1265451,
                    'num' => 09381554465
                ),

            Array
                (
                    'id' => 1265451,
                    'num' => 09370777561
                ),

             Array
                (
                    'id' => 1265451,
                    'num' => 0963665361
                ),

             Array
                (
                    'id' => 1265451,
                    'num' => 0943256361
                ),

             Array
                (
                    'id' => 1265451,
                    'num' => 0975956361
                ),

             Array
                (
                    'id' => 1265451,
                    'num' => 0963516361
                ),

        ),


     Array
        (
            Array
                (
                    'id' => 1265451,
                    'num' => 0133377469
                ),

            Array
                (
                    'id' => 1265451,
                    'num' => 02156326987
                ),

            Array
                (
                    'id' => 1265451,
                    'num' => 01399632548
                ),

        ),


);

I need to search for a specific number in num and return the associated id. I made two attempts, with no success:

This returns null:

$key = array_search(09370777561, $a);
    echo ("**The key is: ". $key);

This returns false:

var_dump(in_array(09370777561, $a));

I expected it to return the id 1265451.

This array contains phone numbers and can be very large.

Matt Raines
  • 4,149
  • 8
  • 31
  • 34
hsoft
  • 49
  • 7
  • You can follow [https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value](https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value) – Sambhaji Katrajkar Apr 27 '18 at 11:04
  • The telephone numbers need to be enclosed in single (`'`) or double (`"`) quotes. This is because they are strings and not integers. In PHP, literal integers beginning with a `0` are octal numbers. For example, `020` is the same as `16`. `09370777561` isn't a valid octal number, since it cannot contain the digit `9`. In PHP 7, you would receive a useful error message about this, but before that the remainder of the number is ignored. Once you've fixed this, your question is then the same as https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value – Matt Raines Apr 27 '18 at 11:59
  • i tried both with or without quotes but still nothing – hsoft Apr 27 '18 at 12:21

1 Answers1

1

You can use like this:

$column_name = "num";
$key = array_search('09370777561', array_column($your_array, $column_name));
Hetal Chauhan
  • 787
  • 10
  • 22
  • thanks for help. i tried your code. but it didnt work.it work on 2 dimention array well. but in 3 or more like my array it returns false – hsoft Apr 27 '18 at 11:59
  • This syntax only works in PHP 5.5 and above. There are solutions for earlier versions of PHP in the answers to the question in the comments above. If you are indeed using an earlier version, I *strongly* advise you to upgrade. 5.4 was end-of-lifed over 2 years ago, so you are not getting security updates, and subsequent versions are lots faster too. – Matt Raines Apr 27 '18 at 12:02
  • my current php is 5.6.30 – hsoft Apr 27 '18 at 12:20