Skip to content

Instantly share code, notes, and snippets.

@codfish
Created May 16, 2016 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codfish/4fde307b23cc6605ebd730eb6170d8b9 to your computer and use it in GitHub Desktop.
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`
<?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