Question

uI don't understand why this wont work? depending on where a value is in a multidimensional array I am trying to target another value from the same location within the same multidimensional array. So I am trying to get this code to pull 23 as its the 2nd value in the "Age" array.

$test = array(
"Name"=>array("Peter","Louis","Megan","Glenn"),
"Age"=>array("21","23","27"),
"Location"=>array("Cardiff","London","Glasgow")
);


if (in_array('Louis', $test["Name"][$n])) {
echo $test["Age"][$n];
}
Was it helpful?

Solution 2

How about array_search to get the index?

$test = array(
  "Name"=>array("Peter","Louis","Megan","Glenn"),
  "Age"=>array("21","23","27"),
  "Location"=>array("Cardiff","London","Glasgow")
);

$index = array_search("Louis", $test["Name"]);
if ($index !== FALSE) {
    echo $test["Age"][$index];
}
else {
    echo "Not Found";
} 

OTHER TIPS

$test["Name"][$n]

is not an array, it's a string, so in_array always returns false

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top