Saving images from an associative array - php

$object = json_decode(file_get_contents('https://api.500px.com/v1/photos/?feature=user&username=nsz&consumer_key=xxx'), true);
This object have the following structure:
Array
(
[0] => Array
(
[id] => 176621
[name] => ***
[description] =>
[times_viewed] => 1411
[rating] => 47.7
[created_at] => 2010-10-08T14:39:43-04:00
[category] => 11
[privacy] =>
[votes_count] => 65
[favorites_count] => 8
[comments_count] => 7
[nsfw] =>
[store_width] => 643
[store_height] => 915
[image_url] => http://pcdn.500px.net/176621/6f2932b14d545f9664e9472a01ae74596f8d4588/2.jpg
What i would like to know is: How to make a foreach loop that saves each file from the [image_url] to my server.
This seems to be working, but i dont know how to make the loop:
copy('http://path/to/image.jpg', 'images/flower.jpg');
Thanks in advance!

Related

Get sql query result group by a column data

I have two table maintenance_owner and maintenance_users
there are my table structure bellow
This is maintenance_users table
and
This is maintenance_owner table
now i can get all user with this code
$this->db->select('mu.*, mo.id_land_owner as mu_owner, mo.group_name, mo.id_mu');
$this->db->from('maintenance_users as mu');
$this->db->join('maintenance_owner as mo','mu.id_maintenance = mo.id_mu');
$this->db->where('mo.id_land_owner', $land_owner_id);
return $this->db->get()->result();
it returns
[0] => stdClass Object
(
[id_maintenance] => 170
[full_name] =>
[username] => abt2050+m3#gmail.com
[password] =>
[token] => 914c001251a1ab018e5eb51923e8f6cc
[mu_owner] => 152
[group_name] => Cleaner
[id_mu] => 170
)
[1] => stdClass Object
(
[id_maintenance] => 176
[full_name] =>
[username] => bii#fatafati.net
[password] =>
[token] => 579faa456520656fcc24be047ca6a3bf
[mu_owner] => 152
[group_name] =>
[id_mu] => 176
)
[2] => stdClass Object
(
[id_maintenance] => 175
[full_name] =>
[username] => iamnow78+m4#gmail.com
[password] =>
[token] => d2f6b180a6a7bb32ecd26ffe0297f8f5
[mu_owner] => 152
[group_name] =>
[id_mu] => 175
)
but i need to get the result like this
[0] => stdClass Object
(
[id] => 30
[id_land_owner] => 152
[group_name] => Cleaner
[group_users] => abt2050+m1#gmail.com,abt2050+m2#gmail.com,abt2050+m3#gmail.com,abt2050+m4#gmail.com
)
[1] => stdClass Object
(
[id] => 29
[id_land_owner] => 152
[group_name] => Gardener
[group_users] => abt2050+a1#gmail.com,abt2050+a2#gmail.com,abt2050+a3#gmail.com
)
need to do this with sql query only
I'm using codeigniter fraework
You can try this as you are using codeigniter
there is a mysql function called GROUP_CONCAT
learn more about it and you will understand
$this->db->select('mo.id, mo.id_land_owner as id_land_owner, mo.group_name, GROUP_CONCAT(mu.username SEPARATOR ",") as group_users', false);
$this->db->from('maintenance_users as mu');
$this->db->join('maintenance_owner as mo','mu.id_maintenance = mo.id_mu');
$this->db->group_by('mo.group_name');
$this->db->where('mo.id_land_owner', 152);
return $this->db->get()->result();

Extract data from Array/Object in PHP?

First off, I'm new to PHP and coding in general, so this might be quite an obvious answer.
I'm currently working with the Strava API, and I'm trying to extract data from an Array/Object which is the result of the following API call:
$recentactivities = $api->get('athlete/activities', array('per_page' => 100));
which returns:
Array (
[1] => stdClass Object (
[id] => XXXX
[resource_state] => 2
[external_id] => XXXX
[upload_id] => XXXX
[athlete] => stdClass Object (
[id] => XXXX
[resource_state] => 1
)
[name] => Let\'s see if I can remember how to do this cycling malarkey...
[distance] => 11858.3
[moving_time] => 1812
[elapsed_time] => 2220
[total_elevation_gain] => 44
[type] => Ride
[start_date] => 2014-07-12T13:48:17Z
[start_date_local] => 2014-07-12T14:48:17Z
[timezone] => (
GMT+00:00
) Europe/London
[start_latlng] => Array (
[0] => XXXX
[1] => XXXX
)
[end_latlng] => Array (
[0] => XXXX
[1] => -XXXX
)
[location_city] => XXXX
[location_state] => England
[location_country] => United Kingdom
[start_latitude] => XXXX
[start_longitude] => XXXXX
[achievement_count] => 4
[kudos_count] => 1
[comment_count] => 0
[athlete_count] => 1
[photo_count] => 0
[map] => stdClass Object (
[id] => a164894160
[summary_polyline] => XXXX
[resource_state] => 2
)
[trainer] =>
[commute] =>
[manual] =>
[private] =>
[flagged] =>
[gear_id] => b739244
[average_speed] => 6.544
[max_speed] => 10.8
[average_cadence] => 55.2
[average_temp] => 29
[average_watts] => 99.3
[kilojoules] => 179.9
[device_watts] =>
[average_heartrate] => 191.2
[max_heartrate] => 200
[truncated] =>
[has_kudoed] =>
)
This repeats for the most recent activities.
I'm attempting to extract average_heartrate, which I can do for a single object using the following:
$recentactivities[1]->average_heartrate;
but I'd like to extract all instances of average_heartrate from the Array. I've tried to use a foreach statement, but to be honest, I have no idea where to start.
Any help would be much appreciated.
It's actually pretty simple you can indeed use a foreach loop:
foreach($myArray as $obj){
//$obj is an object so:
if(isset($obj->average_heartrate)){
echo $obj->average_heartrate;
}
}
With this code you iterate through your array with objects and save the wanted array within an array so you can work further with it.
$heartrateArray = array(); // create a new array
//iterate through it with an foreach
foreach($recentactivities as $activity){
// save the average_heartrate as a value under a new key in the array
$heartrateArray[] = $activity->average_heartrate;
}

how to read a json file containing url and save the url

I have a json file. The json file contains the path of some images. I want to read the url path and save the image in my computer. I was able to read the json file but can't get the image_path object in the json. My json file is of the format:
Array
(
[0] => Array
(
[product] => Array
(
[product_id] => 262
[product_name] => VD0289 CUT OUT BACK DRESS
[product_inventory] => 2
[product_price] => B
[label_name] => 15
[product_status] => A
[images] => Array
(
[0] => Array
(
[image_id] => 935
[image_path] => http://gird.com/images/thumbnails/0/400/500/VD0289_VD0289.jpg
[image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/VD0289_VD0289.jpg
[image_detailed] => http://gird.com/images/detailed/0/VD0289_VD0289.jpg
[image_type] => M
)
[1] => Array
(
[image_id] => 938
[image_path] => http://gird.com/images/thumbnails/0/400/500/VD0289_VD0289_(4).jpg
[image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/VD0289_VD0289_(4).jpg
[image_detailed] => http://gird.com/images/detailed/0/VD0289_VD0289_(4).jpg
[image_type] => A
)
)
[options] => Array
(
)
)
)
[1] => Array
(
[product] => Array
(
[product_id] => 263
[product_name] => Chic Chanel Inspired Dress - Blue
[product_inventory] => 1
[product_price] => O
[label_name] => 9
[product_status] => A
[images] => Array
(
[0] => Array
(
[image_id] => 939
[image_path] => http://gird.com/images/thumbnails/0/400/500/100678-Blue-1.jpg
[image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/100678-Blue-1.jpg
[image_detailed] => http://gird.com/images/detailed/0/100678-Blue-1.jpg
[image_type] => M
)
[1] => Array
(
[image_id] => 942
[image_path] => http://gird.com/images/thumbnails/0/400/500/100678-Blue-4.jpg
[image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/100678-Blue-4.jpg
[image_detailed] => http://gird.com/images/detailed/0/100678-Blue-4.jpg
[image_type] => A
)
..............................
How can I get all the image path and image_detailed from this file?? My php code is:The code is to only get the first image path.
<?php
$file="dw.json";
$json= json_decode(file_get_contents($file),true);
print_r ($json[0]["product"]["images"][0]["image_path"]);
//print_r($json);
?>
Try this out:
foreach($json as $key => $products) {
$product = $products['product'];
foreach($product['images'] as $key => $image) {
$product_images[$product['product_name']][] = $image['image_path'];
}
}
print_r($product_images);
Use a for loop, like this:
foreach ($json[0]["product"]["images"] as $key)
echo $key['image_path'];

Codeigniter: Referencing a specific array index returned in model result

I'm still trying to wrap my head around passing db query results from a model back to controller and finally to a view. I seem to be getting the data to the right place, I'm just not sure how to best access the resulting array of objects in the view.
Specifically, I'm trying to query my db for the most recent 7 distinct dates that someone has submitted a link. I get back an array of dates, and then for each of those dates I do a query for all links submitted on that date and store the results in an array. Then in the view, for each of those distinct dates, I show a header (the date), immediately followed by the links associated with it.
The array that come from my distinct date query ($link_headers):
Array (
[0] => stdClass Object([added_date] => 2011-08-11)
[1] => stdClass Object([added_date] => 2011-05-03)
[2] => stdClass Object([added_date] => 2011-04-21)
[3] => stdClass Object([added_date] => 2011-04-10)
[4] => stdClass Object([added_date] => 2011-03-04)
[5] => stdClass Object([added_date] => 2011-02-28)
[6] => stdClass Object([added_date] => 2011-02-22)
)
The array that comes from my query for actual links submitted ($links_result):
Array
(
[0] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1178
[link_url] => http://www.amazon.com/Silicone-Rubber-CASSETTE-Design-IPHONE/dp/B004YDJWOY
[link_name] => Silicone Skin BLACK CASSETTE TAPE
[link_notes] => iPhone case... probably won't fit in my dock.
[added_date] => 2011-08-11
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[1] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1177
[link_url] => http://snorby.org/
[link_name] => Snorby - Snort front-end
[link_notes] =>
[added_date] => 2011-05-03
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[2] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1176
[link_url] => http://www.nytimes.com/2011/04/17/business/17excerpt.html?_r=4&pagewanted=1&ref=business
[link_name] => Corner Office - The 5 Habits of Highly Effective C.E.O.s
[link_notes] => Sounds a lot like what Nathanial said...
[added_date] => 2011-04-21
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[3] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1175
[link_url] => http://chezlarsson.com/myblog/2010/06/panduro-concrete-challenge-3.html
[link_name] => Concrete book-ends
[link_notes] => Cool look...
[added_date] => 2011-04-10
[flag_new] => 1
[rating] => 4
[public] => 1
)
[1] => stdClass Object
(
[user_id] => 2
[link_id] => 1174
[link_url] => http://themeforest.net/item/reciprocity-photo-blog-gallery/154590
[link_name] => Site Templates - Reciprocity - Photo Blog
[link_notes] =>
[added_date] => 2011-04-10
[flag_new] => 1
[rating] => 5
[public] => 1
)
)
[4] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1173
[link_url] => http://lifehacker.com/#!5771943/the-always-up+to+date-guide-to-jailbreaking-your-ios-device
[link_name] => The Always Up-to-Date Guide to Jailbreaking Your iOS Device
[link_notes] =>
[added_date] => 2011-03-04
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[5] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1172
[link_url] => http://lifehacker.com/#!5754463/how-to-jailbreak-your-ios-421-device
[link_name] => How to Jailbreak Your iOS 4.2.1 Device
[link_notes] =>
[added_date] => 2011-02-28
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[6] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1171
[link_url] => http://www.bitplumber.net/2010/10/a-cassandra-hardware-stack-dell-c1100s-ocz-vertex-2-ssds-with-sandforce-arista-7048s/
[link_name] => A Cassandra Hardware Stack
[link_notes] =>
[added_date] => 2011-02-22
[flag_new] => 1
[rating] => 3
[public] => 1
)
)
)
... all seems fine enough. But my problem comes from my view, where I'm trying to build the HTML as described above. A simplified view of the code I'm trying to get to work is as follows:
foreach ($link_headers as $header) {
echo "INDEX: ". $links_headers .", ADDED DATE: ". $header->added_date ."<BR>";
foreach ($links_result[$link_headers] as $result){
echo $result->added_date ."<BR>";
echo $result->link_name ."<BR><BR>";
}
}
So, I'm trying to use the index of the first one to tell my foreach loop which index of the second array to loop through and get the content. Clearly I'm misusing the $links_result[$link_headers] variable(s) but I left it in to show what I was trying to do.
Any help is very much appreciated!
Michael
I dont use CodeIgniter but whether within th framework or from PHP i would just grab it all in one go and then the issue with the indexes becomes moot:
SELECT * FROM model_table mt WHERE mt.added_date IN (
SELECT DISTINCT md.added_date from model_table md
ORDER BY md.added_date DESC
LIMIT 7
) ORDER BY mt.added_date DESC
That should get you an array of models ordered by date limted to the 7 most recent dates. So then its just a matter of choosing when to display a header:
$current = null;
foreach($links as $link) {
if($link->added_date !== $current) {
// show the header and set current to the current date
$current = $link->added_date;
echo 'HEADER: Added on ' . $current . '<br />';
}
echo $row->added_date ."<BR>";
echo $row->link_name ."<BR><BR>";
}

Stackoverflow API getting answers in array PHP

I'm trying to use the stackoverflow API and I want to get answers of a question in a php array. So far here is my php code:
<?php
//KEY
$string = "key=my_key";
//Call stack API .$string
$stack_url = "compress.zlib://http://api.stackoverflow.com/1.1/questions";
//Get and Store API results into a variable
$result = file_get_contents($stack_url);
$jsonArray = json_decode($result);
print_r($jsonArray);
//echo($jsonArray->questions[0]->question_answers_url);
//var_dump($jsonArray);
?>
I want to store the answers of a question in an array called answers so that I can access them with a for loop.
The answer i get is :
stdClass Object
(
[total] => 2618591
[page] => 1
[pagesize] => 30
[questions] => Array
(
[0] => stdClass Object
(
[tags] => Array
(
[0] => c#
[1] => ssh
[2] => openssh
[3] => rsacryptoserviceprovider
)
[answer_count] => 1
[favorite_count] => 0
[question_timeline_url] => /questions/9164203/timeline
[question_comments_url] => /questions/9164203/comments
[question_answers_url] => /questions/9164203/answers
[question_id] => 9164203
[owner] => stdClass Object
(
[user_id] => 311966
[user_type] => registered
[display_name] => simonc
[reputation] => 301
[email_hash] => 021f3344004f0c886d715314fa02037d
)
[creation_date] => 1328548627
[last_edit_date] => 1328611688
[last_activity_date] => 1328611688
[up_vote_count] => 0
[down_vote_count] => 0
[view_count] => 25
[score] => 0
[community_owned] =>
[title] => Format of PKCS private keys
)
[1] => stdClass Object
(
[tags] => Array
(
[0] => c#
[1] => .net
[2] => combobox
)
[answer_count] => 3
[favorite_count] => 0
[question_timeline_url] => /questions/9174765/timeline
[question_comments_url] => /questions/9174765/comments
[question_answers_url] => /questions/9174765/answers
[question_id] => 9174765
[owner] => stdClass Object
(
[user_id] => 1194399
[user_type] => registered
[display_name] => Goxy
[reputation] => 1
[email_hash] => 5fc8c96b6b85c6339cb9ac4ab60cb247
)
[creation_date] => 1328611202
[last_activity_date] => 1328611686
[up_vote_count] => 0
[down_vote_count] => 0
[view_count] => 15
[score] => 0
[community_owned] =>
[title] => WPF: Bind simple List<myClass> to Combobox
)
....
Not sure exactly which property you want to extract, but I assume it's the 'question_answers_url'.
$answersArray = Array();
for($i=0;$i<count($jsonArray['questions']);$i++){
//assuming it is the 'question_answers_url' property that you want
array_push($answersArray,$jsonArray['questions'][$i]['question_answers_url']);
}
Ought to do it.

Categories