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
)
)
)
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 8 days ago.
I have an array that I have run json_decode on it...
How can I echo the category_name Hoops from $myarray?
The following is the output of
echo"<pre>;
print_r($myarray);
echo"</pre>;
Array
(
[Althetic] => Array
(
[0] => Array
(
[category_id] => 1
[category_name] => Balls
[parent_id] => 0
)
[1] => Array
(
[category_id] => 2
[category_name] => Hoops
[parent_id] => 0
)
[2] => Array
(
[category_id] => 3
[category_name] => Strings
[parent_id] => 0
)
)
[rawRequest] => Hello world
[hasError] => 1
[errorMessage] => OK
)
I tried
echo $myarray[Althetic][1]->[category_name];
Without any results :(
T
This is a multidimensional array, -> is used to access elements of object.
$myarray['Althetic'][1]['category_name'];
This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 5 years ago.
I have an array:
print_r($response);
will show:
Response: Array
(
[errors] => false
[return] => Array
(
[price_recurring] => 0
[country_code] => NL
[invoicemethod] => instant
[product_id] => 0
[affiliate] =>
[number_of_periods] => 1
[description] => Betaling voor eventid 274
[period_duration] => 1 month
[date] => 29/11/2017 19:42
[order_quantity] => 1
[vat] => 21
[amount_total] => 4599
[total_paused_days] => 0
[custom] => WEB1511980957x194237
[emailaddress] => ik#ik.nl
[amount_affiliate_initial] => 0
[total] => 45,99
[current_status] => completed
[amount_affiliate_recurring] => 0
[amount_total_affiliate] => 0
[id] => 1790226
[price_initial] => 4599
[current_number_of_periods] => 1
[firstname] =>
)
)
How can i extract the value 'custom'?
I've tried:
$response = array_shift($response);
echo $response['custom'];
but it will show nothing. What am i doing wrong here? I know i am close.
$response is an array with two keys errors and return. $response['return'] in turn, is an array with several keys, including custom. Therefore, to access custom, you need to reference $response['return']['custom']
you have an array inside an array
$response["return"]["custom"]
also try to var_dump (or print_r depends on what you prefer) instead of echoing when you try to navigate
var_dump( $response["custom"]);
would tell you a lot more than echoing null, which prints nothing
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:
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...
}
This question already has answers here:
Sorting multidim array: prioritize if column contains substring, then order by a second column
(4 answers)
Sort multidimensional array by multiple columns
(8 answers)
Closed 9 years ago.
I have this Array
Array (
[0] => Array
(
[id] => 61
[testo] => articolo di maggio
[data] => 2013-05-03
[orario] => 00:00:00
[nome_files] => fabmad_1920x1200.jpg
[pubblicato] => 1
)
[1] => Array
(
[id] => 58
[testo] =>
[data] => 2013-06-03
[orario] => 00:00:00
[nome_files] => 20130603100647_da_installare.rtf
[pubblicato] => 1
)
[2] => Array
(
[id] => 59
[testo] => Demo
[data] => 2013-06-03
[orario] => 00:00:00
[nome_files] => eye_drops_water_2.jpg
[pubblicato] => 1
)
)
I want to sort it by "data".
I want to display the "data" and for each data the elements...
Try the ksort function or the array-multisort
Check the php documentation: http://php.net/manual/en/function.ksort.php
http://php.net/manual/en/function.array-multisort.php