1

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,

Any suggestions, hints?

Thanks,

rjcode
  • 1,193
  • 1
  • 25
  • 56

4 Answers4

4

Use array_column() like,

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

If using PHP version < 5.5, refer this

Community
  • 1
  • 1
viral
  • 3,724
  • 1
  • 18
  • 32
0
<?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. $arrResult will contain the data like you wanted.

0
$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.

DocRattie
  • 1,392
  • 2
  • 13
  • 27
0

Simply do this

$files = array();
foreach ($array as $key => $value) {
    $files[] = $array[$key]['file_name'];
}
print_r($files);
Faiz Rasool
  • 1,379
  • 1
  • 12
  • 20