0
$series = array();  

while($row = mysql_fetch_assoc($result)) { 
            $series[$row["data_id"]][] = $row; 
        } 

print_ra on的输出$series为两个示例系列:

大批 (

[1] => 数组 ( [0] => 数组 ( [id] => 1 [data_id] => 1 [time_id] => 1 [data] => 1 ) [1] => 数组 ( [id] = > 2 [data_id] => 1 [time_id] => 2 [data] => 3) )

[2] => 数组 ( [0] => 数组 ( [id] => 6 [data_id] => 2 [time_id] => 1 [data] => 7 ) [1] => 数组 ( [id] = > 7 [data_id] => 2 [time_id] => 2 [data] => 4 ) )

我的问题:如何取消设置多维数组,使其仅包含[data]而不包含其他键?我仍然想$series包含[1][2]但我不希望各自的子数组包含除[data].

事实上,由于我正在减少子数组以包含一个键,我真的很想完全摆脱子数组,以便我有两个数组:

$series[1] = array(1,3) 和 $series[2] = array(7,4)

4

2 回答 2

2

尝试这个 :

$series = array();  

while($row = mysql_fetch_assoc($result)) { 
            $series[$row["data_id"]][] = $row['data']; 
        } 
于 2013-06-09T16:02:54.413 回答
0

我认为你可以在你的数组中循环并构建一个只保留data细节的新数组

$array = array ('1' => array ( '0' => array ( 'id' => 1, 'data_id' => 1, 'time_id' => 1, 'data' => 1 ), '1' => array ( 'id' => 2, 'data_id' => 1, 'time_id' => 2, 'data' => 3 ), ),
'2' => array ( '0' => array ( 'id' => 6, 'data_id' => 2, 'time_id' => 1, 'data' => 7 ), '1' => array ( 'id' => 7, 'data_id' => 2, 'time_id' => 2, 'data' => 4 ) ));

$i= 0;
$n= 0;
$series = array();
foreach($array as $dato)
{
    $series[$i] = array();
    foreach($dato as $data)
    {
        foreach($data as $key => $value)
        {
            if($key == 'data')
            {
                $series[$i][$n] = $value;
                $n++;
            }
        }
    }
    $n = 0;
    $i++;
}
var_dump($series);

这将输出

array (size=2)
  0 => 
    array (size=2)
      0 => int 1
      1 => int 3
  1 => 
    array (size=2)
      0 => int 7
      1 => int 4    

现场演示

于 2013-06-09T16:03:12.077 回答