$series = array();
while($row = mysql_fetch_assoc($result)) {
$series[$row["data_id"]][] = $row;
}
print_r
$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)
答案 0 :(得分:2)
试试这个:
$series = array();
while($row = mysql_fetch_assoc($result)) {
$series[$row["data_id"]][] = $row['data'];
}
答案 1 :(得分: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