I a code like this
for($ctr=0; $ctr < 4; $ctr++) {
$messages["message"] = $ctr;
array_push($response["messages"], $messages);
}
I want to get the message
that contains the value 2
. How do I do that in PHP?
you have to use array search on this case.
for($ctr=0; $ctr < 4; $ctr++) {
//$messages["message"] = $ctr;
array_push($response, $ctr);
}
echo array_search('2', $response);
array search give you the index of the array which value is 2.
for($ctr=0; $ctr < 4; $ctr++) {
$messages["message"] = $ctr;
array_push($response["messages"], $messages);
}
function mysearch($val) {
global $response;
for ($i = 0; $i < count($response["messages"]); $i++) {
if ($response["messages"][$i]["message"] === $val) {
return $i;
}
}
return -1;
}
echo mysearch(2); // will print the index, or -1 if not found