从数组中的一个键获取所有值

时间:2014-08-09 19:47:36

标签: php arrays codeigniter multidimensional-array image-upload

我想将所有“file_name”值都放到一个变量中,以便能够将它作为逗号分隔的字符串保存到我的数据库中。

    array (size=13)
  0 => 
    array (size=14)
      'file_name' => string '__0000132.jpg' (length=13)
      'file_type' => string 'image/jpeg' (length=10)
      'file_path' => string 'C:/wamp/www/mou/uploads/items/' (length=30)
      'full_path' => string 'C:/wamp/www/mou/uploads/items/__0000132.jpg' (length=43)
      'raw_name' => string '__0000132' (length=9)
      'orig_name' => string '__00001.jpg' (length=11)
      'client_name' => string '__00001.jpg' (length=11)
      'file_ext' => string '.jpg' (length=4)
      'file_size' => float 2.16
      'is_image' => boolean true
      'image_width' => int 300
      'image_height' => int 300
      'image_type' => string 'jpeg' (length=4)
      'image_size_str' => string 'width="300" height="300"' (length=24)
  1 => 
    array (size=14)
      'file_name' => string '0132.jpg' (length=8)
      'file_type' => string 'image/jpeg' (length=10)
      'file_path' => string 'C:/wamp/www/mou/uploads/items/' (length=30)
      'full_path' => string 'C:/wamp/www/mou/uploads/items/0132.jpg' (length=38)
      'raw_name' => string '0132' (length=4)
      'orig_name' => string '01.jpg' (length=6)
      'client_name' => string '01.jpg' (length=6)
      'file_ext' => string '.jpg' (length=4)
      'file_size' => float 271.2
      'is_image' => boolean true
      'image_width' => int 580
      'image_height' => int 714
      'image_type' => string 'jpeg' (length=4)
      'image_size_str' => string 'width="580" height="714"' (length=24)
  2 => 
    array (size=14)
      'file_name' => string 'CM_logo32.png' (length=13)
      'file_type' => string 'image/png' (length=9)
      'file_path' => string 'C:/wamp/www/mou/uploads/items/' (length=30)
      'full_path' => string 'C:/wamp/www/mou/uploads/items/CM_logo32.png' (length=43)
      'raw_name' => string 'CM_logo32' (length=9)
      'orig_name' => string 'CM_logo.png' (length=11)
      'client_name' => string 'CM_logo.png' (length=11)
      'file_ext' => string '.png' (length=4)
      'file_size' => float 24.61
      'is_image' => boolean true
      'image_width' => int 393
      'image_height' => int 404
      'image_type' => string 'png' (length=3)
      'image_size_str' => string 'width="393" height="404"' (length=24)
  3 => 
    array (size=14)
      'file_name' => string 'download32.png' (length=14)
      'file_type' => string 'image/png' (length=9)
      'file_path' => string 'C:/wamp/www/mou/uploads/items/' (length=30)
      'full_path' => string 'C:/wamp/www/mou/uploads/items/download32.png' (length=44)
      'raw_name' => string 'download32' (length=10)
      'orig_name' => string 'download.png' (length=12)
      'client_name' => string 'download.png' (length=12)
      'file_ext' => string '.png' (length=4)
      'file_size' => float 33.18
      'is_image' => boolean true
      'image_width' => int 160
      'image_height' => int 120
      'image_type' => string 'png' (length=3)
      'image_size_str' => string 'width="160" height="120"' (length=24)

我正在使用codeigniter,这是我的图书馆从$ this-> upload-> get_multi_upload_data();图书馆 - >> https://github.com/stvnthomas/CodeIgniter-Multi-Upload

任何帮助都会非常适合

2 个答案:

答案 0 :(得分:0)

如果你有PHP> = 5.5,你可以使用array_column()

$results = array_column($myArray, 'file_name');

对于早期版本的PHP,您可以使用

$results = array_map(
    function ($value) {
        return $value['file_name'];
    }
    $myArray
);

但是在数据库中保存为逗号分隔的字符串并不是一个好建议:它使它实际上无法搜索(你不能使用索引进行搜索,所以它是一个完整的扫描)它变得非常很难修改

答案 1 :(得分:0)

我发现它是什么。

基本上缺少一个逗号。

 $fileData =$this->upload->get_multi_upload_data();
    $results = array_map(
        function ($fileData) {
            return $fileData['file_name'];
        },
        $fileData
    );

从Codeigniter Library https://github.com/stvnthomas/CodeIgniter-Multi-Upload

返回一个文件名数组