Accessing values from inner array [duplicate] - php

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
}

Related

Get values from Multilevel Array in PHP [duplicate]

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

array does not echo correc value [duplicate]

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
)
)
)

Sort php associative array [duplicate]

This question already has answers here:
Sorting an associative array in PHP [duplicate]
(5 answers)
Closed 8 years ago.
I am trying to sort an array by miles.
$i = 0;
foreach($results as $key => $value)
{
echo $results['miles'][$i] . "<br />";
echo $results['postcode'][$i] . "<br />";
echo $results['id'][$i] . "<br />";
echo $results['description'][$i] . "<br />";
echo $results['title'][$i] . "<br />";
echo $results['thumbnail'][$i] . "<br />";
$i++;
}
AS you can see I'm having 6 different keys here. I want to order everything by $results['miles'] in ASCENDING order.
This is my array structure:
Array ( [miles] => Array ( [0] => 0 [1] => 0 [2] => 14 [3] => 8 [4] => 8 [5] => 0 [6] => 8 [7] => 14 ) [title] => Array ( [0] => Stunning 1 Bedroom Apartment With Concierge [1] => Big Double Inc Bills+ Balcony+Free Parking [2] => Large, Sunny Flat In Willesden [3] => Brewhouse Yard EC1V [4] => Stunning 2 Double Bed Flat - City [5] => Room To Let £575 Pm Bills Included [6] => All Bills Inclusive | Gorgeous 1 Bed In The City [7] => Large Double Room In Zone 2 (Kensal Green 2 Mins) ) [id] => Array ( [0] => 187 [1] => 176 [2] => 186 [3] => 178 [4] => 179 [5] => 177 [6] => 183 [7] => 182 ) [thumbnail] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => ) [postcode] => Array ( [0] => IG11 [1] => ig11 [2] => NW10 [3] => EC1 [4] => ec1 [5] => ig11 [6] => ec1 [7] => NW10 ) )
Any help would be appreciated!
If you can, the best way is to sort the data before it comes in to your script (if it's coming from SQL then order it there).
If you need to do it in PHP though you can use the uasort function, which allows you to define your own comparison:
function compareMiles($a, $b) {
if ($a['miles'] == $b['miles']) {
return 0;
}
return ($a['miles'] < $b['miles']) ? -1 : 1;
}
uasort($results, 'compareMiles');
You can use usort() function to sort it.
Use asort() to sort an array by maintain index association.
Refer this link for details and example

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>";
}

Iterate through array and get key and value [duplicate]

This question already has answers here:
How to loop through an associative array and get the key?
(12 answers)
Closed 8 years ago.
I need to iterate through a dynamic array. The array will look something like the following:
Array
(
[2010091907] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
[away] => Array
(
[score] => Array
(
[1] => 0
[2] => 7
[3] => 0
[4] => 0
[5] => 0
[T] => 7
)
[abbr] => ARZ
[to] => 2
)
[weather] =>
[media] => Array
(
[tv] => FOX
[sat] => 709
[sathd] => 709
[radio] => Array
(
[home] => 153
[away] => 90
)
)
[bp] => 13
[yl] =>
[qtr] => Final
[down] => 0
[togo] => 0
[clock] => 00:26
[posteam] => ARZ
[note] =>
[redzone] =>
[stadium] => Georgia Dome
)
I need it to be dynamic and for testing purposes, I need to be able to call it via:
echo "Key: $key; Value: $value<br />\n";
I'm going to later take this information and place it into a mysql database, but for now, I need to brush up on arrays and figure out how to format the data.
Any help is appreciated.
I´d go for a recursive function: If a value is an array, call it again and otherwise display the key / value pair.
Something like (not tested):
function display_array($your_array)
{
foreach ($your_array as $key => $value)
{
if is_array($value)
{
display_array($value);
}
else
{
echo "Key: $key; Value: $value<br />\n";
}
}
}
display_array($some_array);

Categories