basic json decode question - php

good evening. ingore all other elements, I want get first image in each foreach. how to?
$data = json_decode($json,true);
foreach ($data['data'][0] as $image) {
echo '<img src="'.$image['images'][0]['source'][0].'" />';
}
json tree is too large, over the letters paste limit.
Oops! Your question couldn't be submitted because:
body is limited to 30000 characters; you entered 68494
so here is the url u can get the json tree https://graph.facebook.com/5550296508/photos
and u can paste in http://jsonlint.com/ look the struction well. Thanx.

With json_decode every {} (object) will become an object(stdClass) and every [] (array) will become an array. So:
$data->data[0]->images[0]->source
is what you need, to reach the first image source.
Edit: since the second parameter of json_decode is true, it will become an associative array, and it will be like:
$data['data'][0]['images'][0]['source']
To get all the images:
$images = array();
foreach ($data['data'] as $d)
{
foreach ($d['images'] as $i)
{
$images[] = $i['source'];
}
}

Related

PHP function return multiple JSON object

I have a php function that currently looks like this
function renderJson(PageArray $items) {
$myData = array();
$myData['title'] = [];
$myData['url'] = [];
// cycle through all the items
foreach($items as $item) {
array_push($myData["title"],$item->title);
array_push($myData["url"],$item->url);
}
return json_encode($myData);
}
It currently works in this shape as output
{"title":["A","B"],"url":["A","B"]}
But this is what I am trying to achieve
{"title":["A"],"url":["A"]},{"title":["B"],"url":["B"]}
I am trying to return it as a JSON encode.The $item->title and $item->url returns a string.Any help would be great.
So now you get this :
{"title":["A","B"],"url":["A","B"]}
But you want this :
{"title":["A"],"url":["A"]},{"title":["B"],"url":["B"]}
What you want is add a new "item" each time you loop and each item have a title and an url. Right now you return an array that contain 2 sub-array : one for the title, one for the url.
Try this way :
function renderJson(PageArray $items) {
// Your result array
$myData = array();
// cycle through all the items
foreach($items as $item) {
// Each time you loop, you add a new array in your main array with a title and an URL
$myData[] = array(
"title" => $item->title,
"url" => $item->url
);
}
return json_encode($myData);
}
I used [] because he does what you need : push element at the end of your main array. It's the short way to use array_push(). Here is the documentation for more details : http://php.net/manual/en/function.array-push.php
The output will be :
[{"title":["A"],"url":["A"]},{"title":["B"],"url":["B"]}]
What you want to return is not, of course, valid json. At this point return an array containing those two objects (i.e., push the whole $item to $myData)
function renderJson(PageArray $items) {
$myData = array();
// cycle through all the items
foreach($items as $item) {
array_push($myData,['title' => $item->title, 'url' => $item->url]);
}
return json_encode($myData);
}
which gives you something in the form of
[{"title":"A","url":"A"},{"title":"B","url":"B"}]
You create invalid JSON by concatenation your JSON by comma. Either get your client (speak receiver of the json data) to split the result by comma and then parsing each JSON by its own.
Otherwise you could create a "big" JSON string, containing both (or all if there are more) of your JSON data.
Just create an array in PHP, add a new element for each JSON data, then json_encode() it and go over it by a loop in your client.
What you will use in the end, doesnt really matter that much, but I would use the second approach by a lot over the first, since it´s much cleaner.

php parse content for input->name value

I'm trying to parse html content, for the value stored in the name tag.
My current try looks like the following:
function getAuthToken(){
$html = file_get_contents('url');
$values = array();
foreach($html->find('input') as $element)
{
$values[$element->name] = $element->value;
}
print_r($values);
}
What am I doing wrong?
$html in your code is not an object its a string (the results of file_get_contents).
$object->name is a class object
$array[key] is an array element
Neither of these in your code will do what you want to achieve with the result of file_get_contents because your $html var is a string, you need to do some string parsing to get the desired results.
You can check that with:
echo gettype($html);
You can also just echo out $html to find out what the string looks like to give you a better idea of what you are working with, for instance, is there a common character that you can explode the sting with an so getting an array to work with within your foreach.
example:
$newarray = explode('&', $html);
foreach ($newarray as $key => $value) {
//do your thing
}

php remove element from array without add key

I want to remove an element from an array (converted from json), but with unset, and reconvert in json, the array become indexed.
Source array:
{"rows":
[{"c":[{"v":"Date(1409052482000)"},{"v":22},{"v":22},{"v":22},{"v":null}]},
{"c":[{"v":"Date(1409052614000)"},{"v":22},{"v":22},{"v":22},{"v":null}]},
{"c":[{"v":"Date(1409052782000)"},{"v":22},{"v":22},{"v":22},{"v":null}]}
]}
Result:
{"rows":
"2":{"c":[{"v":"Date(1409052614000)"},{"v":22},{"v":22},{"v":22},{"v":null}]},
"3":{"c":[{"v":"Date(1409052782000)"},{"v":22},{"v":22},{"v":22},{"v":null}]}
}}
the problem is the "2" and "3" keys. I don't want this keys, because I use the data for google chart, and is sensible for this index key.
PHP code:
$tempdata = json_decode($jsonTempLog, TRUE);
foreach ($tempdata['rows'] as $key => $row) {
if ( $logtime < $showtime) {
unset($tempdata['rows'][$key]);
}
}
echo json_encode($tempdata);
How can I remove element from array, keep the original json syntax?
Just do this:
$tempdata["rows"] = array_values($tempdata["rows"]);
echo json_encode($tempdata);
Otherwise JSON thinks you're sending an associative array rather a numeric one
this is how i work with :
unset($infos[$i]);
$infos = array_values($infos);
maybe like this:
foreach($tempdata as $row){
$tempdata[$rows['keyfield']] = $row;
}

PHP: String in json parse

I am trying to parse a json. I am running this in a foreach loop and if I do the following it works:
$places = array('restaurant', 'store', 'etc')
foreach ($this->placesCachingTypes as $places) {
$places_location_lat = $json_decoded->json[0]->restaurant[0]->geometry->location->lat;
$places_location_lng = $json_decoded->json[0]->restaurant[0]->geometry->location->lng;
}
However, when I do the following, i.e. I change restaurant to $places (I need to do this since I have an array of different places and I want to parse all of them in a foreach loop) it doesn't work.
foreach ($this->placesCachingTypes as $places) {
$places_location_lat = $json_decoded->json[0]->$places[0]->geometry->location->lat;
$places_location_lng = $json_decoded->json[0]->$places[0]->geometry->location->lng;
}
Solution is changing $places to {$places}[0]
The $places array contains keywords, such as restaurant or store. So the [0] is referring to the first one in the json which is why it's needed.
Why do you have this in the first loop:
json[0]->restaurant[0]
json[0]->$restaurant[0]
But then in the next you have:
json[0]->$places[0]
json[0]->$places[0]
Perhaps you are parsing the JSON incorrectly and it should be:
foreach ($this->placesCachingTypes as $places) {
$places_location_lat = $json_decoded->json[0]->places[0]->geometry->location->lat;
$places_location_lng = $json_decoded->json[0]->places[0]->geometry->location->lng;
}
And then in the first loop, you should do a similar edit to get rid of $restaurant[0]:
foreach ($this->placesCachingTypes as $places) {
$places_location_lat = $json_decoded->json[0]->restaurant[0]->geometry->location->lat;
$places_location_lng = $json_decoded->json[0]->restaurant[0]->geometry->location->lng;
}
Then again, unclear on what value $places has when you loop via foreach ($this->placesCachingTypes as $places) {. It does’t make sense what you would be looping through with the value of $places. And perhaps assigning that $places in the loop object of $json_decoded->json[0]-> is the source of your issues? Need more info from you to confirm this.

How to use JSON with unknown multiple keys (PHP)

I am receiving a JSON String, which has multiple unknown keys. It's pretty hard to explain, because those JSON strings are pretty large, I'll try to break them down in the most efficient way.
I use PHP to break down the object, that I get, when I decode the JSON string.
$data1 = $json->result->map->12313214654[0]
$data2 = $json->result->map->12313214654[2]
$differentdata1 = $json->result->map->12313214655[0]
As you can see, there are different subsections after the map key.
Those are numbers, that are pretty random. And their appearance isn't regular either. So sometimes there's just one subsection (or number), and sometimes more.
How can I access them? I tried to use $datacount = $count($json->result->map) But it always displays 1. And then I still wouldn't be able to access the subelements of the map key.
Does anyone have a solution for this?
As you seem to have an unknown amount of data in array form, you could iterate your results with a foreach loop:
foreach ($json->result->map as $key => $dataArray) {
// $key will be the numeric key, e.g. 12313214654
// $dataArray will be the array of data you're after
foreach ($value as $dataIndex => $data) {
// $dataIndex is the position of $data within the $dataArray
// $data is the value you were trying to access with $json->result->map->12313214654[n]
// You do your work with $data here.
print_r($data);
}
}

Categories