I have data something like this:
array("1"=>(array("UID"=>"5321", array("1"=>"age", "2"=>"gender", "3"=>"location")), "2"=>(array("UID"=>"3213", array("1"=>"location", "2"=>"gender")), "3"=>(array("UID"=>"4444", array("1"=>"application", "2"=>"gender")) );
so, if I search by “age”, the result is array("1"=>"5321")
if I search by “gender”, the result is array("1"=>"5321", "2"=>"3213", "3"=>"4444")
basically I want to find who has specific value in php, I need a hand to solve this.
Thanks for your time 🙂
EDIT, here’s my snip:
$arr = array("1"=>(array("UID"=>"5321", array("1"=>"age", "2"=>"gender", "3"=>"location"))), "2"=>(array("UID"=>"3213", array("1"=>"location", "2"=>"gender"))), "3"=>(array("UID"=>"4444", array("1"=>"application", "2"=>"gender"))) ); $find = "age"; $storedArr = false; foreach($arr as $key=>$arrValue){ if(in_array($find, $arrValue)){ if(!$storedArr) $storedArr = array("1"=>$arrValue["UID"]); else array_push($storedArr, $arrValue["UID"]); } } print_r($storedArr);
Advertisement
Answer
Your search value is one level below with index 0. In my code this is $subArr[0].
$arr = array( "1"=>(array("UID"=>"5321", array("1"=>"age", "2"=>"gender", "3"=>"location"))), "2"=>(array("UID"=>"3213", array("1"=>"location", "2"=>"gender"))), "3"=>(array("UID"=>"4444", array("1"=>"application", "2"=>"gender"))) ); $find = "gender"; $storedArr = []; foreach($arr as $id => $subArr){ if(in_array($find, $subArr[0])){ $storedArr[$id] = $subArr['UID']; } } var_export($storedArr); //array ( 1 => '5321', 2 => '3213', 3 => '4444', )