Replace array keys - php

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);
}

Related

Adding array into another array (not array_push or array_merge)

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}
]
}

PHP get array items based on string mask

i have an array with a bunch of records like in:
{
"ID": "38424",
"heading": "Nylies soek nuwe hoof",
"typeID": "1",
"datein": "2016-09-26 12:14:16",
"edited_datein": null,
"publishDate": "2016-09-23 00:00:00",
"category": {
"ID": "1",
"heading": "News",
"typeID": "3",
"datein": "2016-09-26 11:50:06",
"edited_datein": null,
"url": "news"
},
"authorID": "592",
"tags": "skool,school,hoof,headmaster,etienne burger"
}
i have another array with "columns" i want the records to be "filtered" by
{
"ID",
"heading",
"datein",
"category|heading",
"category|url"
}
i would like the result to create a multidimensional array based on the column items:
{
"ID": "38424",
"heading": "Nylies soek nuwe hoof",
"datein": "2016-09-26 12:14:16",
"category": {
"heading": "News",
"url": "news"
}
}
how do i achieve this? i'm totally stuck on this now :( busy trying a hack of array_combine now but i dont hold much hope it would work out
so after being stuck on this for many hours.. and posting it here. i found a solution
$new_list = array();
foreach ($n as $record) {
$new_list[] = filter_columns($columns, $record);
}
and the function:
function filter_columns($columns, $record, $pre="") {
$return = array();
foreach ($record as $key => $value) { // loop through the fields in the record
$str = $pre.$key; // create a string of the current key
if (in_array($str,$columns)){
$return[$key] = $value;
}
if (is_array($value)){
$return[$key] = filter_columns($columns, $value,$key."|"); // if the value is an array recall the function but prepend the current key| to the key mask
}
}
return $return;
}

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

How to place the json objects into array usin php?

this is my php code, to obtain json format data
if($status==1)
{
$post_id=$json_object['post_id'];
$get_postid=mysqli_query($con,"select * from User_Post where post_id='$post_id'");
if(mysqli_num_rows($get_postid)==0)
{
//For failure status if session id is wrong.
http_response_code(500);
echo json_encode(array("error_code"=>"500","error_message"=>"Sorry, post id does not exists."));
}
else
{
foreach($field_check as $values)
{
if($values=='id')
{
$row_array = array();
while ($row = $get_postid->fetch_array())
{
$row_array['id']=$row['post_id'];
$row_array['image_urls']=explode(',',$row['post_image_url']);
$storetag= explode(',',$row['Post_tagged_id']);
$has_liked="false";
$has_commented="false";
}
}
elseif($values=='tagged_users')
{
while ($row = $get_postid->fetch_array())
{
$storetag= explode(',',$row['Post_tagged_id']);
$has_liked="false";
$has_commented="false";
}
for($i=0;$i<count($storetag);$i++)
{
$user=mysqli_query($con,"select user_id, profile_image_url from Wheel_User where user_id='$storetag[$i]'");
if(mysqli_num_rows($user)==0)
{
//For failure status if session id is wrong.
http_response_code(500);
echo json_encode(array("error_code"=>"500","error_message"=>"Sorry, post id does not exists.".die()));
}
else
{
while ($row = $user->fetch_array())
{
$tagged_users[$i]['user_id']=$row['user_id'];
$array['user_id']=$tagged_users[$i]['user_id'];
$pro_image_url[$i]=$row['profile_image_url'];
$short_image_url[$i]=str_replace('_b','_t',$pro_image_url[$i]);
$short_image_url[$i]=str_replace('/images/','/thumbnails/',$short_image_url[$i]);
$array['short_image_url']=$short_image_url[$i];
}
}
array_push($row_array,$array);
}
}
}
array_push($json_response,$row_array);
echo str_replace('\/','/',json_encode($json_response));
}
}
It is returning the following objects
[
{
"id": "1111",
"image_urls": [
"https://docs.google.com/document/d/14kVqw9d2kzYIEClN-SVp2Co2mlglM9F-8HIy0ggTZ3g/edit",
"http://stackoverflow.com/questions/21762150/how-do-i-insert-data-from-a-json-array-into-mysql-database",
"https://drive.google.com/?authuser=0#my-drive",
"https://drive.google.com/?tab=mo&authuser=0#shared-with-me"
],
"0": {
"user_id": "111",
"short_image_url": "chrome://restclient/content/thumbnails/restclient_t.jpg"
},
"1": {
"user_id": "321",
"short_image_url": "chrome://restclient/thumbnails/restclient_t.jpg"
},
"2": {
"user_id": "1234",
"short_image_url": "http://54.169.40.195/wheel/wheel/service/testing/chetan/audio/c71dfe45421b2864476a0bde257f0a57e72084783ce859e26595599670904907.mp3"
}
}
]
but i want to assign name to that array like this
[
{
"id": "1111",
"image_urls": [
"https://docs.google.com/document/d/14kVqw9d2kzYIEClN-SVp2Co2mlglM9F-8HIy0ggTZ3g/edit",
"http://stackoverflow.com/questions/21762150/how-do-i-insert-data-from-a-json-array-into-mysql-database",
"https://drive.google.com/?authuser=0#my-drive",
"https://drive.google.com/?tab=mo&authuser=0#shared-with-me"
],
"tagged_users":[
"0": {
"user_id": "111",
"short_image_url": "chrome://restclient/content/thumbnails/restclient_t.jpg"
},
"1": {
"user_id": "321",
"short_image_url": "chrome://restclient/thumbnails/restclient_t.jpg"
},
"2": {
"user_id": "1234",
"short_image_url": "http://54.169.40.195/wheel/wheel/service/testing/chetan/audio/c71dfe45421b2864476a0bde257f0a57e72084783ce859e26595599670904907.mp3"
}
]
}
i don't know where i have to add this array name. Please help me solve this.
I can't assign the array name like this because $row_array also contains other data
echo str_replace('\/','/',json_encode(array("tagged_user"=>$json_response));
On second to last line this should be what you wanted:
array_push($json_response,array("tagged_users" => $row_array));
Edited to remove second part.
Try with -
array_push($row_array['tagged_users'],$array);
Or define it like -
$row_array['tagged_users'] = array();
Then all the values will be as you want.

json object is bahaving wrongly

I am test-running a web application
where in json is required. I have the following json
structure:
{
"Articles": [
{
"Article": {
"ID": 111,
"title": "Idiot",
"author": "Moron",
"pubDate": "11/2/14",
"summary": "bla bla bla"
},
"Article": {
"ID": 222,
"title": "wisdom",
"author": "wise one",
"pubDate": "11/2/15",
"summary": "ha ha ha"
}
}
]
}
I then decided to check if a matching ID exits before adding
any record. To this effect, I wrote a method, encased it within
a JSon class as follows:
public function ID_Exists($ID){
$file = file_get_contents($this->FileName, true);
$data = json_decode($file, false); //get json in array string format
foreach($data as $child){
foreach($child as $item){
if($item->ID == $ID){
echo 'Exists';
}else{
echo 'Non Existent';
}
}
}
}
I test-ran it like:
$Obj = new JSon('file.json'); //knows what to do
if($Obj->ID_Exists(111)){
//ok ! no problem
}else{
////no problem
}
Here's the output I got:
Undefined property: stdClass::$ID in
C:\Server\wamp\www\Oweb\libs\dmanager.php on line 635
What am I doing wrong? I don't want to use the array
format of json_decode().
Your JSON structure is impossible. Articles is an array which contains a single object which contains the key Article twice - this cannot work.
Your structure needs to be:
{
"Articles": [
{
"ID": 111,
...
},
{
"ID": 222,
...
}
]
}
Which you can traverse using:
$data = json_decode($json);
foreach ($data->Articles as $article) {
if ($article->ID == ..) ..
}

Categories