Adding array into another array (not array_push or array_merge) - php
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}
]
}
Related
How to display info on double array JSON (php)
I would like to know how to display the information below. Link (JSON): https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents What I wish to have: "teams": [ [ { "provider": "psn", "username": "Germania1992" }, { "provider": "psn", "username": "killzoneprofi" }, { "provider": "psn", "username": "ayozetf87" }, { "provider": "psn", "username": "Seith911" }, { "provider": "psn", "username": "domibreu92" } ], [ { "provider": "psn", "username": "Thejuankarboy" }, { "provider": "psn", "username": "Gamermad101" }, { "provider": "psn", "username": "Izdrap" }, { "provider": "psn", "username": "Guerra_sv" }, { "provider": "psn", "username": "TriX_FollOoW_YT" } ] ], I want to display the nicks of the different teams Example: Team 1 = Germania1992, killzoneprofi, ayozetf87, Seith911, domibreu92 Thanks
You will have to do a double foreach that for sure. <?php // Get the json of the team $team = team("https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents"); // Display the teams info foreach($team as $nb=>$data){ echo "Team $nb<br />"; foreach($data as $key=>$value){ echo "Provider: ".$value->provider."<br />"; echo "Username: ".$value->username."<br />"; } echo "<hr>"; } // Returns the team array of the json function team($jsonURL){ $content=file_get_contents($jsonURL); $data=json_decode($content); return $data->data->teams; } ?> You could increment the team number by 1 to avoid the first being 0 The above will display the below screenshot (You can format the output as you please)
$result = []; $counter = 0; dump($array = json_decode(file_get_contents('https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents'))); dump($t = array_column((array)$array, 'teams')); foreach ($t as $r) { foreach ($r as $p) { $counter++; foreach ($p as $value){ $result["team$counter"][] = $value->username; } } } var_dump($result);
Reading messy JSON using php
Hello I've got this really messy JSON that I would like to read data from but I can't think of a way to do it. It looks like this https://pastebin.com/55pWWgnK { "capacity_test": { "date": "2017-03-01", "status": "done", "PROPERTIES": { "fail": { "capacity_test": { . . . }, "def": [ { "drop_test": { "Properties": { "date": "2017-03-05", "status": "done" } }, "waves_test": { "date": "2018-03-06", "status": "done" } }, { "drop_test": { "Properties": null }, "waves_test": { "date": "2018-03-06", "status": "done" } }, { "drop_test": { "Properties": null }, "waves_test": { "date": "2018-03-06", "status": "done" } } ] }, "final_test": { "Properties": null } } } } PROPERTIES is not the same as Properties and this json array is recursive, it can have infinite amount of "capacity_test" inside one another. My issue is that I would like to check if there is a "status" key with no value in there somewhere. This JSON is quite messy, I tried to come up with a recursive php function such as: $this->myFunction($json, 'capacity_test'); public function myFunction($json, $step, $status = 0) { if ( is_object($json->$step) ) { if ( isset($json->$step->status) ) { if ( !empty($json->$step->status) ) { $status = 1; } } else { foreach ( $json->$step as $item ) { if ( is_object($item) ) { $status = $this->myFunction($item, $step, $status); if ( $status === 0 ) { exit(0); } } } } } return $status; } This doesnt seem to work for me
This piece of code should help you. Implement your own logic, when status is empty. <?php $jsonString = '{ "capacity_test": { "date": "2017-03-01", "status": "done", "PROPERTIES": { "fail": { "capacity_test": { "date": "2017-03-02", "status": "done", "PROPERTIES": { "boolean": false } }, "def": [ { "drop_test": { "Properties": { "date": "2017-03-05", "status": "done" } }, "waves_test": { "date": "2018-03-06", "status": "done" } }, { "drop_test": { "Properties": null }, "waves_test": { "date": "2018-03-06", "status": "done" } }, { "drop_test": { "Properties": null }, "waves_test": { "date": "2018-03-06", "status": "" } } ] }, "final_test": { "Properties": null } } } }'; $ar = json_decode($jsonString); function recArr($array) { foreach ($array as $k => $v) { if (is_array($v) || is_object($v)) { recArr($v); } else { if ($k == 'status' && $v == '') { // some empty logic echo $k; } } } } var_dump(recArr($ar));
You can use array_walk_recursive, like so: $array = json_decode($str, true); // Convert JSON string to array $hasEmptyStatus = false; try { array_walk_recursive($array, function($item, $key) { if ($key == "status" && $item == "") { throw new Exception; } }); } catch(Exception $e) { $hasEmptyStatus = true; } var_dump($hasEmptyStatus); This code converts your JSON object into array, then runs recorsively on your array until it finds a key named "status" that it's value is empty, then it throws an exception in order to break the recorsive function since we found it, so no need to keep running on the array. https://3v4l.org/LTa56
Access variable Array Data PHP
i have the following Array Structure from Facebook Graph API response. "data": [ { "actions": [ { "action_type": "comment", "value": "2" }, { "action_type": "offsite_conversion", "value": "1606" } ], "date_start": "2017-04-03", "date_stop": "2017-05-02" }, { "actions": [ { "action_type": "post", "value": "2" }, { "action_type": "post_reaction", "value": "33" }, { "action_type": "page_engagement", "value": "816" }, { "action_type": "post_engagement", "value": "807" }, { "action_type": "offsite_conversion", "value": "1523" } ], "date_start": "2017-04-03", "date_stop": "2017-05-02" }, ] The Number of values is flexible and i want to get the value from "offsite_conversion". Normally i would do it for example like that: data['data'][0]['actions']['1']['value'] But in that case this doesn't work because ['1'] is variable.
Use a loop and test the action type. foreach ($data['data'][0]['actions'] as $action) { if ($action['action_type'] == 'offsite_conversion') { $result = $data['value']; break; } }
because "offsite_conversions" is always the last If $data['data'][0]['actions'][LAST VALUE]['value'] is what you're looking for: Your idea of counting should work then: $actions = $data['data'][0]['actions']; $index = count($actions) - 1; $value = $actions[$index]['value'];
So not completely clear what are you trying to achieve, but in a simple way you can just iterate over your $data array: $needed_values = array(); foreach ($data['data'] as $item) { foreach ($item['actions'] as $action) { if ($action['action_type'] == 'offsite_conversion') { $needed_values[] = $action['value']; } } }
Barmar has the best approach if you don't know where it is, but it's much easier if you want the last one: $result = end($data['data'][0]['actions'])['value'];
Pretend $json holds the data from facebook <?php $data = json_decode($json); $conversions = 0; foreach ($data as $datum) { foreach ($datum['actions'] as $action) { if ($action['action_type'] === 'offsite_convserion') { $conversions += (int)$action['value']; break; } } }
search for duplicate classid in json array
I'm trying to search for duplicate classids in a json array and for each duplicate found, echo the dulicate id... This is just an example of the json file. I've tried a few things but failed - if I posted my code it wouldn't work with this sample code. It's a lot more complex as after I am check another json file for matching ids... and a bunch of other stuff. Thanks in advance. { "response": { "received": [ { "items": [ { "classid": "356464564", }, { "classid": "456456456", }, { "classid": "356464564", }, { "classid": "721248158", } ] , "time_created": 1440782791, }, { "items": [ { "classid": "845362344", }, { "classid": "2543634754", }, { "classid": "2543634754", }, { "classid": "5967856788", } ] , "time_created": 1440456791, } } }
This can do what you're looking for: <?php $array = json_decode('{ "response": { "received": [ { "items": [ { "classid": "356464564" }, { "classid": "456456456" }, { "classid": "356464564" }, { "classid": "721248158" } ] , "time_created": 1440782791 } ] } }', true); $cleanArray = array(); foreach($array['response']['received'][0]['items'] as $classid) { if(in_array($classid['classid'], $cleanArray)) echo "Duplicate found: ".$classid['classid'].'<br>'; else $cleanArray[] = $classid['classid']; } ?>
Try this, it makes the array smaller as matches are found, thus making the algorithm more efficient. $arr = json_decode($json , true); $items_array = array_column($arr['response']['items'], 'classid'); foreach ($items_array as $k => $val) { foreach ($items_array as $k2 => $val2) { if ($k2 != $k) { if ($val == $val2) { unset($items_array[$k]); if (isset($final[$val])) { $final[$val]++; } else { $final[$val] = 1; } } } } } var_dump($final); //will show you your duplicates
Replace array keys
i have an array like this JSON Example .I'm trying to replace the numeric key ..."conn":{"1":{"... with string key such as "node". FOR EXAMPLE i want to create this: { "Level": [ { "main": "472321514", "main_lat": "39.1057579", "main_lon": "26.5451331", "conn": { "node": { "id": "599416249", "coords": { "lat": "39.1055889", "lon": "26.5452403" }, "distance": 0.0209442235276 },... Before json encoding my script is: foreach ($ways as $w){ $nd=$w->nd; foreach ($nd as $w2){ $nodes_Array[]=(string)$w2->attributes()->ref; } for($ww=0;$ww<count($nodes_Array);$ww++){ $nodes_Array2[$bb]['main'] = $nodes_Array[$ww]; for($gg=0;$gg<count($node_content);$gg++){ if($node_content[$gg]['id']==$nodes_Array2[$bb]['main']){ $nodes_Array2[$bb]['main_lat']= $node_content[$gg]['lat']; $nodes_Array2[$bb]['main_lon']= $node_content[$gg]['lon']; } } $nodes_Array2[$bb]['conn'] = array_diff($nodes_Array, array($nodes_Array[$ww])); for($cc=0;$cc<count($nodes_Array2[$bb]['conn']);$cc++){ for($gg=0;$gg<count($node_content);$gg++){ if($node_content[$gg]['id']==$nodes_Array2[$bb]['conn'][$cc]){ $nodes_Array2[$bb]['conn'][$cc]=Array( 'id'=>$node_content[$gg]['id'], 'coords'=>Array( 'lat'=>$node_content[$gg]['lat'], 'lon'=>$node_content[$gg]['lon'], ), 'distance'=>distance($nodes_Array2[$bb]['main_lat'],$nodes_Array2[$bb]['main_lon'],$node_content[$gg]['lat'],$node_content[$gg]['lon'],"K"), ); } } } $bb++; } unset($nodes_Array); }