RuleBritannia Posted November 22, 2013 Share Posted November 22, 2013 Hello If I have a multidimensional array <?php Array ( [0] => Array ( [id] => 40000 [type] => automatic [status] => error [colour] => o ) [1] => Array ( [id] => 50000 [type] => automatic [status] => good [colour] => e ) ) ?> I want to search for the status 'error', and if it exists within the array, return true(thats it(dont need keys and such to locate it)) I am fully aware I can just make a loop, But I want to know if its possible using a core function. I have tried continuosly using functions like array_search, in_array, array_keys,array_values, And I cannot acheive it, But I am sure its possible. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/284179-searching-array/ Share on other sites More sharing options...
Psycho Posted November 22, 2013 Share Posted November 22, 2013 (edited) I want to know if its possible using a core function. No But I am sure its possible. It's not (at least not with a single built-in function) . . dont need keys and such to locate it. If you are going to go to the trouble of building something, might as well build it to be extensible. Edited November 22, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/284179-searching-array/#findComment-1459600 Share on other sites More sharing options...
Psycho Posted November 22, 2013 Share Posted November 22, 2013 (edited) <?php function subarray_search($searchArray, $searchParam) { $searchKey = key($searchParam); $searchValue = current($searchParam); foreach($searchArray as $index => $subArray) { if(isset($subArray[$searchKey]) && $subArray[$searchKey]=$searchValue) { return $index; } } return false; } $findErrorStatus = subarray_search($myArray, array('status'=>'error')) if($findErrorStatus !== false) { echo "A status is in error"; } else { echo "None of the statses are in error"; } ?> Edited November 22, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/284179-searching-array/#findComment-1459602 Share on other sites More sharing options...
RuleBritannia Posted November 23, 2013 Author Share Posted November 23, 2013 No It's not (at least not with a single built-in function) If you are going to go to the trouble of building something, might as well build it to be extensible. Thanks for the function below, But I will continue trying for a while as I believe there maybe someway of acheiving this. As for building something to be extensible, I want to make my code as lightweight(resourcefully and both lines of code) as possible Quote Link to comment https://forums.phpfreaks.com/topic/284179-searching-array/#findComment-1459623 Share on other sites More sharing options...
Solution RuleBritannia Posted November 23, 2013 Author Solution Share Posted November 23, 2013 (edited) Ok So I did manage to find a core function to do this For those interested, array_column is the function. In what I was trying to acheive here, We can use it like this. <?php Array ( [0] => Array ( [id] => 40000 [type] => automatic [status] => error [colour] => o ) [1] => Array ( [id] => 50000 [type] => automatic [status] => good [colour] => e ) ) $status = array_column($array,'status'); //returns [0] = error [1] = good ?> With this result in a nice array, we can just use another simple function like in_array to test if the array contains a error string. I believe this is a much more elegant and simple way than to use big created functions or loops. EDIT, 1 last thought. If anybody has any other alternatives, please post, I have also found another, but its not so elegant as it consists of using 5 functions chained, But there is proberly even more ways to acheive this result. Thanks Edited November 23, 2013 by RuleBritannia Quote Link to comment https://forums.phpfreaks.com/topic/284179-searching-array/#findComment-1459631 Share on other sites More sharing options...
.josh Posted November 23, 2013 Share Posted November 23, 2013 okay well then you can do return false!==array_search('error',(array_column($array,'status'))); but note that array_column is introduced in php 5.5.0 so using it will probably not be very portable for a while.. Quote Link to comment https://forums.phpfreaks.com/topic/284179-searching-array/#findComment-1459632 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.