I have a php script that returns a JSON encoded array. It works correctly, but I need to check if it's empty. My problem lies in the fact that it's an array of arrays. How can I properly check for an empty array on the server side?
The PHP script (the important stuff)
$data = array();
foreach ($results = $db->getRows('SELECT * FROM users WHERE username !=?', [$username]) as $result) {
$name = $result['username'];
$lat = $result['lat'];
$lng = $result['lng'];
$distance = getDistance($user_lat, $user_lng, $lat, $lng);
if(withinRange($distance, $selected_distance, $selected_distance + 4)) {
//This is what's populated if data is available
$data[] = array('name'=>$name, 'lat'=>$lat, 'lng'=>$lng);
}
$response = array('data' => $data);
echo json_encode($response);
If data exists, it looks like this:
{"data":[{"name":"test_user2","lat":"35.932517","lng":"-89.905343"}]}
I have tried:
if(empty($response)
if(count($response) < 1)
but neither worked. Am I trying to access the wrong array? If so, how do I access the correct one to check for no data?
$responsewill never be empty, but$response['data']or$datamay be.