如何以多维数组的形式显示数据库值

时间:2017-07-27 14:06:52

标签: php arrays multidimensional-array

我想以这种方式显示两个数据库值

array(
array(0.05, 0.95),
array(0.1, 0.9),
array(0.2, 0.8),
array(0.25, 0.75),
array(0.45, 0.55),
array(0.5, 0.5),
array(0.55, 0.45),
array(0.85, 0.15),
array(0.9, 0.1),
array(0.95, 0.05)
 );

但我有以下数据

array(76.8736, 1, 76.8736, 3, 869.69500946137, 2, 869.69500946137, 5, 431, 6, 23.55, 2, 869.69500946137, 3, 869.69500946137, 1, 869.69500946137, 30, 567, 36, 33, 45, 456, 15, 562, 3, 441, 17)

我的代码在

之下
<?php

 include 'linker.php';


 $statement = $conn->prepare("SELECT * FROM android_data");
 $statement->execute();
 $array1 = [];

 foreach( $statement -> get_result() as $row ){

     array_push($array1, $row['distance']);
     array_push($array1, $row['speed']);
 }
  $arr ='array('.implode(', ',$array1).')';

    echo $arr; 
 ?>

请有人帮助我。感谢

1 个答案:

答案 0 :(得分:0)

您应该创建一个包含值的新数组,并将其推送到主数组中,而不是将所有值直接推送到主数组中:

foreach( $statement -> get_result() as $row ){
    $array1[] = [
        $row['distance'],
        $row['speed']
    ];
}