简体   繁体   English

如何从数组内部的数组中提取特定的键值

[英]How to pull specific key values from array inside array

I have array output like below, 我有如下的数组输出,

Array ( [0] => Array ( [file_name] => test.pdf 
                       [file_type] => application/pdf 
                       [file_path] => /Applications/AMPPS/www/testing/uploads/attachments/2/
[1] => Array ( [file_name] => test1.pdf 
                       [file_type] => application/pdf 
                       [file_path] => /Applications/AMPPS/www/testing/uploads/attachments/2/ )

How can i pull a new array like below 我如何拉一个新的数组如下

Array( [0] => test.pdf [1] => test1.pdf)

Background, 背景,

I am doing multiple file upload using Codeigniter, my files are uploading and getting return data array, i want only file names to be send back to my view, so need to pull file names of files which are uploaded, 我正在使用Codeigniter进行多个文件上传,我的文件正在上传并获取返回数据数组,我只希望将文件名发送回我的视图,因此需要提取已上传文件的文件名,

Any suggestions, hints? 有什么建议,提示吗?

Thanks, 谢谢,

Use array_column() like, 使用array_column()喜欢,

$new_array = array_column ($your_array, 'file_name');

If using PHP version < 5.5, refer this 如果使用PHP版本<5.5,请参考

<?php
$arrResult = array();
foreach ($arrGiven as $key => $value) {
    $arrResult[] = $value['file_name'];
}

$arrGiven is the first array from which you want to extract the data. $ arrGiven是要从中提取数据的第一个数组。 $arrResult will contain the data like you wanted. $ arrResult将包含所需的数据。

$new = array();
foreach($old as $entrie){
  $new[] = $entrie['file_name'];
}

This will go through all the entreis of the old array, and put the file_name of each in a new array. 这将遍历旧数组的所有实体,并将每个文件的file_name放入新数组中。

Simply do this 只需这样做

$files = array();
foreach ($array as $key => $value) {
    $files[] = $array[$key]['file_name'];
}
print_r($files);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM