我有这样的代码
$years_of_service = mysqli_query($mysqli, "SELECT YEAR(curdate())-Year_Entry FROM result");
$result = mysqli_fetch_all($years_of_service,MYSQLI_NUM);
当我尝试回显该结果以查看该代码发生了什么时,我发现了这种数组
Array ( [0] => Array ( [0] => 25 ) [1] => Array ( [0] => 25 ) [2] => Array ( [0] => 25 ) [3] => Array ( [0] => 25 ) [4] => Array ( [0] => 25 ) [5] => Array ( [0] => 14 ) [6] => Array ( [0] => 14 ) [7] => Array ( [0] => 14 ) [8] => Array ( [0] => 14 ) [9] => Array ( [0] => 14 ) [10] => Array ( [0] => 12 ) [11] => Array ( [0] => 12 ) [12] => Array ( [0] => 12 ) [13] => Array ( [0] => 12 ) [14] => Array ( [0] => 12 ) [15] => Array ( [0] => 11 ) [16] => Array ( [0] => 11 ) [17] => Array ( [0] => 11 ) [18] => Array ( [0] => 11 ) [19] => Array ( [0] => 11 ) [20] => Array ( [0] => 10 ) [21] => Array ( [0] => 10 ) [22] => Array ( [0] => 10 ) [23] => Array ( [0] => 10 ) [24] => Array ( [0] => 10 ) [25] => Array ( [0] => 9 ) [26] => Array ( [0] => 9 ) [27] => Array ( [0] => 9 ) [28] => Array ( [0] => 9 ) [29] => Array ( [0] => 8 ) [30] => Array ( [0] => 8 ) [31] => Array ( [0] => 8 ) [32] => Array ( [0] => 8 ) [33] => Array ( [0] => 7 ) [34] => Array ( [0] => 7 ) [35] => Array ( [0] => 7 ) [36] => Array ( [0] => 7 ) [37] => Array ( [0] => 6 ) [38] => Array ( [0] => 6 ) [39] => Array ( [0] => 6 ) [40] => Array ( [0] => 6 ) [41] => Array ( [0] => 6 ) [42] => Array ( [0] => 6 ) [43] => Array ( [0] => 6 ) [44] => Array ( [0] => 6 ) [45] => Array ( [0] => 6 ) [46] => Array ( [0] => 5 ) [47] => Array ( [0] => 5 ) [48] => Array ( [0] => 5 ) [49] => Array ( [0] => 5 ) [50] => Array ( [0] => 5 ) [51] => Array ( [0] => 5 ) [52] => Array ( [0] => 5 ) [53] => Array ( [0] => 5 ) [54] => Array ( [0] => 5 ) [55] => Array ( [0] => 4 ) [56] => Array ( [0] => 4 ) [57] => Array ( [0] => 4 ) [58] => Array ( [0] => 4 ) [59] => Array ( [0] => 4 ) [60] => Array ( [0] => 4 ) [61] => Array ( [0] => 4 ) [62] => Array ( [0] => 4 ) [63] => Array ( [0] => 4 ) [64] => Array ( [0] => 4 ) [65] => Array ( [0] => 4 ) [66] => Array ( [0] => 4 ) [67] => Array ( [0] => 4 ) [68] => Array ( [0] => 4 ) [69] => Array ( [0] => 3 ) [70] => Array ( [0] => 3 ) [71] => Array ( [0] => 3 ) [72] => Array ( [0] => 3 ) [73] => Array ( [0] => 3 ) [74] => Array ( [0] => 3 ) [75] => Array ( [0] => 3 ) [76] => Array ( [0] => 3 ) [77] => Array ( [0] => 3 ) [78] => Array ( [0] => 2 ) [79] => Array ( [0] => 2 ) [80] => Array ( [0] => 2 ) [81] => Array ( [0] => 2 ) [82] => Array ( [0] => 1 ) [83] => Array ( [0] => 1 ) [84] => Array ( [0] => 1 ) [85] => Array ( [0] => 1 ) [86] => Array ( [0] => 0 ) [87] => Array ( [0] => 0 ) [88] => Array ( [0] => 0 ) [89] => Array ( [0] => 0 ) )
我想让它变成一个只包含其值的简单数组,如
Array(25,25,25,....)
我可以使用的最佳方法是什么。我已经尝试用这个
制作简单的数组$simple = array_values($result);
但它确实有效。它显示与$ result相同的输出。我将它用于map和reduce功能,按特定范围对服务年份进行分类。
答案 0 :(得分:3)
如果 PHP > 5.5.0
,您只需使用array_column
就像
$result = array_column($your_array,0);
或者您可以像{/ p>一样使用call_user_func_array
call_user_func_array('array_merge',$your_array);
答案 1 :(得分:1)
发生这种情况的原因是您要检索作为列数组(内部数组)返回的所有行(外部数组)。
您需要做的是遍历行:
$years_of_service = mysqli_query($mysqli, "SELECT YEAR(curdate())-Year_Entry FROM result");
$result = mysqli_fetch_all($work_period,MYSQLI_NUM);
$data = [];
foreach ($result as $row) {
$data[] = $row[0];
}
您的阵列现在将是' flat':
$data => [
0 => 25,
1 => 24,
...
];
现在会在您使用array_values
时返回您想要的内容:
$simple = array_values($data); // $simple will now equal Array(25, 24, ...)
答案 2 :(得分:0)
您可以尝试以下方式:
$simple = array();
foreach ($result as $value) {
$simple[] = $value[0];
}