I have
$userPostsInt = array("22", "45", "56");
I am receiving via ajax an id:
$post_id = $_POST['id'];
Then on front end I click and send an ID and I need to check if:
1. the clicked ID is in the array
2. if the array count is <= 2 if not do something
So I try:
$totSaved = array();
$userPostsInt = array("22", "45", "56");
$count = count($userPostsInt);
if($count<2) {
foreach($userPostsInt as $key=>$post){
if( $post == $post_id ) {
foreach($userPostsInt as $idInt){
array_push($totSaved, $idInt);
}
echo json_encode($count);
}
}
} else {
echo json_encode($count);
}
Then on ajax success I do:
success: function(data) {
var received = true;
if(received) {
if(data < 2) {
do_something
} else {
do_something
}
} else {
do_something else
}
}
How can I send 2 variable on echo json_encode($count); in order to do a double check for "is ID in the array? Is the array less than 2?" or is it there another way I'm missing?
in_array()instead offoreach()to check ajax given values lies in your array or notecho json_encode(array('found' => $found, 'count' => $count));and you will get an object in your success code and you can access the values viadata.foundanddata.count(note you need to specifydataType: jsonor parse the data)dataType: "json". But I see you have a working solution now anyway.