I am quite new to PHP and I have some problems with pushing arrays to another. For more details, I want to show a part of my code:
This is a sample response data:
edit: I have more than one "sentenceArray"
{
"data": [
{
"sentenceArray": [
{
"path": {
"type": "walk",
"nodes": [
{
"lat": 39.952614,
"lng": 32.854491
},
{
"lat": 39.952083,
"lng": 32.857761
}
]
}
},
{
"path": {
"type": "bus",
"nodes": [
{
"lat": 39.952418,
"lng": 32.85727
},
{
"lat": 39.952797,
"lng": 32.856825
},
{
"lat": 39.953102,
"lng": 32.856438
}
]
}
},
{
"path": {
"type": "bus",
"nodes": [
{
"lat": 39.964951,
"lng": 32.841305
},
{
"lat": 39.964785,
"lng": 32.841514
}
]
}
}
]
},
{
"sentenceArray": [
{
"path": {
"type": "walk",
"nodes": [
{
"lat": 39.952614,
"lng": 32.854491
},
{
"lat": 39.952083,
"lng": 32.857761
}
]
}
},
{
"path": {
"type": "bus",
"nodes": [
{
"lat": 39.952418,
"lng": 32.85727
},
{
"lat": 39.952797,
"lng": 32.856825
},
{
"lat": 39.953102,
"lng": 32.856438
}
]
}
},
{
"path": {
"type": "bus",
"nodes": [
{
"lat": 39.964951,
"lng": 32.841305
},
{
"lat": 39.964785,
"lng": 32.841514
}
]
}
}
]
}
]
}
Here is what I did so far:
for($j = 0; $j < $l; $j++) {
if($sentenceArray[$j]['path']['type'] == "bus") {
if(isset($sentenceArray[$j]['path']['nodes'])){
$busCount++;
$nodes = $sentenceArray[$j]['path']['nodes'];
for($k = 0, $c = count($nodes); $k < $c; $k++) {
$latlng = $nodes[$k];
array_push($arrBus,$latlng);
}
}
}
}
At first, I tried to push arrays into one array but my code merged it all in one array, not two separated arrays in one array. By the way, real data is much bigger than this and generated dynamically. So bus array count can be two or more. It is little bit confusing for me as a newbie PHP developer.
Let me show you what is my goal here:
var lineCoordinates = {
"0": [
{
"lat": 39.952418,
"lng": 32.85727
},
{
"lat": 39.952797,
"lng": 32.856825
},
{
"lat": 39.953102,
"lng": 32.856438
}
],
"1": [
{
"lat": 39.964951,
"lng": 32.841305
},
{
"lat": 39.964785,
"lng": 32.841514
}
]
}
This sample output will work for me well.
I spend many hours to do this by myself and I also read my posts on web but couldn't find the solution. Please, do not judge me. As I said, I am newbie. Thanks in advance.
By the way, I use json_decode() when I pull data first and I use json_encode() at the end. But I didn't need to mention it.
Iterate the data subarray, then iterate the sentenceArray, then if the ['path']['type'] value is bus and the ['path']['nodes'] subarray exists, then store that subarray as a separate group in the result array.
Code: (Demo)
$array = json_decode($json, true);
foreach ($array['data'] as $datas) {
foreach ($datas['sentenceArray'] as $subitem) {
if ($subitem['path']['type'] == 'bus' && isset($subitem['path']['nodes'])) {
$result[] = $subitem['path']['nodes'];
}
}
}
echo json_encode($result);
Output:
[[{"lat":39.952418,"lng":32.85727},{"lat":39.952797,"lng":32.856825},{"lat":39.953102,"lng":32.856438}],[{"lat":39.964951,"lng":32.841305},{"lat":39.964785,"lng":32.841514}],[{"lat":39.952418,"lng":32.85727},{"lat":39.952797,"lng":32.856825},{"lat":39.953102,"lng":32.856438}],[{"lat":39.964951,"lng":32.841305},{"lat":39.964785,"lng":32.841514}]]
This also work on your full json input string to provide:
[[{"lat":39.952418,"lng":32.85727},{"lat":39.952797,"lng":32.856825},{"lat":39.953102,"lng":32.856438},{"lat":39.953273,"lng":32.856239},{"lat":39.953713,"lng":32.855659},{"lat":39.954017,"lng":32.855296},{"lat":39.95498,"lng":32.854037},{"lat":39.955138,"lng":32.853829},{"lat":39.955317,"lng":32.853592},{"lat":39.955448,"lng":32.853421},{"lat":39.955638,"lng":32.853161},{"lat":39.955744,"lng":32.85303},{"lat":39.956395,"lng":32.852171},{"lat":39.957293,"lng":32.851087},{"lat":39.957668,"lng":32.850631},{"lat":39.957715,"lng":32.850579},{"lat":39.957854,"lng":32.850421},{"lat":39.957963,"lng":32.850286},{"lat":39.959664,"lng":32.848184},{"lat":39.960768,"lng":32.846954},{"lat":39.961228,"lng":32.846328},{"lat":39.961566,"lng":32.845869},{"lat":39.962472,"lng":32.844696},{"lat":39.963265,"lng":32.843721},{"lat":39.963397,"lng":32.84357},{"lat":39.963511,"lng":32.843414},{"lat":39.963628,"lng":32.843271},{"lat":39.963804,"lng":32.843048},{"lat":39.963898,"lng":32.842929},{"lat":39.965452,"lng":32.840996}],[{"lat":39.964951,"lng":32.841305},{"lat":39.964785,"lng":32.841514},{"lat":39.964555,"lng":32.841818},{"lat":39.963827,"lng":32.842688},{"lat":39.963622,"lng":32.842942},{"lat":39.963542,"lng":32.843037},{"lat":39.96343,"lng":32.843185},{"lat":39.963304,"lng":32.843335},{"lat":39.962502,"lng":32.844327},{"lat":39.961437,"lng":32.845665},{"lat":39.961162,"lng":32.84601},{"lat":39.961054,"lng":32.84613},{"lat":39.960679,"lng":32.846639},{"lat":39.960299,"lng":32.84711},{"lat":39.959572,"lng":32.848031},{"lat":39.959476,"lng":32.848146},{"lat":39.957918,"lng":32.85016},{"lat":39.957835,"lng":32.850267},{"lat":39.957696,"lng":32.850436},{"lat":39.957636,"lng":32.850509},{"lat":39.957244,"lng":32.850993},{"lat":39.95635,"lng":32.852096},{"lat":39.955658,"lng":32.852909},{"lat":39.954229,"lng":32.854682},{"lat":39.953853,"lng":32.855119},{"lat":39.953422,"lng":32.85567},{"lat":39.953096,"lng":32.856053},{"lat":39.952949,"lng":32.856252},{"lat":39.952406,"lng":32.856964},{"lat":39.952278,"lng":32.857091},{"lat":39.951554,"lng":32.857753},{"lat":39.951114,"lng":32.857934},{"lat":39.950962,"lng":32.857907},{"lat":39.950826,"lng":32.857881},{"lat":39.950556,"lng":32.857782},{"lat":39.94987,"lng":32.857458},{"lat":39.948744,"lng":32.856459},{"lat":39.948303,"lng":32.856178},{"lat":39.947866,"lng":32.855815},{"lat":39.947623,"lng":32.855625},{"lat":39.947283,"lng":32.855428},{"lat":39.946974,"lng":32.855249},{"lat":39.946209,"lng":32.854963},{"lat":39.945342,"lng":32.854679},{"lat":39.945094,"lng":32.85462},{"lat":39.944673,"lng":32.854562},{"lat":39.944124,"lng":32.854557},{"lat":39.943877,"lng":32.854564},{"lat":39.943572,"lng":32.854576},{"lat":39.943336,"lng":32.854503},{"lat":39.943041,"lng":32.854494},{"lat":39.942475,"lng":32.854465},{"lat":39.942371,"lng":32.85444},{"lat":39.942294,"lng":32.854403},{"lat":39.94201,"lng":32.854252},{"lat":39.941672,"lng":32.854101},{"lat":39.941495,"lng":32.854233},{"lat":39.940512,"lng":32.854229}],[{"lat":39.952418,"lng":32.85727},{"lat":39.952797,"lng":32.856825},{"lat":39.953102,"lng":32.856438},{"lat":39.953273,"lng":32.856239},{"lat":39.953713,"lng":32.855659},{"lat":39.954017,"lng":32.855296},{"lat":39.95498,"lng":32.854037},{"lat":39.955138,"lng":32.853829},{"lat":39.955317,"lng":32.853592},{"lat":39.955448,"lng":32.853421},{"lat":39.955638,"lng":32.853161},{"lat":39.955744,"lng":32.85303},{"lat":39.956395,"lng":32.852171},{"lat":39.957293,"lng":32.851087},{"lat":39.957668,"lng":32.850631},{"lat":39.957715,"lng":32.850579},{"lat":39.957854,"lng":32.850421},{"lat":39.957963,"lng":32.850286},{"lat":39.959664,"lng":32.848184},{"lat":39.960768,"lng":32.846954},{"lat":39.961228,"lng":32.846328},{"lat":39.961566,"lng":32.845869},{"lat":39.962472,"lng":32.844696},{"lat":39.963265,"lng":32.843721},{"lat":39.963397,"lng":32.84357},{"lat":39.963511,"lng":32.843414},{"lat":39.963628,"lng":32.843271},{"lat":39.963804,"lng":32.843048},{"lat":39.963898,"lng":32.842929},{"lat":39.965452,"lng":32.840996}],[{"lat":39.964951,"lng":32.841305},{"lat":39.964785,"lng":32.841514},{"lat":39.964555,"lng":32.841818},{"lat":39.963827,"lng":32.842688},{"lat":39.963622,"lng":32.842942},{"lat":39.963542,"lng":32.843037},{"lat":39.96343,"lng":32.843185},{"lat":39.963304,"lng":32.843335},{"lat":39.962502,"lng":32.844327},{"lat":39.961437,"lng":32.845665},{"lat":39.961162,"lng":32.84601},{"lat":39.961054,"lng":32.84613},{"lat":39.960679,"lng":32.846639},{"lat":39.960299,"lng":32.84711},{"lat":39.959572,"lng":32.848031},{"lat":39.959476,"lng":32.848146},{"lat":39.957918,"lng":32.85016},{"lat":39.957835,"lng":32.850267},{"lat":39.957696,"lng":32.850436},{"lat":39.957636,"lng":32.850509},{"lat":39.957244,"lng":32.850993},{"lat":39.95635,"lng":32.852096},{"lat":39.955658,"lng":32.852909},{"lat":39.954229,"lng":32.854682},{"lat":39.953422,"lng":32.85567},{"lat":39.953096,"lng":32.856053},{"lat":39.952949,"lng":32.856252},{"lat":39.952406,"lng":32.856964},{"lat":39.952278,"lng":32.857091},{"lat":39.951554,"lng":32.857753},{"lat":39.951114,"lng":32.857934},{"lat":39.950962,"lng":32.857907},{"lat":39.950826,"lng":32.857881},{"lat":39.950556,"lng":32.857782},{"lat":39.94987,"lng":32.857458},{"lat":39.948744,"lng":32.856459},{"lat":39.948303,"lng":32.856178},{"lat":39.947866,"lng":32.855815},{"lat":39.947623,"lng":32.855625},{"lat":39.947283,"lng":32.855428},{"lat":39.947283,"lng":32.855428},{"lat":39.946974,"lng":32.855249},{"lat":39.946209,"lng":32.854963},{"lat":39.945342,"lng":32.854679},{"lat":39.945094,"lng":32.85462},{"lat":39.944673,"lng":32.854562},{"lat":39.944124,"lng":32.854557},{"lat":39.943877,"lng":32.854564},{"lat":39.943572,"lng":32.854576},{"lat":39.943336,"lng":32.854503},{"lat":39.943041,"lng":32.854494},{"lat":39.942475,"lng":32.854465},{"lat":39.942371,"lng":32.85444},{"lat":39.942294,"lng":32.854403},{"lat":39.94201,"lng":32.854252},{"lat":39.941672,"lng":32.854101},{"lat":39.941495,"lng":32.854233},{"lat":39.940512,"lng":32.854229}],[{"lat":39.952418,"lng":32.85727},{"lat":39.952797,"lng":32.856825},{"lat":39.953102,"lng":32.856438},{"lat":39.953273,"lng":32.856239},{"lat":39.953713,"lng":32.855659},{"lat":39.954017,"lng":32.855296},{"lat":39.95498,"lng":32.854037},{"lat":39.955138,"lng":32.853829},{"lat":39.955317,"lng":32.853592},{"lat":39.955448,"lng":32.853421},{"lat":39.955638,"lng":32.853161},{"lat":39.955744,"lng":32.85303},{"lat":39.956395,"lng":32.852171},{"lat":39.957293,"lng":32.851087},{"lat":39.957668,"lng":32.850631},{"lat":39.957715,"lng":32.850579},{"lat":39.957854,"lng":32.850421},{"lat":39.957963,"lng":32.850286},{"lat":39.959664,"lng":32.848184},{"lat":39.960768,"lng":32.846954},{"lat":39.961228,"lng":32.846328},{"lat":39.961566,"lng":32.845869},{"lat":39.962472,"lng":32.844696},{"lat":39.963265,"lng":32.843721},{"lat":39.963397,"lng":32.84357},{"lat":39.963511,"lng":32.843414},{"lat":39.963628,"lng":32.843271},{"lat":39.963804,"lng":32.843048},{"lat":39.963898,"lng":32.842929},{"lat":39.965452,"lng":32.840996}],[{"lat":39.964951,"lng":32.841305},{"lat":39.964785,"lng":32.841514},{"lat":39.964555,"lng":32.841818},{"lat":39.963827,"lng":32.842688},{"lat":39.963622,"lng":32.842942},{"lat":39.963542,"lng":32.843037},{"lat":39.96343,"lng":32.843185},{"lat":39.963304,"lng":32.843335},{"lat":39.962502,"lng":32.844327},{"lat":39.961437,"lng":32.845665},{"lat":39.961162,"lng":32.84601},{"lat":39.961054,"lng":32.84613},{"lat":39.960679,"lng":32.846639},{"lat":39.960299,"lng":32.84711},{"lat":39.959572,"lng":32.848031},{"lat":39.959476,"lng":32.848146},{"lat":39.957918,"lng":32.85016},{"lat":39.957835,"lng":32.850267},{"lat":39.957696,"lng":32.850436},{"lat":39.957636,"lng":32.850509},{"lat":39.957244,"lng":32.850993},{"lat":39.95635,"lng":32.852096},{"lat":39.955658,"lng":32.852909},{"lat":39.954229,"lng":32.854682},{"lat":39.953853,"lng":32.855119},{"lat":39.953422,"lng":32.85567},{"lat":39.953096,"lng":32.856053},{"lat":39.952949,"lng":32.856252},{"lat":39.952406,"lng":32.856964},{"lat":39.952278,"lng":32.857091},{"lat":39.951554,"lng":32.857753},{"lat":39.951114,"lng":32.857934},{"lat":39.950962,"lng":32.857907},{"lat":39.950826,"lng":32.857881},{"lat":39.950556,"lng":32.857782},{"lat":39.94987,"lng":32.857458},{"lat":39.948744,"lng":32.856459},{"lat":39.948303,"lng":32.856178},{"lat":39.947866,"lng":32.855815},{"lat":39.947623,"lng":32.855625},{"lat":39.947283,"lng":32.855428},{"lat":39.946974,"lng":32.855249},{"lat":39.946209,"lng":32.854963},{"lat":39.945342,"lng":32.854679},{"lat":39.945094,"lng":32.85462},{"lat":39.944673,"lng":32.854562},{"lat":39.944124,"lng":32.854557},{"lat":39.943877,"lng":32.854564},{"lat":39.943572,"lng":32.854576},{"lat":39.943336,"lng":32.854503},{"lat":39.943041,"lng":32.854494},{"lat":39.942475,"lng":32.854465},{"lat":39.942371,"lng":32.85444},{"lat":39.942294,"lng":32.854403},{"lat":39.94201,"lng":32.854252},{"lat":39.941672,"lng":32.854101},{"lat":39.941495,"lng":32.854233},{"lat":39.940512,"lng":32.854229}],[{"lat":39.952418,"lng":32.85727},{"lat":39.952797,"lng":32.856825},{"lat":39.953102,"lng":32.856438},{"lat":39.953273,"lng":32.856239},{"lat":39.953713,"lng":32.855659},{"lat":39.954017,"lng":32.855296},{"lat":39.95498,"lng":32.854037},{"lat":39.955138,"lng":32.853829},{"lat":39.955317,"lng":32.853592},{"lat":39.955448,"lng":32.853421},{"lat":39.955638,"lng":32.853161},{"lat":39.955744,"lng":32.85303},{"lat":39.956395,"lng":32.852171},{"lat":39.957293,"lng":32.851087},{"lat":39.957668,"lng":32.850631},{"lat":39.957715,"lng":32.850579},{"lat":39.957854,"lng":32.850421},{"lat":39.957963,"lng":32.850286},{"lat":39.959664,"lng":32.848184},{"lat":39.960768,"lng":32.846954},{"lat":39.961228,"lng":32.846328},{"lat":39.961566,"lng":32.845869},{"lat":39.962472,"lng":32.844696},{"lat":39.963265,"lng":32.843721},{"lat":39.963397,"lng":32.84357},{"lat":39.963511,"lng":32.843414},{"lat":39.963628,"lng":32.843271},{"lat":39.963804,"lng":32.843048},{"lat":39.963898,"lng":32.842929},{"lat":39.965452,"lng":32.840996}],[{"lat":39.964951,"lng":32.841305},{"lat":39.964785,"lng":32.841514},{"lat":39.964555,"lng":32.841818},{"lat":39.963827,"lng":32.842688},{"lat":39.963622,"lng":32.842942},{"lat":39.963542,"lng":32.843037},{"lat":39.96343,"lng":32.843185},{"lat":39.963304,"lng":32.843335},{"lat":39.962502,"lng":32.844327},{"lat":39.961437,"lng":32.845665},{"lat":39.961162,"lng":32.84601},{"lat":39.961054,"lng":32.84613},{"lat":39.960679,"lng":32.846639},{"lat":39.960299,"lng":32.84711},{"lat":39.959572,"lng":32.848031},{"lat":39.959476,"lng":32.848146},{"lat":39.957918,"lng":32.85016},{"lat":39.957835,"lng":32.850267},{"lat":39.957696,"lng":32.850436},{"lat":39.957636,"lng":32.850509},{"lat":39.957244,"lng":32.850993},{"lat":39.95635,"lng":32.852096},{"lat":39.955658,"lng":32.852909},{"lat":39.954229,"lng":32.854682},{"lat":39.953853,"lng":32.855119},{"lat":39.953422,"lng":32.85567},{"lat":39.953096,"lng":32.856053},{"lat":39.952949,"lng":32.856252},{"lat":39.952406,"lng":32.856964},{"lat":39.952278,"lng":32.857091},{"lat":39.951554,"lng":32.857753},{"lat":39.951114,"lng":32.857934},{"lat":39.950962,"lng":32.857907},{"lat":39.950826,"lng":32.857881},{"lat":39.950556,"lng":32.857782},{"lat":39.94987,"lng":32.857458},{"lat":39.948744,"lng":32.856459},{"lat":39.948303,"lng":32.856178},{"lat":39.947866,"lng":32.855815},{"lat":39.947623,"lng":32.855625},{"lat":39.947283,"lng":32.855428},{"lat":39.946974,"lng":32.855249},{"lat":39.946209,"lng":32.854963},{"lat":39.945342,"lng":32.854679},{"lat":39.945094,"lng":32.85462},{"lat":39.944673,"lng":32.854562},{"lat":39.944124,"lng":32.854557},{"lat":39.943877,"lng":32.854564},{"lat":39.943572,"lng":32.854576},{"lat":39.943336,"lng":32.854503},{"lat":39.943041,"lng":32.854494},{"lat":39.942475,"lng":32.854465},{"lat":39.942371,"lng":32.85444},{"lat":39.942294,"lng":32.854403},{"lat":39.94201,"lng":32.854252},{"lat":39.941672,"lng":32.854101},{"lat":39.941495,"lng":32.854233},{"lat":39.940512,"lng":32.854229}],[{"lat":39.952418,"lng":32.85727},{"lat":39.952797,"lng":32.856825},{"lat":39.953102,"lng":32.856438},{"lat":39.953273,"lng":32.856239},{"lat":39.953713,"lng":32.855659},{"lat":39.954017,"lng":32.855296},{"lat":39.95498,"lng":32.854037},{"lat":39.955138,"lng":32.853829},{"lat":39.955317,"lng":32.853592},{"lat":39.955448,"lng":32.853421},{"lat":39.955638,"lng":32.853161},{"lat":39.955744,"lng":32.85303},{"lat":39.956395,"lng":32.852171},{"lat":39.957293,"lng":32.851087},{"lat":39.957668,"lng":32.850631},{"lat":39.957715,"lng":32.850579},{"lat":39.957854,"lng":32.850421},{"lat":39.957963,"lng":32.850286},{"lat":39.959664,"lng":32.848184},{"lat":39.960768,"lng":32.846954},{"lat":39.961228,"lng":32.846328},{"lat":39.961566,"lng":32.845869},{"lat":39.962472,"lng":32.844696},{"lat":39.963265,"lng":32.843721},{"lat":39.963397,"lng":32.84357},{"lat":39.963511,"lng":32.843414},{"lat":39.963628,"lng":32.843271},{"lat":39.963804,"lng":32.843048},{"lat":39.963898,"lng":32.842929},{"lat":39.965452,"lng":32.840996}],[{"lat":39.964951,"lng":32.841305},{"lat":39.964785,"lng":32.841514},{"lat":39.964555,"lng":32.841818},{"lat":39.963827,"lng":32.842688},{"lat":39.963622,"lng":32.842942},{"lat":39.963542,"lng":32.843037},{"lat":39.96343,"lng":32.843185},{"lat":39.963304,"lng":32.843335},{"lat":39.962502,"lng":32.844327},{"lat":39.961437,"lng":32.845665},{"lat":39.961162,"lng":32.84601},{"lat":39.961054,"lng":32.84613},{"lat":39.960679,"lng":32.846639},{"lat":39.960299,"lng":32.84711},{"lat":39.959572,"lng":32.848031},{"lat":39.959476,"lng":32.848146},{"lat":39.957918,"lng":32.85016},{"lat":39.957835,"lng":32.850267},{"lat":39.957696,"lng":32.850436},{"lat":39.957636,"lng":32.850509},{"lat":39.957244,"lng":32.850993},{"lat":39.95635,"lng":32.852096},{"lat":39.955658,"lng":32.852909},{"lat":39.954229,"lng":32.854682},{"lat":39.953422,"lng":32.85567},{"lat":39.953096,"lng":32.856053},{"lat":39.952949,"lng":32.856252},{"lat":39.952406,"lng":32.856964},{"lat":39.952278,"lng":32.857091},{"lat":39.951554,"lng":32.857753},{"lat":39.951114,"lng":32.857934},{"lat":39.950962,"lng":32.857907},{"lat":39.950826,"lng":32.857881},{"lat":39.950556,"lng":32.857782},{"lat":39.94987,"lng":32.857458},{"lat":39.948744,"lng":32.856459},{"lat":39.948303,"lng":32.856178},{"lat":39.947866,"lng":32.855815},{"lat":39.947623,"lng":32.855625},{"lat":39.947283,"lng":32.855428},{"lat":39.947283,"lng":32.855428},{"lat":39.946974,"lng":32.855249},{"lat":39.946209,"lng":32.854963},{"lat":39.945342,"lng":32.854679},{"lat":39.945094,"lng":32.85462},{"lat":39.944673,"lng":32.854562},{"lat":39.944124,"lng":32.854557},{"lat":39.943877,"lng":32.854564},{"lat":39.943572,"lng":32.854576},{"lat":39.943336,"lng":32.854503},{"lat":39.943041,"lng":32.854494},{"lat":39.942475,"lng":32.854465},{"lat":39.942371,"lng":32.85444},{"lat":39.942294,"lng":32.854403},{"lat":39.94201,"lng":32.854252},{"lat":39.941672,"lng":32.854101},{"lat":39.941495,"lng":32.854233},{"lat":39.940512,"lng":32.854229}]]
Question Extension
To filter the results by user-provided value seq:
if (!isset($_GET['seq']) || !ctype_digit($_GET['seq'])){
echo "Missing/Invalid SEQ value.";
} else {
$array = json_decode($json, true);
$result = [];
$selected_seq = (int)$_GET['seq'];
foreach ($array['data'] as $seq => $datas) {
if ($seq === $selected_seq) {
foreach ($datas['sentenceArray'] as $subitem) {
if ($subitem['path']['type'] == 'bus' && isset($subitem['path']['nodes'])) {
$result[] = $subitem['path']['nodes'];
}
}
}
}
echo json_encode($result);
}
Please follow below code:
for($j = 0; $j < $l; $j++) {
if($sentenceArray[$j]['path']['type'] == "bus") {
$childArr = array();
if(isset($sentenceArray[$j]['path']['nodes'])){
foreach($sentenceArray[$j]['path']['nodes'] as $nodes)
{
array_push($childArr, $nodes);
}
}
$arrBus[$j] = $childArr;
}
}
$arrBus = array_values($arrBus);
$arrBus = json_encode($arrBus);
Output as below:
[
[
{
"lat":39.952418,
"lng":32.85727
},
{
"lat":39.952797,
"lng":32.856825
},
{
"lat":39.953102,
"lng":32.856438
}
],
[
{
"lat":39.964951,
"lng":32.841305
},
{
"lat":39.964785,
"lng":32.841514
}
]
]
This code will produce the output that you desire ($json is assumed to contain the JSON from your first snippet):
$arr = json_decode($json);
$buslines = new stdClass();
$routenum = 0;
foreach ($arr->data as $data) {
foreach ($data->sentenceArray as $route) {
if (!isset($route->path, $route->path->type, $route->path->nodes)) continue;
if ($route->path->type != 'bus') continue;
$buslines->{$routenum} = array();
foreach ($route->path->nodes as $node) {
$buslines->{$routenum}[] = $node;
}
$routenum++;
}
}
echo json_encode($buslines);
Output:
{
"0": [
{"lat":39.952418,"lng":32.85727},
{"lat":39.952797,"lng":32.856825},
{"lat":39.953102,"lng":32.856438}
],
"1": [
{"lat":39.964951,"lng":32.841305},
{"lat":39.964785,"lng":32.841514}
]
}