Solved

php loop through 2D array

Posted on 2010-11-13
2
388 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 110

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Persistent Connections 10 52
How to hide contact form only if no validation errors 4 62
How do I add date to text file name 15 26
Sending JSON Via POST In PHP 7 20
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
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 dynamically set the form action using jQuery.

737 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