show a value depending on location of another value in a multidimensional array?

StackOverflow https://stackoverflow.com/questions/21716240

  •  10-10-2022
  •  | 
  •  

문제

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];
}
도움이 되었습니까?

해결책 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";
} 

다른 팁

$test["Name"][$n]

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top