Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 管理mysql获取阵列_Php_Arrays - Fatal编程技术网

Php 管理mysql获取阵列

Php 管理mysql获取阵列,php,arrays,Php,Arrays,我有一个mysql表,其中包含我需要以更基本的格式使用的数据 有ID号、日期、油、气和电值 我可以做一个mysql_fetch_数组,它会返回我需要的所有值,但我需要以一种与javascript图表一起使用的格式查看它们。它们需要采用以下格式: $data = array(array("6/22/2009",425.32),array("6/8/2009",424.84),array("5/26/2009",417.23),array("5/11/2009",390)); 如你所见,我需要日期

我有一个mysql表,其中包含我需要以更基本的格式使用的数据

有ID号、日期、油、气和电值

我可以做一个mysql_fetch_数组,它会返回我需要的所有值,但我需要以一种与javascript图表一起使用的格式查看它们。它们需要采用以下格式:

$data = array(array("6/22/2009",425.32),array("6/8/2009",424.84),array("5/26/2009",417.23),array("5/11/2009",390));
如你所见,我需要日期和油值。然后我需要创建另一个数组,其中包含日期和电气值,最后是数据和气体值


我不确定如何从mysql_fetch_数组中获取数据并将其添加到数组中,如上图所示。有人能帮忙吗?

您可以通过
[]
语法附加它:

$data = array('oil' => array(), 'gas' => array(), 'electric' => array());
while ($row = mysql_fetch_assoc($result)) {
    $data['oil'][$row['date']] = $row['oil'];
}
这假设所选日期也是唯一的



顺便说一句,您应该避免在新代码中使用
mysql.*
函数。

您可以通过
[]
语法附加它:

$data = array('oil' => array(), 'gas' => array(), 'electric' => array());
while ($row = mysql_fetch_assoc($result)) {
    $data['oil'][$row['date']] = $row['oil'];
}
这假设所选日期也是唯一的


顺便说一下,您应该避免在新代码中使用
mysql.*
函数。

试试这个

$finalArray=array();
while ($row = mysql_fetch_array($result)) 
{ 
    $data=array();
    $data[] = $row['date'];
    $data[] = $row['oil'];
    $finalArray[]=$data;
}
print_r($finalArray); //This will print your values in required fromat.
试试这个

$finalArray=array();
while ($row = mysql_fetch_array($result)) 
{ 
    $data=array();
    $data[] = $row['date'];
    $data[] = $row['oil'];
    $finalArray[]=$data;
}
print_r($finalArray); //This will print your values in required fromat.

谢谢你的帮助。我已将我的代码修改为:

$oil = array('oil' => array());
$result=mysql_query(" SELECT * FROM energyprices");
while ($row = mysql_fetch_assoc($result)) {
$oil[$row['pDate']]['oil'] = $row['oil'];
}
当我打印数据时,它如下所示:

Array ( [oil] => Array ( ) [2004-01-01] => Array ( [oil] => 29.73 ) [2004-01-02] => Array ( [oil] => 29.73 ));
我需要的格式是:

$oil = array(
array("6/22/2009",425.32),
array("6/8/2009",424.84),
array("5/26/2009",417.23));

谢谢大家!

谢谢你的帮助。我已将我的代码修改为:

$oil = array('oil' => array());
$result=mysql_query(" SELECT * FROM energyprices");
while ($row = mysql_fetch_assoc($result)) {
$oil[$row['pDate']]['oil'] = $row['oil'];
}
当我打印数据时,它如下所示:

Array ( [oil] => Array ( ) [2004-01-01] => Array ( [oil] => 29.73 ) [2004-01-02] => Array ( [oil] => 29.73 ));
我需要的格式是:

$oil = array(
array("6/22/2009",425.32),
array("6/8/2009",424.84),
array("5/26/2009",417.23));

谢谢大家!

您需要使用“数字数组”:

    $result = mysql_query(" SELECT date, oil FROM energyprices");
    $i = 0;
    while($row = mysql_fetch_array($result, MYSQL_NUM))
    {
         $oil[$i] = array($row[0], $row[1]);
         $i++;
    }

如果我没有理解你的问题,请纠正我,我将相应地编辑我的答案。

你需要使用“数字数组”:

    $result = mysql_query(" SELECT date, oil FROM energyprices");
    $i = 0;
    while($row = mysql_fetch_array($result, MYSQL_NUM))
    {
         $oil[$i] = array($row[0], $row[1]);
         $i++;
    }

如果我不理解你的问题,请纠正我,我将相应地编辑我的答案。

你能发布你的代码吗?你能发布你的代码吗?