how to change values nested array value in php - 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..
:)

Related

parsing data from json php get image src

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.

How to loop over multi dimensional Instagram array and get the url of all images?

I want to loop over the array below and echo out every low_resolution url.
I have tried running a foreach loop over this but since it is multi dimensional I am not sure how to access the url value. (Not used to working with multi dimensional arrays)
PHP:
public function getInstagramImages() {
$url = "https://api.instagram.com/v1/users/31008104/media/recent?access_token=31008104.02ed65e.4130309107d34e2cb220d0ef62c2e86d";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($ch);
curl_close($ch);
$json_array = json_decode($json, true);
print_r($json_array);
}
The array:
ArrayArray ( [pagination] => Array ( ) [meta] => Array ( [code] => 200 ) [data] => Array ( [0] => Array ( [attribution] => [tags] => Array ( ) [type] => image [location] => [comments] => Array ( [count] => 0 ) [filter] => Amaro [created_time] => 1399119582 [link] => https://www.instagram.com/p/niND4jsISQi7hJjODg-Z6Mm1MaMrY9nOIfiaQ0/ [likes] => Array ( [count] => 2 ) [images] => Array ( [low_resolution] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/s320x320/e15/10268744_1432980593617900_371121229_n.jpg?ig_cache_key=NzEyMTg5MTMyNTY5MzQ3MjE2.2 [width] => 320 [height] => 320 ) [thumbnail] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/s150x150/e15/10268744_1432980593617900_371121229_n.jpg?ig_cache_key=NzEyMTg5MTMyNTY5MzQ3MjE2.2 [width] => 150 [height] => 150 ) [standard_resolution] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/e15/10268744_1432980593617900_371121229_n.jpg?ig_cache_key=NzEyMTg5MTMyNTY5MzQ3MjE2.2 [width] => 640 [height] => 640 ) ) [users_in_photo] => Array ( ) [caption] => Array ( [created_time] => 1399119582 [text] => Insane mall! [from] => Array ( [username] => dj_kp [profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg [id] => 31008104 [full_name] => Kieran P ) [id] => 712189132938446543 ) [user_has_liked] => [id] => 712189132569347216_31008104 [user] => Array ( [username] => dj_kp [profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg [id] => 31008104 [full_name] => Kieran P ) ) [1] => Array ( [attribution] => [tags] => Array ( [0] => tailored ) [type] => image [location] => [comments] => Array ( [count] => 0 ) [filter] => 1977 [created_time] => 1398926862 [link] => https://www.instagram.com/p/ncdeeosIU0UR6oYZoy8YMuwAGsM4PPDtW1xUo0/ [likes] => Array ( [count] => 2 ) [images] => Array ( [low_resolution] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/s320x320/e15/10296802_800197019990459_2044337121_n.jpg?ig_cache_key=NzEwNTcyNDc5MDQ1NzMxNjM2.2 [width] => 320 [height] => 320 ) [thumbnail] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/s150x150/e15/10296802_800197019990459_2044337121_n.jpg?ig_cache_key=NzEwNTcyNDc5MDQ1NzMxNjM2.2 [width] => 150 [height] => 150 ) [standard_resolution] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/e15/10296802_800197019990459_2044337121_n.jpg?ig_cache_key=NzEwNTcyNDc5MDQ1NzMxNjM2.2 [width] => 640 [height] => 640 ) ) [users_in_photo] => Array ( ) [caption] => Array ( [created_time] => 1398926862 [text] => Getting suited up! #tailored [from] => Array ( [username] => dj_kp [profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg [id] => 31008104 [full_name] => Kieran P ) [id] => 710572479314167115 ) [user_has_liked] => [id] => 710572479045731636_31008104 [user] => Array ( [username] => dj_kp [profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg [id] => 31008104 [full_name] => Kieran P ) )
Screenshot of array:
FYI the array goes on further than this. Just showing this part to keep it as simple as possible.
You can do something like this,
foreach($json_array['data'] as $images){
echo $images['images']['low_resolution']['url'];
}
This will give you all the low_resolution urls from each data array.
I tried with following code to get all image titles & urls.
if(is_array($user_recent_data->data)) {
foreach($user_recent_data->data as $feed_data) {
if(!empty($feed_data->caption))
{
$mediaTitle = $feed_data->caption->text;
}
else
{
$mediaTitle = '';
}
?>
<p><?php echo "<img src='".$feed_data->images->thumbnail->url."' style='width:135px; border:1px solid #ccc; padding:3px;'/>";?></p>
<?php
}
}
?>
$user_recent_data is the veriable where instagram array is collected.
What you need is a recursive function which looks for the index of url like this one:
function find_urls($array){
$urls = array();
foreach ($array as $item){
if (is_array($item)){
if(isset($item['url']){
$urls[] = $item['url'];
}
array_merge($urls,find_urls($item));
}
}
return $urls;
}
This should find any index called url in any array with any structure ;)

How to extract multiple elements of an array into a new array

My question is how would I take this array:
Array (
[0] => stdClass Object (
[item] => 0
[size] => 2657017
[group] => MAXAT
[description] => 265/70R17 MAXTRAC A/T 115T 00K
[sort4] => 115
[sort5] => T
[sort6] =>
[price] => 118.91
)
[1] => stdClass Object (
[item] => 8127
[size] => 2657017
[group] => FZSUV
[description] => 265/70R17 FUZION SUV OWL 115T 50K
[sort4] => 115
[sort5] => T
[sort6] =>
[price] => 137.81
)
[2] => stdClass Object (
[item] => 0
[size] => 2657017
[group] => MAXAT
[description] => LT265/70R17 MAXTRAC A/T 118S E 00K
[sort4] => 118
[sort5] => S
[sort6] => E
[price] => 153.79
)
[3] => stdClass Object (
[item] => 1237
[size] => 2657017
[group] => ATS
[description] => 265/70R17 GEO AT-S OWL 113S 50K
[sort4] => 113
[sort5] => S
[sort6] =>
[price] => 167.15
)
)
and turn it into this array (without running another query):
Array (
[0] => stdClass Object (
[group] => MAXAT
[price] => 118.91
)
[1] => stdClass Object (
[group] => FZSUV
[price] => 137.81
)
[2] => stdClass Object (
[group] => MAXAT
[price] => 153.79
)
[3] => stdClass Object (
[group] => ATS
[price] => 167.15
)
)
All that I am trying to achieve is to pull the group and price from the first array into a new array.
$newEntries = array();
foreach ($originalEntries as $originalEntry) {
$newEntry = new stdClass();
$newEntry->group = $originalEntry->group;
$newEntry->price = $originalEntry->price;
$newEntries[] = $newEntry;
}
Could you use foreach to loop through the array and insert values into new array?!
$new_array = array();
$i = 0;
foreach($array as $k => $v){
$new_array[$i]['group'] = $v['group'];
$new_array[$i]['price'] = $v['price'];
$i++;
}

PHP: Displaying single array data?

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

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.

Categories