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,

1
  • sorry there wan typo in key Commented Jun 16, 2015 at 9:09

4 Answers 4

4

Use array_column() like,

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

If using PHP version < 5.5, refer this

Sign up to request clarification or add additional context in comments.

7 Comments

@Uchiha it will help the OP. May be That is a typo.
@Uchiha sorry there wan typo in key
@Viral, how to pull only array values without key? means array('test.pdf', 'test1.pdf')?
Did you tried the code it'll do exactly what you need @rjcode
@Uchiha, yes that worked but i feel i need to remove keys as well, to process that further is easy.
|
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.

Comments

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.

Comments

0

Simply do this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.