PHP: Displaying single array data? - php

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

Related

how to change values nested array value in php

I'm having an array like this
Array
(
[0] => stdClass Object
(
[id] => 1522
[image] => 1465458797-sug.jpg,1465458797-rajdhanee.jpg
[user_name] => Suganya
[thumbnail] => Taylor_Otwell_.jpg
[feature_product] => 1
)
[1] => stdClass Object
(
[id] => 151
[image] => 1465296555-14.jpg
[user_name] => Sugan
[thumbnail] => sug.jpg
[feature_product] => 0
)
[2] => stdClass Object
(
[id] => 1
[image] => 1401101483-best-kitchen-appliances.jpg,1401101483-home-appliance_laundry.jpg,1401101483-kitchen-appliances-contemporary.jpg
[user_name] => admin
[thumbnail] => images_(1).jpg
[feature_product] => 0
)
)
And I need the resultant array like this
Array
(
[0] => stdClass Object
(
[id] => 1522
[image] => www.example.com/1465458797-sug.jpg,www.example.com/1465458797-rajdhanee.jpg
[user_name] => Suganya
[thumbnail] => Taylor_Otwell_.jpg
[feature_product] => 1
)
[1] => stdClass Object
(
[id] => 151
[image] => www.example.com/1465296555-14.jpg
[user_name] => Sugan
[thumbnail] => sug.jpg
[feature_product] => 0
)
[2] => stdClass Object
(
[id] => 1
[image] => www.example.com/1401101483-best-kitchen-appliances.jpg,www.example.com/1401101483-home-appliance_laundry.jpg,www.example.com/1401101483-kitchen-appliances-contemporary.jpg
[user_name] => admin
[thumbnail] => images_(1).jpg
[feature_product] => 0
)
)
i.e.,
The image values must append the given url into it...
I tried something like follows,But it din't work for me..
foreach ($result as $key => $value) {
$req_i=0;
$img = explode(',', $value->image);
if(!isset($image)){
$image = $url.$image_url.$img[$req_i];
} else {
// $value->image = $url.'images/'.$default_image;
$image = $image.','.$url.$image_url.$img[$req_i];
}
$req_i++;
}
print_r($image);echo "<br>";exit;
Here I'm just try to print those values..
But it returns value like this.
www.example.com/1465458797-sug.jpg,www.example.com/1465296555-14.jpg,www.example.com/1401101483-best-kitchen-appliances.jpg
Somebody help me that how could I reach this :(
Thank you,
im assuiming your using this to display in a view why dont you just pass it in the view?
<image src="www.example.com/<?php 'echo Your image path '; ?> "
I got it guyzz...
Finally I did it..
$url = www.example.com/
foreach ($result as $key => $value) {
$image = array();
$img = explode(',', $value->image);
foreach ($img as $key_in => $value_in) {
$image[] = $url.$value_in;
}
$value->image = implode(',', $image);
}
And I get final array as
Array
(
[0] => stdClass Object
(
[id] => 1522
[image] => www.example.com/1465458797-sug.jpg,www.example.com/1465458797-rajdhanee.jpg
[user_name] => Suganya
[thumbnail] => Taylor_Otwell_.jpg
[feature_product] => 1
)
[1] => stdClass Object
(
[id] => 151
[image] => www.example.com/1465296555-14.jpg
[user_name] => Sugan
[thumbnail] => sug.jpg
[feature_product] => 0
)
[2] => stdClass Object
(
[id] => 1
[image] => www.example.com/1401101483-best-kitchen-appliances.jpg,www.example.com/1401101483-home-appliance_laundry.jpg,www.example.com/1401101483-kitchen-appliances-contemporary.jpg
[user_name] => admin
[thumbnail] => images_(1).jpg
[feature_product] => 0
)
)
Thank you everyone who are all trying to help me..
:)

Looping through associative array using foreach

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.

Youtube API PHP json decode

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.

how do i access these array keys as a variable in CI?

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

Accessing field value in Drupal 7 on load_node in template

In a template for a content type I am loading a node from a node reference.
It loads and if I do a print_r I get this:
stdClass Object (
[vid] => 40
[uid] => 14
[title] => Cover
[log] =>
[status] => 1
[comment] => 0
[promote] => 1
[sticky] => 0
[nid] => 40
[type] => portfolio_image_main
[language] => en
[created] => 1309382711
[changed] => 1309382711
[tnid] => 0
[translate] => 0
[revision_timestamp] => 1309382711
[revision_uid] => 14
[field_portolio_image] => Array (
[en] => Array (
[0] => Array (
[fid] => 5626
[alt] =>
[title] =>
[uid] => 14
[filename] => Cover.jpg
[uri] => public://Cover.jpg
[filemime] => image/jpeg
[filesize] => 147898
[status] => 1
[timestamp] => 1309382711
)
)
)
[name] => jojo
[picture] => 0
[data] => a:1:{s:7:"contact";i:1;}
)
and Im trying to access the single variable here:
$newImagePath1 = $newImage1->field_portfolio_image['en '][0]['filename'];
but so far nothing. Any thoughts?
please try to use below code
$keys = array_keys($arr[field_portolio_image][en]);
$arr[field_portolio_image][en][$keys][filename];
There is a helper function to access field items for the user's correct language (otherwise, you'd have to hardcode the ['en'] part).
field_get_items()
So your code would end up being something like this:
$field_instances = field_get_info('node', $newImage1, 'field_portfolio_image');
// $field_instances should now be an array.
foreach ($field_instances as $field_instance) {
print $field_instance['filepath'];
}

Categories