Created
May 16, 2016 13:27
-
-
Save codfish/4fde307b23cc6605ebd730eb6170d8b9 to your computer and use it in GitHub Desktop.
Search multi-dimensional array based on key value pair. i.e. you have an array of posts, and you want to return the post with the id of `1231`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$posts = [ | |
[ | |
'id' => 1231, | |
'title' => 'Test' | |
], | |
[ | |
'id' => 1111, | |
'title' => 'Test #2' | |
] | |
]; | |
$key = array_search(1231, array_column($posts, 'id')); | |
$post = $key ? $posts[$key] : null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment