?
Solved

php loop through 2D array

Posted on 2010-11-13
2
Medium Priority
?
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

765 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