Solved

php loop through 2D array

Posted on 2010-11-13
2
353 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 30

Expert Comment

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 count occurrences of each item in an array.

743 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

12 Experts available now in Live!

Get 1:1 Help Now