解析从数据库返回的多级关联数组

时间:2014-01-08 17:44:35

标签: php mysql arrays

我试图从mysql查询返回的数组中返回一个特定的值。我不确定它会被称为什么,我猜它是一个多维关联数组,请纠正我,如果我错了。

我的PHP脚本

$query = "SELECT * FROM flws ORDER BY day LIMIT 1,5";

$query_run = mysql_query($query);

if($query_run){
$flws = array();

while($row = mysql_fetch_assoc($query_run)){
    $flws[] = $row;
}

var_dump($flws);
//echo $flws[1];

 } else{echo "broken query";} 

$ flws的var转储返回:

array(5){[0] => array(8){[“day”] => string(1)“2”[“openday”] => string(4)“5.47”[“highday”] => string(4)“5.54”[“lowday”] => string(4)“5.41”[“closeday”] => string(4)“5.48”[“volumeday”] => string(5)“76700”[“adjday”] => NULL [“deviationday”] => NULL} [1] => array(8){[“day”] => string(1)“3”[“openday”] => string(4)“5.38”[“highday”] => string(4)“5.47”[“lowday”] => string(4)“5.29”[“closeday”] => string(4)“5.44”[“volumeday”] => string(5)“68000”[“adjday”] => NULL [“deviationday”] => NULL} [2] => array(8){[“day”] => string(1)“4”[“openday”] => string(4)“5.50”[“highday”] => string(4)“5.50”[“lowday”] => string(4)“5.36”[“closeday”] => string(4)“5.39”[“volumeday”] => string(6)“247500”[“adjday”] => NULL [“deviationday”] => NULL} [3] => array(8){[“day”] => string(1)“5”[“openday”] => string(4)“5.10”[“highday”] => string(4)“5.46”[“lowday”] => string(4)“4.99”[“closeday”] => string(4)“5.44”[“volumeday”] => string(6)“472300”[“adjday”] => NULL [“deviationday”] => NULL} [4] => array(8){[“day”] => string(1)“6”[“openday”] => string(4)“5.10”[“highday”] => string(4)“5.15”[“lowday”] => string(4)“5.00”[“closeday”] => string(4)“5.10”[“volumeday”] => string(6)“115200”[“adjday”] => NULL [“deviationday”] => NULL}

我不确定如何从结果数组中返回特定值。

2 个答案:

答案 0 :(得分:2)

您将获得多个highday值。我只是打印它,您将highday中的所有$arrHighDay值作为数组:

$query = "SELECT * FROM flws ORDER BY day LIMIT 1,5";

$query_run = mysql_query($query);

if($query_run){
   $flws = array();

   while($row = mysql_fetch_assoc($query_run)){
      echo $row['highday']; //print value
      $arrHighDay[] = $row['highday']; // assign in an array
   }

print_r($arrHighDay);// you can return this array 

else{
      echo "broken query";
} 

在您退回的页面中,您可以获得以下所有日期:

foreach($arrHighDay as $a){
   echo $a['highday'];
}

答案 1 :(得分:1)

访问这样的数组值:

echo $flws['highday'];

它会回显highday的价值。

while($row = mysql_fetch_assoc($query_run)){
    $flws[] = $row['highday'];
}

现在,数组$flws包含所有列highday值。