I have been trying to loop through an array. Inserting the values in two separate arrays $location and $name. But the $name array prints the index values as well, the $location works fine. Here's a sample of code
$j = 0;
foreach( $entities->results as $key => $value ) {
if( stristr($value->vicinity, $key_stroke) ) {
$location[$j]['place_id'] = $value->place_id;
$location[$j]['vicinity'] = $value->vicinity;
}
if( stristr($value->name, $key_stroke) ) {
$name[$j]['place_id'] = $value->place_id;
$name[$j]['name'] = $value->name;
}
$j++; }
Here is the json output
{
"locations": [
{
"place_id": "ChIJRQqyYRFZWjcRmxKd0esyj-k",
"vicinity": "GS Road, Christian Basti, Guwahati"
},
{
"place_id": "ChIJG5IvxhNZWjcRlkMD6lCJ64c",
"vicinity": "GS Road, Ananda Nagar, Christian Basti, Guwahati"
},
{
"place_id": "ChIJxQp72BZZWjcR98oQbFrdTII",
"vicinity": "GS Road, Christian Basti, Guwahati"
},
{
"place_id": "ChIJm5eeJBBZWjcRksI_VY9u1Qo",
"vicinity": "Zoo Road, Sundarpur, Guwahati"
}
],
"names": {
"1": {
"place_id": "ChIJG5IvxhNZWjcRlkMD6lCJ64c",
"name": "Ayush Medico's"
},
"2": {
"place_id": "ChIJxQp72BZZWjcR98oQbFrdTII",
"name": "Premananda Medico's"
},
"3": {
"place_id": "ChIJm5eeJBBZWjcRksI_VY9u1Qo",
"name": "Asaan Medicos"
}
}
}
Tried everything. What could be the problem??
if( stristr($value->name, $key_stroke) )seem to return false on the first iteration, which makes it omit the 0 index. When converting to json, that will make the "array" into an object instead since arrays can't have any gaps in the indexes.