This question already has answers here:
Able to see a variable in print_r()'s output, but not sure how to access it in code
(9 answers)
Closed 8 years ago.
I am a bit of a PHP newbie so this is probably easy but I just cant figure it out. I'm using a script that pulls info from the movie database 'TMDB' using there API.
The following code:
$playing = $tmdb_V3->nowPlayingMovies($page=1);
echo"<pre>";print_r($playing);echo"</pre>";
prints out this info:
Array
(
[dates] => Array
(
[minimum] => 2014-05-20
[maximum] => 2014-06-10
)
[page] => 1
[results] => Array
(
[0] => Array
(
[adult] =>
[backdrop_path] => /aBkkrhQS4rO3u1OTahywtSXu3It.jpg
[id] => 127585
[original_title] => X-Men: Days of Future Past
[release_date] => 2014-05-23
[poster_path] => /qKkFk9HELmABpcPoc1HHZGIxQ5a.jpg
[popularity] => 232.77188788256
[title] => X-Men: Days of Future Past
[vote_average] => 7.2
[vote_count] => 178
)
[1] => Array
(
[adult] =>
[backdrop_path] => /9bd6IFBMwSOINrEW8VIBxaS4cd9.jpg
[id] => 102651
[original_title] => Maleficent
[release_date] => 2014-05-30
[poster_path] => /1cFVCUYKSBuEUDoVftKvqcfuIgc.jpg
[popularity] => 57.46008792307
[title] => Maleficent
[vote_average] => 7.3
[vote_count] => 42
)
[2] => Array
(
[adult] =>
[backdrop_path] => /7mgKeg18Qml5nJQa56RBZO7dIu0.jpg
[id] => 137113
[original_title] => Edge of Tomorrow
[release_date] => 2014-06-06
[poster_path] => /zMP1PEvwH8Uy6jiIG1Qzp0svS1q.jpg
[popularity] => 56.13626675
[title] => Edge of Tomorrow
[vote_average] => 7.5
[vote_count] => 29
)
and so on.
What I would like to do is extract the information and save it as its own variable. so for example:
[0] => Array
(
[adult] =>
[backdrop_path] => /aBkkrhQS4rO3u1OTahywtSXu3It.jpg
[id] => 127585
[original_title] => X-Men: Days of Future Past
[release_date] => 2014-05-23
[poster_path] => /qKkFk9HELmABpcPoc1HHZGIxQ5a.jpg
[popularity] => 232.77188788256
[title] => X-Men: Days of Future Past
[vote_average] => 7.2
[vote_count] => 178
Would be the first movie so I can then save its [id] as $id, the [original_title] as $title and so on, so I can call on those variables any where on the page.
Is this possible?
I basically want to get all the info from the database and display it in a PHP file but not by printing the array
any info will be very much appreciated.
You can target the movies individually with the array as it currently is
$playing['results'][0]['title'] // would give you the first movie title
$playing['results'][1]['title'] // would give you the second movie title
Access your elements like this:
foreach($playing["results"] as $array){
$your_variable=$array["id"]; // and so on...
}
Related
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
Array ( [err] => [num] => 1 [result] => Array ( [0] => Array ( [0] => 85 [proj_id] => 85 [1] => Automatic Mobile Alert System For Lethal Gas Detection [proj_item_title] => Automatic Mobile Alert System For Lethal Gas Detection [2] => GSM PROJECTS [proj_category] => GSM PROJECTS [3] => 5000 [actual_price] => 5000 [4] => 4000 [discount_price] => 4000 [5] => 5 [rating] => 5 [6] => automatic-mobile-alert-system-for-lethal-gas-detection [friendly_url] => automatic-mobile-alert-system-for-lethal-gas-detection [7] => gsnrtmccfoqqkgb8qs2ni5hud36c032j [session_id] => gsnrtmccfoqqkgb8qs2ni5hud36c032j [8] => 85 [9] => 2017-01-20 [date_added] => 2017-01-20 [10] => 1 [qty] => 1 ) ) )
I need to access elements from the array "result". How can I access them. Please help me.
echo $array['result'][0][0]
Will return 85
$result = $array['result'][0]
Will give you a nice way to save the Result array into another Array.
So you can do
$result[0] //Returns 85 again
Your $data['result'] is an array so you can simply loop over the elements
foreach($data['result'] as $result) {
var_dump($result);
echo $result[0];
echo $result['proj_item_title'];
echo $result['proj_id']; //etc
}
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I just initialize an array $exp_arr with some values. The print_r($exp_arr[0]) results the following values
Array
(
[0] => Array
(
[id] => 40
[email] => shishir012010#gmail.com
[company_name] => Yahoo
[title] => Developer2
[location] => Noida
[description] =>
[smonth] => January
[syear] => 2012
[emonth] => December
[eyear] => 2015
[status] => 0
)
)
After that echo $exp_arr[0]['location'] does not print anything.
I also tried var_dump($exp_arr[0]['location']) and it results in output of NULL.
I can't track the problem. I have put the echo statement just after the print_r statement just like that
echo "<pre>";
print_r($exp_arr[0]);
echo $exp_arr[0]['location'];
var_dump($exp_arr[0]['location']);
To explain Karma Pals point, your array is already one deep in a 0 index, so if you do this:
print_r($exp_arr[0][0]['location'])
You will get your location out. You need to check if your array structure is correct based on that.
Your vardump of just $exp_arr will look like something this:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 40
[email] => shishir012010#gmail.com
[company_name] => Yahoo
[title] => Developer2
[location] => Noida
[description] =>
[smonth] => January
[syear] => 2012
[emonth] => December
[eyear] => 2015
[status] => 0
)
)
)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I fetch an array from API using
<?php
$json = json_decode(file_get_contents("API_URL"), TRUE);
echo '<pre>';
print_r($json);
?>
I got that result like that
Array
(
[deals] => Array
(
[0] => Array
(
[activeDa
te] => 1430332361000
[bogo] =>
[categories] => Array
(
[0] => Array
(
[id] => 29057
[name] => Bath & Body
)
[1] => Array
(
[id] => 20733
[name] => Cosmetics
)
[2] => Array
(
[id] => 29190
[name] => Skin Care
)
[3] => Array
(
[id] => 20856
[name] => Fragrances
)
[4] => Array
(
[id] => 29059
[name] => Beauty & Personal Care
)
)
[clearance] =>
[couponCode] => HAPPYMOM
[dealImageUrl] => http://cdn.savings.com/logo/1737578.png
[dealUrl] => http://www.savings.com/m/p/19561077/8306099/c?afsrc=1&up=2015-05-01-05-15
[description] => Go through this link to get Assorted Spring Getaway tote for only $20 on orders $40 or more, save 80%. Restrictions may apply. Limited time offer only or when supplies run out.
[discount] => 1
[exclusive] =>
[freeShipping] =>
[homePageStaffPick] =>
[id] => 3862713
[lastUpdated] => 1430332361000
[merchantDisplayUrl] => http://www.bathandbodyworks.com
[merchantId] => 236514
[merchantImageUrl] => http://cdn.savings.com/logo/1737578.png
[merchantName] => Bath and Body Works
[merchantPageStaffPick] =>
[merchantScore] => 17
[merchantUrl] => http://www.savings.com/m/p/19561077/1742990/c?afsrc=1
[minimumSpend] => 0.00
[mobileMonetized] =>
[monetized] => 1
[printable] =>
[promotion] => 80% Off
[rebate] =>
[scope] => SITE_WIDE
[score] => 579
[siteUrls] => Array
(
[0] => http://www.bathandbodyworks.com
)
[startDate] => 1430290800000
[tip] =>
[title] => Get 80% off Assorted Spring Getaway Tote on Orders Over $40 - Only $20
[validated] =>
[voteDown] => 0
[voteUp] => 0
)
My question is how i get the value of [deals][categories][0][name] ?
I want to store value of categories name.
You have your answer in your question itelf. just a little modifiction needed. Please try this:-
echo $yourarrayname['deals'][0]['categories'][0]['name'];
Note:-since category is on the Zero th index of deals. So put zero index before category index.
You already have your answer. The only thing you need to do is make them literal strings and take the first element in the deals array. So:
echo $json['deals'][0]['categories'][0]['name']
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>";
}
$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!