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++;
}
Related
I'm trying to generate a product combination system based on 2 or more types of options where the final array should look like as displayed in the bellow example. I'm a bit new to this and I have no clue how to do it.
They array keys should start from 1000 and increment from there. The first part of the array contains info about option like option_id name etc and the second part contains some parts of the products in product_option_value.
Any advice or help would be helpful because i'm out of ideas it's very frustrating.
Given an array of options
Array
(
[0] => Array
(
[option_id] => 19
[name] => Colors
)
[1] => Array
(
[option_id] => 16
[name] => Fabric
)
[2] => Array
(
[option_id] => 12
[name] => Size
)
)
And my products array based on color
Array
(
[0] => Array
(
[product_id] => 54
[model] => 01035
[sku] => 050102/bs
)
[1] => Array
(
[product_id] => 57
[model] => 01038
[sku] => 050104/b
)
[2] => Array
(
[product_id] => 58
[model] => 01039
[sku] => 050106/C
)
)
stdClass Object
(
[10010] => stdClass Object
(
[product_option_id] =>
[name] => option_name_1
[value] => option_name_1-option_name_2-option_name_3:::0
[product_option_value] => stdClass Object
(
[12] => stdClass Object
(
[option_value_id] => 849
[product_option_value_id] =>
[model] => repellendus nemo ve-050102/bs
)
[15] => stdClass Object
(
[option_value_id] => 861
[product_option_value_id] =>
[model] => repellendus nemo ve-050104/b
)
)
)
[10011] => stdClass Object
(
[product_option_id] =>
[name] => option_name_1-option_name_2-option_name_3
[value] => option_name_1-option_name_2-option_name_3:::0-849
[product_option_value] => stdClass Object
(
[13] => stdClass Object
(
[option_value_id] => 2407
[product_option_value_id] =>
[model] => repellendus nemo ve-050102/bs-050102/bs
)
)
)
[10012] => stdClass Object
(
[product_option_id] =>
[name] => option_name_1-option_name_2-option_name_3
[value] => option_name_1-option_name_2:::0-849-2407
[product_option_value] => stdClass Object
(
[14] => stdClass Object
(
[option_value_id] => 2884
[product_option_value_id] =>
[model] => repellendus nemo ve-050102/bs-050102/bs-050102/bs
)
)
)
[10013] => stdClass Object
(
[product_option_id] =>
[name] => option_name_1-option_name_2
[value] => option_name_1-option_name_2:::0-861
[product_option_value] => stdClass Object
(
[16] => stdClass Object
(
[option_value_id] => 2407
[product_option_value_id] =>
[model] => repellendus nemo ve-050104/b-050102/bs
)
)
)
[10014] => stdClass Object
(
[product_option_id] =>
[name] => option_name_1-option_name_2-option_name_3
[value] => option_name_1-option_name_2-option_name_3:::0-861-2407
[product_option_value] => stdClass Object
(
[17] => stdClass Object
(
[option_value_id] => 2884
[model] => repellendus nemo ve-050104/b-050102/bs-050102/bs
)
)
)
)
For the moment I've managed building the "first array that holds the base products" but it doesn't have the name concatenated with the option names
$something = function ($cell, $optionName) {
$cOption = $this->getOptionByName($optionName, $cell);
return [
"product_option_id" => "",
'option_value_id' => 0,
'option_val_id' => $cOption['option_value_id'],
"name" => $cOption['name'],
"value" => $cOption['name'],
"option_id" => $cOption['option_id'],
"required" => 0,
"swatch_image" => 0,
"show_first_option_in_list" => "1",
"product_option_value" => []
];
};
$products = [];
$baseProduct = [];
$i = 1000;
for ($c = 4; $c <= 7; $c++) {
if (!empty($cell[$c])) {
$baseProduct = $another($cell[$c], 'Related Colors');
$baseProduct['name'] = $baseProduct['name'] . ':::0';
$baseProduct['value'] = $baseProduct['value'] . ':::0';
$product = $this->getProductBySku($cell[$c]);
$relatedColors[] = $product;
}
}
$baseProduct['product_option_value'] = $relatedColors;
$products[$i++] = $baseProduct;
And this is a visual representation of how on the front should display if the array was build correctly.
End result
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.
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..
:)
I have this array:
stdClass Object
(
[tid] => 26001835
[vid] => 5
[name] => AppleTV
[description] => My description
[format] => filtered_html
[weight] => 0
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608990
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => Apple-TV.png
[uri] => public://Apple-TV.png
[filemime] => image/png
[filesize] => 2103
[status] => 1
[timestamp] => 1405346182
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.something.com
[format] =>
[safe_value] => http://www.something.com
)
)
)
[field_learn_more] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://something.com/somepage
[format] =>
[safe_value] => http://something.com/somepage
)
)
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => network
)
)
)
)
stdClass Object
(
[tid] => 26001834
[vid] => 5
[name] => Playstation - USA
[description] => My description
[format] => filtered_html
[weight] => 2
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608991
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => ps4network.png
[uri] => public://ps4network.png
[filemime] => image/png
[filesize] => 4566
[status] => 1
[timestamp] => 1405346218
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.somesite.com
[format] =>
[safe_value] => http://somesite.com
)
)
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => blast_areas
)
)
)
)
stdClass Object
(
[tid] => 26001836
[vid] => 5
[name] => Brighthouse Networks
[description] => My description
[format] => filtered_html
[weight] => 3
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608993
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => brighthouse.png
[uri] => public://brighthouse.png
[filemime] => image/png
[filesize] => 8392
[status] => 1
[timestamp] => 1405358781
)
)
)
[field_buy_now_button_link] => Array
(
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => ppv_provider
)
)
)
)
I want to sort the array by the array by the value in field_device_category. Basically, I want to group the results but first I need to make sure all the objects are sorted by field_device_category.
Thanks in advance!
Use usort for this kind of sorting. It uses Quicksort in the background and takes a user-defined function to compare the array elements:
usort($array, "complicatedArrayComparer");
function complicatedArrayComparer($a,$b)
{
if ($a['field_device_category'] == $b['field_device_category']) {
return 0;
}
return ($a['field_device_category'] < $b['field_device_category']) ? -1 : 1;
}
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.