php get array key by value multidimensional – How to get specific key value from multidimensional array in php?

php get array key by value multidimensional – array_column — Return the values from a single column in the input array. PHP Multidimensional Array Searching (Find key by specific value).

php get array key by value multidimensional

multidimensional array search refers to searching a value in a multilevel nested array. PHP multidimensional array search by value.

$member_names = array_column($membersList, 'name');

$emails = array_map(function ($ar) {return $ar['email'];}, $membersList);

Example

<?php
  
    $membersList = [
        [
          'name' => 'krunal',
          'email' => '[email protected]'
        ],
        [
          'name' => 'Ankit',
          'email' => '[email protected]'
        ],
        [
          'name' => 'Dinesh',
          'email' => '[email protected]'
        ],
    ];
  
    $member_names = array_column($membersList, 'name');
    $emails = array_map(function ($ar) {return $ar['email'];}, $membersList);
  
    print_r($member_names);
    print_r($emails);
  
?>

Result

Array
(
    [0] => krunal
    [1] => Ankit
    [2] => Dinesh
)
Array
(
    [0] => [email protected]
    [1] => [email protected]
    [2] => [email protected]
)

Don’t Miss : PHP Multidimensional Array Check If Specific Array Key

Read Also:  sort query order by in codeigniter Examples

PHP multidimensional array search by value

function getProductDate($items, $field, $value)
{
   foreach($items as $key => $item)
   {
      if ( $item[$field] === $value )
         return $key;
   }
   return false;
}

PHP Multidimensional Array Search By Value Example

<?php
    $players = [
        [

            "id" => "1",
            "name" => "Dev",
            "email" => "[email protected]"
        ],
        [

            "id" => "2",
            "name" => "sachin",
            "email" => "[email protected]"

        ],
        [

            "id" => "3",
            "name" => "Pankil",
            "email" => "[email protected]"

        ],
        [

            "id" => "4",
            "name" => "Krunal",
            "email" => "[email protected]"
        ]
    ];

    $playersAddress = [
        [

            "member_id" => "3",
            "address" => "Surat, Kolkata, India"

        ],
        [

            "member_id" => "1",
            "address" => "Ahemdabad, Kolkata, India"
        ]

    ];

?>    

  

<h2>PHP Multidimensional Array Search By Value Example - www.pakainfo.com</h2>
<table border="1" width="700">

    <tr>
        <td>ID</td>
        <td>Name</td>
        <td>Email</td>
        <td>Address</td>
    </tr>


    <?php foreach ($players as $key => $mData): ?>
    <tr>

        <td><?php echo $mData['id'] ?></td>
        <td><?php echo $mData['name'] ?></td>
        <td><?php echo $mData['email'] ?></td>
        <td>
        <?php 

            $key = array_search($mData['id'], array_column($playersAddress, 'member_id'));

            if (!empty($key) || $key === 0) {
                echo $playersAddress[$key]['address'];
            }
        ?>
        </td>
    </tr>

    <?php endforeach ?>
</table>

I hope you get an idea about php get array key by value multidimensional.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.