i want echo only value name no all.
my array:
$file_list[] = array(
'name' => $CI->upload->file_name,
'file' => $CI->upload->upload_path.$CI->upload->file_name,
'size' => $CI->upload->file_size,
'ext' => $CI->upload->file_ext,
'image_type' => $imageVar->image_type,
'height' => $imageVar->height,
'width' => $imageVar->width
);
}
my foreach:
foreach($upload_data as $file) {
echo '<li><ul>';
foreach ($file as $item => $value) {
echo '<li>'.$item.': '.$value.'</li>';
}
echo '</ul></li>';
}
output now:
- name: Chrysanthemum19.jpgfile: D:/xampp/htdocs/Siran-mehdi/uploads/Chrysanthemum19开发者_运维百科.jpg size: 858.78 ext: .jpg image_type: jpeg height: 768 width: 1024
- name: Desert19.jpg file: D:/xampp/htdocs/Siran-mehdi/uploads/Desert19.jpg size: 826.11 ext: .jpg image_type: jpeg height: 768 width: 1024
i want this output:
Chrysanthemum19.jpg, Desert19.jpg
see you full class and Controller
class Multi_upload(libraries) CI_ControllerWith respect
$names = array();
foreach ($file_list as $file) {
$names[] = $file['name'];
}
echo implode(',', $names);
Using PHP5.3 you can compact it a little bit more
echo implode(',', array_map(function ($file) {
return $file['name'];
}, $file_list);
But there is no semantic difference between both.
You're printing all file information data. You want to print only name of it. Here:
foreach($upload_data as $file) {
echo '<li>' . $file->name . '</li>';
}
Try
foreach($upload_data as $file) {
echo '<li><ul>';
foreach ($file as $item => $value) {
if ($item == 'name'){
echo '<li>'.$item.': '.$value.'</li>';
}
}
echo '</ul></li>';
}
Tjeu
First you need to find the part of your loop that outputs the filename:
echo '<li>'.$item.': '.$value.'</li>';
You only want the value to be displayed when $item
is equal to 'name'. You can express that in code like this:
$file['name']
Then, eliminate all the parts of your code that do not contribute to the desired output. Your new code can look like this:
$output = array();
foreach($upload_data as $file) {
$output[] = $file['name'];
}
echo implode( ',' $output );
Use:
foreach($upload_data as $file) {
echo '<li><ul>',$file['name'],'</ul></li>';
}
try something like this :
foreach($upload_data as $file) {
foreach ($file as $item) {
$return[] = $file['name'];
}
}
echo implode(",",$return);
$to_store_in_db = serialize($return);
Adding serialize
精彩评论