doufuhao8085
doufuhao8085
2015-10-17 12:34

将关联数组从mysqli_fetch_all的结果转换为简单的索引数组

已采纳

I have code like this

 $years_of_service = mysqli_query($mysqli, "SELECT YEAR(curdate())-Year_Entry FROM result");

 $result = mysqli_fetch_all($years_of_service,MYSQLI_NUM);

When i try to echo that result to see what happened with that code, i found this kind of array

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 ) )

I want to make it become a simple array containing only its value like

Array(25,25,25,....)

What is the best approach i can use. I've tried to make it simple array with this

$simple = array_values($result);

But it did'nt work. It show the same output with $result. I will use it for map and reduce function to categorize years of service by specific range.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • duanqun9740 duanqun9740 6年前

    The reason this is happening is you're retrieving all rows (outer array) which are being returned as an array of columns (the inner arrays).

    What you need to do is loop through the rows:

    $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];
    }
    

    Your array will now be 'flat':

    $data => [
        0 => 25,
        1 => 24,
        ...
    ];
    

    which will now return what you want when you use array_values:

    $simple = array_values($data); // $simple will now equal Array(25, 24, ...)
    
    点赞 评论 复制链接分享
  • doulangyu9636 doulangyu9636 6年前

    If PHP > 5.5.0 you can simply use array_column like as

    $result = array_column($your_array,0);
    

    or you can simply use call_user_func_array like as

    call_user_func_array('array_merge',$your_array);
    

    Demo

    点赞 评论 复制链接分享
  • douji6667 douji6667 6年前

    You could try something like:

    $simple = array();
    foreach ($result as $value) {
        $simple[] = $value[0];
    }
    
    点赞 评论 复制链接分享

相关推荐