So I'm using curl to return some JSON from an API and I'm using json_decode($result,true);
I have managed to access the title but I need to loop through the whole array and output the title, url, location and picture.
I'm doing a foreach to print all the content:
<?php
foreach ($json as $value) {
echo "<pre>";
print_r($json);
echo $json['data'][2]['title'];
}
?>
What blocks me its how do I access every element.
Array
(
[paging] => Array
(
[total_items] => 33
[current_page] => 1
[total_pages] => 3
)
[data] => Array
(
[0] => Array
(
[id] => 776583
[title] => NAME
[url] =>URL
[status] => open
[current_status] => open
[location] => a, a
[programmes] => Array
(
[id] => 2
[short_name] => SHORT NAME
)
[applications_count] => 5
[is_favourited] =>
[branch] => Array
(
[id] => 319532
[name] => International SOS - 1
[organisation_id] => 318911
[profile_photo_url] => URL
[url] => URL
)
[views] => 248
[duration_min] => 22
[duration_max] => 24
[applications_close_date] => 2016-09-06T00:00:00.000Z
[earliest_start_date] => 2016-10-01T00:00:00.000Z
[latest_end_date] => 2017-04-01T00:00:00.000Z
[profile_photo_urls] => Array
(
[original] => PIC URL
[medium] => PIC URL
[thumb] => PIC URL
)
[cover_photo_urls] => PNG IMG
[created_at] => 2016-08-30T03:24:41Z
[updated_at] => 2016-08-30T15:36:02Z
)
[1] => Array
(
[id] => 774984
[title] => NAME
[url] => URL
[status] => open
[current_status] => open
[location] => Bonn, Germany
[programmes] => Array
(
[id] => 2
[short_name] => NAME
)
[applications_count] => 128
[is_favourited] =>
[branch] => Array
(
[id] => 287321
[name] => Deutsche Post DHL Group
[organisation_id] => 286836
[profile_photo_url] => PHOTO
[url] => URL
)
[views] => 1331
[duration_min] => 48
[duration_max] => 48
[applications_close_date] => 2016-09-04T00:00:00.000Z
[earliest_start_date] => 2016-10-01T00:00:00.000Z
[latest_end_date] => 2017-10-01T00:00:00.000Z
[profile_photo_urls] => Array
(
[original] => PIC
[medium] => PIC
[thumb] => PIC
)
[cover_photo_urls] => PIC
[created_at] => 2016-08-23T19:47:04Z
[updated_at] => 2016-08-24T06:35:58Z
)
<?php
foreach ($json as $value) {
foreach($value['data'] as $inner){ //looping json['data']
echo $inner['id'];
echo $inner['title'];
foreach($inner['programmes'] as $in_inner){ //looping json['data']['element_number']['programmers']
echo $in_inner['id'].':'.$in_inner['short_name'];
}
}
}
?>
In this way you can iterate through inner arrays.
Related
I'm trying to access the second array (contacts) in this decoded json to grab the ID next to first_name (the two ID keys could be different), but the second array doesn't seem to be an object so I can't find a loop which can acces it:
stdClass Object (
[data] => Array (
[0] => stdClass Object (
[account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv
[is_owner] => 1
[id] => 1
[name] => test test
[display_name] => test test
[balance] => 0
[paid_to_date] => 0
[updated_at] => 1578494555
[archived_at] =>
[address1] => Street
[address2] =>
[city] => Town
[state] => State
[postal_code] => Code
[country_id] => 0
[work_phone] => Number
[private_notes] =>
[public_notes] =>
[last_login] =>
[website] =>
[industry_id] => 0
[size_id] => 0
[is_deleted] =>
[payment_terms] => 30
[vat_number] =>
[id_number] =>
[language_id] => 0
[currency_id] => 0
[custom_value1] =>
[custom_value2] =>
[invoice_number_counter] => 1
[quote_number_counter] => 1
[task_rate] => 0
[shipping_address1] =>
[shipping_address2] =>
[shipping_city] =>
[shipping_state] =>
[shipping_postal_code] =>
[shipping_country_id] => 0
[show_tasks_in_portal] => 1
[send_reminders] => 1
[credit_number_counter] => 1
[custom_messages] => {}
[contacts] => Array (
[0] => stdClass Object (
[account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv
[is_owner] => 1
[id] => 1
[first_name] => test
[last_name] => test
[email] => myemail#me.com
[contact_key] => mq1dzpkqznfgtqwhdwt9nte1ohmvsju1
[updated_at] => 1578494555
[archived_at] =>
[is_primary] => 1
[phone] => 07919446174
[last_login] =>
[send_invoice] => 1
[custom_value1] =>
[custom_value2] =>
)
)
)
)
[meta] => stdClass Object (
[pagination] => stdClass Object (
[total] => 1
[count] => 1
[per_page] => 15
[current_page] => 1
[total_pages] => 1
[links] => Array ( )
)
)
)
This is what I've tried, but it doesn't find anything:
$person = getclient($itemid);
$person_data = json_decode($person);
foreach ($person_data->contacts as $key => $item)
{
$itemid = $item->id . "<br />";
}
$person_data contains the data property, which is an array of objects.
foreach ($person_data->data as $person) {
foreach ($person->contacts as $contact) {
echo $contact->id . '<br>';
}
}
I want to parse data from JSON Instagram without API,
a result like this
Array ( [nodes] => Array ( [0] => Array ( [__typename] => GraphVideo [id] => 1532294365652508146 [comments_disabled] => [dimensions] => Array ( [height] => 750 [width] => 750 ) [gating_info] => [media_preview] => ACoq6aiionJGTQBLRWXLOwJySuFyATjv16j2496dbu8mQWJwAVPqp6H69jQBpUUg6UtABWLfXslvkjBGcAEVtVxOoO09w69FRj9Bj+poASC5kklJkO7cDn8fT2rTsJminEJ5XG36dx+XeseAHf8APkg8Z7EfX04pxuvs0xaMDAOQD0zigDuaWooX8yNXP8Sg/mM1LQAVEYIySSq5PX5Rz9eKlooAiEEYGAqgfQU020J5KIf+Aj/Cp6KAEAAGBwBS0UUAf//Z [owner] => Array ( [id] => 6860189 ) [thumbnail_src] => https://scontent.cdninstagram.com/t51.2885-15/s640x640/e15/18888588_1753665424925670_2681598092939100160_n.jpg [is_video] => 1 [code] => BVDzZtsDN3y [date] => 1496883744 [display_src] => https://scontent.cdninstagram.com/t51.2885-15/e15/18888588_1753665424925670_2681598092939100160_n.jpg [video_views] => 2304729 [caption] => #2U Friday #romeestrijd [comments] => Array ( [count] => 18485 ) [likes] => Array ( [count] => 985087 ) ) [1] => Array ( [__typename] => GraphVideo [id] => 1532284690492268192 [comments_disabled] => [dimensions] => Array ( [height] => 750 [width] => 750 ) [gating_info] => [media_preview] => ACoq6aikNU7ycwwF1wDwAT0GSBn8KALlGa5u21KSGRVmbzI3ON5GME9MHuP89q6OgB1FJS0AIaytVRntG29sH64Nap6Vg6xKUtlXOGY9B0IHJ+vOPzoA56SVnQZ9evoR6eldtZzieJXyMkDODnBxXCNOzrtOPr3rZ0ViHCDo27d+XH9KAOsoqpZwPBHskbeck556enOat0AJVG606O6ADlgF6AEY+vIP0/8A1VfooAw/+Eft/V/zH/xNXbPTorMkoWJIx8xB/kBV+igAooooA//Z [owner] => Array ( [id] => 6860189 ) [thumbnail_src] => https://scontent.cdninstagram.com/t51.2885-15/s640x640/e15/18947831_1799767330352720_4657693158346326016_n.jpg [is_video] => 1 [code] => BVDxM6_j56g [date] => 1496882591 [display_src] => https://scontent.cdninstagram.com/t51.2885-15/e15/18947831_1799767330352720_4657693158346326016_n.jpg [video_views] => 3116560 [caption] => #2U Friday #sarasampaio [comments] => Array ( [count] => 28703 ) [likes] => Array ( [count] => 1272010 ) ) [2] => Array ( [__typename] => GraphImage [id] => 1532046157831653372 [comments_disabled] => [dimensions] => .....
how to I can get a display_src result in foreach loop
$data = json_decode($response, true);
if ($data === null) {
break;
}
$media = $data['user']['media'];
$found += count($media['nodes']);
foreach ($media as $dataa){
echo $dataa->nodes->display_src;
}
I try it but I do not get result.
So the code is
$gallery = get_gallery('gallery_id');
print_r ($gallery);
And I get:
Array ( [0] => Array ( [id] => 13 [image] => 0 [user] => 13 [timestamp] => 1366237591 [ext] => png [caption] => Happy smile shi ma? [comment] => ) [1] => Array ( [id] => 14 [image] => 0 [user] => 13 [timestamp] => 1366237954 [ext] => jpg [caption] => Confused [comment] => ) [2] => Array ( [id] => 15 [image] => 0 [user] => 13 [timestamp] => 1366237979 [ext] => jpg [caption] => Facebookerg [comment] => ) [3] => Array ( [id] => 16 [image] => 0 [user] => 13 [timestamp] => 1366377510 [ext] => gif [caption] => lolwut? [comment] => ) [4] => Array ( [id] => 17 [image] => 0 [user] => 13 [timestamp] => 1366380899 [ext] => jpg [caption] => rorwut? [comment] => ) [5] => Array ( [id] => 18 [image] => 0 [user] => 13 [timestamp] => 1366651685 [ext] => jpg [caption] => Notes? [comment] => ) [6] => Array ( [id] => 19 [image] => 0 [user] => 13 [timestamp] => 1366711880 [ext] => jpg [caption] => asd [comment] => ) [7] => Array ( [id] => 20 [image] => 0 [user] => 14 [timestamp] => 1366940983 [ext] => jpg [caption] => Belzelga [comment] => ) )
Which is good, it finally worked. But how do you display a single data/table.
Because I am trying to get a single 'id' from these thingies.
I tried echoing $gallery['id']
but I got an error. :/
You need to access the correct index first:
$gallery[0]['id']
// ^^^
Your $gallery variable is now a multi dimentional array.
You have
$gallery[0]['id'];
$gallery[1]['id'];
.....
Now you can either use a foreach to process through the array or a for loop
foreach ($gallery as $anItem ) {
echo $anItem['id'];
}
OR
for ( $x=0; $x < count($gallery); $x++ ) {
echo $gallery[$x]['id'];
}
Try this looping code-segment:
foreach( $gallery as $temp ) {
echo $temp['id'];
}
Your $gallery is a multidimensional array. You need to iterate through it.
If you want to only display a particular gallery, you should probably use something like www.yoursite.com/gallery.php?id=1. then in your code to display it
if(isset($_GET['id']))
{
foreach( $gallery as $g )
{
if($g['id'] == $_GET['id'])
echo $g['id'];
}
}
You can get the data so. Without being bound to a key.
current($gallery)['id']
I'm trying to search youtube and grab the videos video ID and title.
In the bellow example I am searching for "youtube":
<?php
$url="http://gdata.youtube.com/feeds/api/videos?q='youtube'&format=5&max-results=2&v=2&alt=jsonc";
$json = file_get_contents($url,0,null,null);
$json_output = json_decode($json);
echo '<pre>';
print_r("query results:");
print_r($json_output);
'</pre>';
foreach ( $json_output->data as $data ){
echo "{$data->id}";
echo "{$data->title}";
}
?>
Here is the json output for the above query:
query results:stdClass Object
(
[apiVersion] => 2.1
[data] => stdClass Object
(
[updated] => 2012-03-04T20:19:06.314Z
[totalItems] => 1000000
[startIndex] => 1
[itemsPerPage] => 2
[items] => Array
(
[0] => stdClass Object
(
[id] => IpSYwvzKukI
[uploaded] => 2009-03-14T08:17:36.000Z
[updated] => 2012-02-29T10:51:35.000Z
[uploader] => kenbedict009
[category] => Music
[title] => one of the funniest kid in youtube!!
[description] => he is a 3 years old korean,this kid makes me laugh
[tags] => Array
(
[0] => rin on the rox
[1] => charice pempengco
[2] => joeydiamond
)
[thumbnail] => stdClass Object
(
[sqDefault] => http://i.ytimg.com/vi/IpSYwvzKukI/default.jpg
[hqDefault] => http://i.ytimg.com/vi/IpSYwvzKukI/hqdefault.jpg
)
[player] => stdClass Object
(
[default] => http://www.youtube.com/watch?v=IpSYwvzKukI&feature=youtube_gdata_player
[mobile] => http://m.youtube.com/details?v=IpSYwvzKukI
)
[content] => stdClass Object
(
[5] => http://www.youtube.com/v/IpSYwvzKukI?version=3&f=videos&app=youtube_gdata
[1] => rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQlCusr8wpiUIhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
[6] => rtsp://v8.cache2.c.youtube.com/CiILENy73wIaGQlCusr8wpiUIhMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
)
[duration] => 344
[rating] => 4.6304307
[likeCount] => 23419
[ratingCount] => 25803
[viewCount] => 9848083
[favoriteCount] => 15390
[commentCount] => 16074
[accessControl] => stdClass Object
(
[comment] => allowed
[commentVote] => allowed
[videoRespond] => moderated
[rate] => allowed
[embed] => allowed
[list] => allowed
[autoPlay] => allowed
[syndicate] => allowed
)
)
[1] => stdClass Object
(
[id] => PPNMGYOm1aM
[uploaded] => 2011-06-23T16:35:33.000Z
[updated] => 2012-02-29T13:15:47.000Z
[uploader] => shakeitupvevo
[category] => Music
[title] => "Watch Me" from Disney Channel's "Shake It Up"
[description] => To download Watch Me visit www.smarturl.it Performed by Bella Thorne and Zendaya
[tags] => Array
(
[0] => Bella
[1] => Thorne
[2] => Zendaya
[3] => Cast
[4] => of
[5] => Shake
[6] => It
[7] => Up:
[8] => Break
[9] => Down
[10] => Watch
[11] => Me
[12] => Walt
[13] => Disney
[14] => Soundtrack
)
[thumbnail] => stdClass Object
(
[sqDefault] => http://i.ytimg.com/vi/PPNMGYOm1aM/default.jpg
[hqDefault] => http://i.ytimg.com/vi/PPNMGYOm1aM/hqdefault.jpg
)
[player] => stdClass Object
(
[default] => http://www.youtube.com/watch?v=PPNMGYOm1aM&feature=youtube_gdata_player
)
[content] => stdClass Object
(
[5] => http://www.youtube.com/v/PPNMGYOm1aM?version=3&f=videos&app=youtube_gdata
)
[duration] => 193
[aspectRatio] => widescreen
[rating] => 4.7312055
[likeCount] => 145059
[ratingCount] => 155509
[viewCount] => 48201555
[favoriteCount] => 69981
[commentCount] => 81938
[status] => stdClass Object
(
[value] => restricted
[reason] => limitedSyndication
)
[restrictions] => Array
(
[0] => stdClass Object
(
[type] => country
[relationship] => deny
[countries] => DE
)
)
[accessControl] => stdClass Object
(
[comment] => allowed
[commentVote] => allowed
[videoRespond] => allowed
[rate] => allowed
[embed] => allowed
[list] => allowed
[autoPlay] => allowed
[syndicate] => allowed
)
)
)
)
)
Im trying to echo out [id] and [title]. Any idea what I am doing wrong?
Thanks!
Change line 9 to:
foreach ( $json_output->data->items as $data ){
You should get first items as an Objects Array:
$items = $json -> data -> items;
So try foreach now:
foreach ( $items as $item ){
echo "{$item->id}";
echo "{$item->title}";
}
I hope it's clear now.
Array
(
[abc] => Array
(
[0] => Array
(
[id] => 1
[title] => hello 12
[meta_keyword] =>
[meta_description] =>
[tags] => sdfgdfg
[status] => draft
[body] => dsfdsf dfdsafsdfsdfsdf
[photo] => images/blog/nari.jpg
[raw] => nari
[ext] => .jpg
[views] => 0
[video] =>
[categoryid] => 5
[subcatid] => 7
[featured] =>
[pubdate] => 2011-06-17 03:39:55
[user_id] => 0
)
[1] => Array
(
[id] => 2
[title] => hello xyz
[meta_keyword] =>
[meta_description] =>
[tags] => xcfasdfcasd
[status] => draft
[body] => dfdsafsdf dsfdsf dfdsafsdfsdfsdf
[photo] => images/blog/nari.jpg
[raw] => nari
[ext] => .jpg
[views] => 0
[video] =>
[categoryid] => 1
[subcatid] => 2
[featured] =>
[pubdate] => 2011-06-17 03:43:12
[user_id] => 0
)
for example if i want to echo out title I would do echo $abc['title'] but it's not working pls help,
the above output is a result of print_r($count['abc]);
it shows nothing when i do print_r($count['abc']['title'])
You would need to use the numeric key as well: $abc[0]['title'].
In other words, you've got an array with array members of an array type which use numeric keys, in which each of those members are arrays which use associative keys to access values. So you need to access each array in $abc to get to the array which contains your title values.
EDIT
If you're trying to loop through these values, you would need to loop through each array. Such as:
$c_abc = count($abc);
for ($i = 0; $i < $c_abc; $i++) {
echo "{$abc[$i]['title']}<br/>";
}
Read about php associative arrays....you will have you goal achieved
try this:
foreach ($array as $i => $values) {
print "$i {\n";
foreach ($values as $key => $value) {
print " $key => $value\n";
}
print "}\n";
}
To access you array variables, the right way is like this
$count['abc'][0]['title']
However, in your title, you are asking about Array keys as variables?
Actually this does not need to be related with CI.
A simple example
$array = array ( "hi" => "bye");
extract( $array);
//it will make "hi" a variable :: $hi = "bye"
echo $hi; // will output bye
Heres structured solution
$data = Array(
[abc] => Array
(
[0] => Array
(
[id] => 1
[title] => hello 12
[meta_keyword] =>
[meta_description] =>
[tags] => sdfgdfg
[status] => draft
[body] => dsfdsf dfdsafsdfsdfsdf
[photo] => images/blog/nari.jpg
[raw] => nari
[ext] => .jpg
[views] => 0
[video] =>
[categoryid] => 5
[subcatid] => 7
[featured] =>
[pubdate] => 2011-06-17 03:39:55
[user_id] => 0
)
[1] => Array
(
[id] => 2
[title] => hello xyz
[meta_keyword] =>
[meta_description] =>
[tags] => xcfasdfcasd
[status] => draft
[body] => dfdsafsdf dsfdsf dfdsafsdfsdfsdf
[photo] => images/blog/nari.jpg
[raw] => nari
[ext] => .jpg
[views] => 0
[video] =>
[categoryid] => 1
[subcatid] => 2
[featured] =>
[pubdate] => 2011-06-17 03:43:12
[user_id] => 0
)
)
);
extract($data);
foreach($abc as $value){
echo $value['title']."<br>";
}