Solved

php loop through 2D array

Posted on 2010-11-13
2
369 Views
Last Modified: 2012-05-10
Hello,
I access the following values like this.
    $result->{'HistoricalPricesResult'}->HistoricalPricesResult[0]->Price
    $result->{'HistoricalPricesResult'}->HistoricalPricesResult[0]->Low
    //next row
    $result->{'HistoricalPricesResult'}->HistoricalPricesResult[1]->Price
    $result->{'HistoricalPricesResult'}->HistoricalPricesResult[1]->Low

Open in new window

However I need to consolidate this to

    
    $values[0][price]
    $values[0][low]
    $values[1][price]
    $values[1][low]

Open in new window

2 other strange things.  The values are strings and I need them to be decimals(2 decimal points) and also the min and the max for price and low accross all the rows
0
Comment
Question by:casit
2 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 34130343
Can you post the array and the code your using to loop? Thanks
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34131194
We need to see a little bit more of the code to help you.  What you have here is Object-Oriented Notation (and it looks a little confused):

$result->{'HistoricalPricesResult'}->HistoricalPricesResult[0]->Price
$result->{'HistoricalPricesResult'}->HistoricalPricesResult[0]->Low

What you would probably want is an iterator like foreach() if you really need to reassign these variables to new variables.  But with PHP's loose data typing, you might be able to simply use the values "as is" or multiply each by 1.0 to get a floating point value.  PHP has no such thing as a "decimal" value.  You can sort() arrays to find the min and max values - after sorting you can use reset()  and end() to find these values after a sort.
http://us2.php.net/manual/en/array.sorting.php
http://us2.php.net/manual/en/function.reset.php
http://us2.php.net/manual/en/function.end.php
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now