[英]How to serialize more than one fields into one variable in php
我得到兩個字段作為數組圖像及其顏色,HTML是:
<div class="form-group">
<input type="file" name="images[source][]" class="form-control input-lg">
<input type="text" name="images[color][]" class="form-control input-lg">
</div>
PHP是
$images=array();
if(isset($_FILES['images']) && sizeof($_FILES['images']['source']) > 0)
{
foreach($_FILES['images']['source'] as $index=>$source)
{
if(!empty($source) && !empty($_POST['images']['color'][$index]))
{
$images[]=array('source'=>$source,'color'=>$_POST['images']['color'][$index]);
}
}
if(sizeof($images) > 0)
{
$data['images']=$N['images']=serialize($images);
}
}
但是,當單擊“提交”按鈕時,除了圖像顏色顯示之外,圖像源沒有任何結果。任何幫助將是感激的。
嗨,伙計,您缺少$_FILES
對象中的名稱鍵。 您還需要檢查一下。 這是更新的代碼。
<?php
$images=array();
if(isset($_FILES['images']) && sizeof($_FILES['images']['name']['source']) > 0)
// name is present before source, you need to add that part
{
foreach($_FILES['images']['name']['source'] as $index=>$source)
{
if(!empty($source) && !empty($_POST['images']['color'][$index]))
{
$images[]=array('source'=>$source,'color'=>$_POST['images']['color'][$index]);
}
}
if(sizeof($images) > 0)
{
$data['images']=$N['images']=serialize($images);
}
} ?>
希望這可以幫助 !
感謝您的幫助,我的代碼現在可以使用HTML:
<input type="file" name="images[]"> <input type="text" name="color[]">
PHP:
$array_serialize = array();
if (!empty($_FILES['image'])){
$files = $_FILES['image'];
$images = $files['name'];
foreach($images as $index=>$image)
{
if(!empty($image) && !empty($_POST['color'][$index]))
{
$array_serialize[]=array('image'=>$image,'color'=>$_POST['color'][$index]);
}
}
$N['images'] = serialize($array_serialize);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.