php循环遍历2D数组

时间:2010-11-14 05:23:39

标签: php

我可以像这样访问以下值。

$result->{'HistoricalPricesResult'}->HistoricalPricesResult[0]->Price
$result->{'HistoricalPricesResult'}->HistoricalPricesResult[0]->Low
//next row
$result->{'HistoricalPricesResult'}->HistoricalPricesResult[1]->Price
$result->{'HistoricalPricesResult'}->HistoricalPricesResult[1]->Low

但是我需要将其合并到

$values[0][price]
$values[0][low]
$values[1][price]
$values[1][low]
其他两件奇怪的事情。值是字符串,我需要它们是小数(2个小数点),还有价格的最小值和最大值以及所有行的低值

3 个答案:

答案 0 :(得分:0)

构建值数组的明显方法是:

$values = array();
for($i = 0; $i < some_maximum_value; $i++) {
   $values[$i] = array(
      'price' => $result->{'HistoricalPricesResult'}->HistoricalPricesResult[$i]->Price,
      'low' => $result->{'HistoricalPricesResult'}->HistoricalPricesResult[$i]->Low,
   );
}

答案 1 :(得分:0)

TADAAAAAA !!!!

$values = array();
foreach($result->{'HistoricalPricesResult'}->HistoricalPricesResult as $key => $obj){
    $values[$key]['price'] = $obj->Price;
    $values[$key]['low'] = $obj->low;
}

答案 2 :(得分:0)

$myVals = array();
foreach ($result->{'HistoricalPricesResult'}->HistoricalPricesResult as $key => $v)
{
   $myVals[$key]['price'] = 1.0 * $c->Price;  //hoping string has 2 after the decimal
   $myVals[$key]['low'] = 1.0 * $c->Low
}

尝试自己计算出最大/最小值

查看foreach循环和字符串/浮点转换

http://us2.php.net/manual/en/control-structures.foreach.php
http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion