doudi8829 2015-05-06 07:22
浏览 276

如何将2D数组转换为1D数组?

I want to convert my 2D array into 1D array. When I do var_dump($image_name_db); It shows :

array(2) {
  [0]=>
  array(1) {
    ["image"]=>
    string(7) "pic.PNG"
  }
  [1]=>
  array(1) {
    ["image"]=>
    string(14) "abouttown3.jpg"
  }
}

Now how can I convert It into 1D array. As I want to compare two arrays. One array is 1D and other array is 2D, that is why i want 2D array in 1D. So i need both of them in 1D to compare easily. I am using codeigniter.

  • 写回答

5条回答 默认 最新

  • dongma7796 2015-05-06 07:26
    关注

    You need to traverse through the array and store images in to a 1D array.

    <?php
    $arr = array();
    $arr[0]['image'] = 'pic.PNG';
    $arr[1]['image'] = 'abouttown3.jpg';
    $images = array();
    if (! empty($arr)) {
      foreach ($arr as $row) {
        $images[] = $row['image'];
      }
    }
    
    echo "<br/> Existing";
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
    
    echo "<br/> New";
    echo '<pre>';
    print_r($images);
    echo '</pre>';
    

    Working demo:

    评论

报告相同问题?