rjcode
rjcode

Reputation: 1349

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,

Any suggestions, hints?

Thanks,

Upvotes: 1

Views: 117

Answers (4)

viral
viral

Reputation: 3743

Use array_column() like,

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

If using PHP version < 5.5, refer this

Upvotes: 4

Faiz Rasool
Faiz Rasool

Reputation: 1379

Simply do this

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

Upvotes: 0

DocRattie
DocRattie

Reputation: 1423

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

Upvotes: 0

Arun Saragadam
Arun Saragadam

Reputation: 75

<?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.

Upvotes: 0

Related Questions