Reading messy JSON using php - 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

Related

Clean Up GraphQL Response and Convert to JSON without edges and nodes

I am calling the Shopify admin api.
When I use the REST API, I get beautiful JSON objects in my responses.
[{
{
"id": 7036594978985,
"title": "7 Shakra Bracelet",
"body_html": "<p>7 chakra bracelet, in blue or black.</p>",
"vendor": "Company 123",
"product_type": "Bracelet",
"created_at": "2021-06-17T17:45:05-04:00",
"handle": "chain-bracelet",
"updated_at": "2021-06-23T20:08:21-04:00",
"published_at": "2021-06-17T17:45:04-04:00",
"template_suffix": null,
"published_scope": "web",
"tags": "Beads, Bracelet",
"admin_graphql_api_id": "gid://shopify/Product/7036594978985",
"variants": [
{
"id": 40671266963625,
"product_id": 7036594978985,
"title": "Blue",
"price": "42.99",
"sku": null,
"position": 1,
"inventory_policy": "deny",
"compare_at_price": "44.99",
"fulfillment_service": "manual",
"inventory_management": null,
"option1": "Blue",
}
},
{
id: 7036594978986
...
]
However, if I use GraphQL I end up with a bunch of edges and nodes that don't play well with my front end JS.
{
"products": {
"edges": [
{
"node": {
"description": "Sleek and black",
"featuredImage": {
"id": "gid://shopify/ProductImage/15464783282345",
"originalSrc": "https://cdn.shopify.com/s/files/1/0268/1005/6873/products/bb20.jpg?v=1602116082"
},
"handle": "skirt",
"id": "gid://shopify/Product/4773734482089",
"images": {
"edges": [
{
"node": {
"id": "gid://shopify/ProductImage/15464783282345",
"originalSrc": "https://cdn.shopify.com/s/files/1/0268/1005/6873/products/bb20.jpg?v=1602116082"
}
},
{
"node": {
"id": "gid://shopify/ProductImage/26072838406313",
"originalSrc": "https://cdn.shopify.com/s/files/1/0268/1005/6873/products/bb7.jpg?v=1612636091"
}
},
{
"node": {
"id": "gid://shopify/ProductImage/26072838373545",
"originalSrc": "https://cdn.shopify.com/s/files/1/0268/1005/6873/products/bb6.jpg?v=1612636091"
}
}
]
},
I'd like to flatten the array a little bit by replacing all edges and nodes with the friendly minimal JSON structure.
From this:
{
"products": {
"edges": [
{
"node": {
"description": "Sleek and black",
"featuredImage": {
"id": "gid://shopify/ProductImage/15464783282345",
"originalSrc": "https://cdn.shopify.com/s/files/1/0268/1005/6873/products/bb20.jpg?v=1602116082"
},
To this:
{
"products": [
{
"description": "Sleek and black",
"featuredImage": {
"id": "gid://shopify/ProductImage/15464783282345",
"originalSrc": "https://cdn.shopify.com/s/files/1/0268/1005/6873/products/bb20.jpg?v=1602116082"
},
I'd like to be able to call a recursive PHP function to replace all nodes and edges nested in the array.
I tried this, but I think it causes iteration issues.
public function parse_and_remove (&$arr) {
foreach ($arr as $key => $val) {
if (($key == 'edges' || $key == 'node') && (is_array($val) || is_object($val))) {
$arr = $val;
$val = $this->parse_and_remove($val);
} else if (is_array($val) || is_object($val)) {
$val = $this->parse_and_remove($val);
} else {
}
}
return $arr;
}
PART II
How is everyone okay with this kind of response in GraphQL? It's a pain to work with on the front end where API responses are often consumed. Don't give me the array of edges and the node then finally the object. I just want that array of objects :)
I finally created a function that did it. It is terribly inefficient because every time I move a node up to replace the parent, I break and restart the entire loop in order to be able to parse the parent again.
$nice_data = $this->move_value_at_node_to_parent($response['body']['data'], 'node');
$nice_data2 = $this->move_value_at_node_to_parent($nice_data, 'edges');
the function: (no indentation because I like prefer the ease of repetition with code alignment - * the foreach loops are nested)
public function move_value_at_node_to_parent($obj, $key_name) {
// make array
$obj_string = json_encode($obj);
$arr = json_decode($obj_string, true);
// do this a bunch to avoid index problems
for ($i=0; $i < 1000; $i++) {
if(!is_array($arr)) continue;
foreach ($arr as $key => $val) {
if($key == $key_name) {
$arr = $val;
break;
}
$arr2 = $arr[$key];
if(!is_array($arr2)) continue;
foreach ($arr2 as $key2 => $val2) {
if($key2 == $key_name) {
$arr[$key] = $val2;
break;
}
$arr3 = $arr[$key][$key2];
if(!is_array($arr3)) continue;
foreach ($arr3 as $key3 => $val3) {
if($key3 == $key_name) {
$arr[$key][$key2] = $val3;
break;
}
$arr4 = $arr[$key][$key2][$key3];
if(!is_array($arr4)) continue;
foreach ($arr4 as $key4 => $val4) {
if($key4 == $key_name) {
$arr[$key][$key2][$key3] = $val4;
break;
}
$arr5 = $arr[$key][$key2][$key3][$key4];
if(!is_array($arr5)) continue;
foreach ($arr5 as $key5 => $val5) {
if($key5 == $key_name) {
$arr[$key][$key2][$key3][$key4] = $val5;
break;
}
$arr6 = $arr[$key][$key2][$key3][$key4][$key5];
if(!is_array($arr6)) continue;
foreach ($arr6 as $key6 => $val6) {
if($key6 == $key_name) {
$arr[$key][$key2][$key3][$key4][$key5] = $val6;
break;
}
$arr7 = $arr[$key][$key2][$key3][$key4][$key5][$key6];
if(!is_array($arr7)) continue;
foreach ($arr7 as $key7 => $val7) {
if($key7 == $key_name) {
$arr[$key][$key2][$key3][$key4][$key5][$key6] = $val7;
break;
}
}
}
}
}
}
}
}
}
return $arr;
}

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

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

Get JSON key By Matching sub-object's key value

I have a json string:
$testArray =
{
"Test1": {
"id": "26",
"admin": "Admin TestClient"
},
"Test2": {
"id": "27",
"admin": "Admin TestClient"
},
"Test3": {
"id": "28",
"admin": "Admin TestClient"
}
}
And a variable with id value, say
$idSearch = 28;
Now I need to get its key : "Test3"
I tried :
$NameKey = array_search($idSearch , $testArray->id);
But this gives null
For this, it's better to use a simple loop:
function getById($id) {
foreach ($testArray as $key => $value) {
if ($value['id'] == $id) {
return $key;
}
}
return '';
}
$key = getById(28);
var_dump($key);
array_reduce($testArray, function ($result, $item) use ($idSearch) {
return $result ?: ($item['id'] === $idSearch ? $item : null);
});

Categories