使用foreach语句填充下拉列表-它显示为单个字符串

时间:2019-07-15 21:43:51

标签: php

在数组上使用foreach语句填充下拉列表时,它将单个项目显示为字符串。但是,它可以使用此语法[“ test1”,“ test2”]

正确填充列表
foreach($dataNew[$i]['message'] as $x => $item){
     $myMessage[]='"'.$item['Lots'].'"';
}

$lots=[implode (',', $myMessage)];//does not work
//$lots=['4342355555555@1', '32335455@5'];//works fine
$dataNew[$i]=['Lots'=>[$lots]]; 

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这里是corrext重叠的一个示例,假设您的问题的某些细节目前还不清楚。

假设:$dataNew参数在[$i]['message']处包含一个字符串数组,其中包含很多,用逗号分隔并来自某个统计过程。

输出应为二维数组,该数组将一次迭代的所有批次合并到一个列表中。因此,reslut是一个批次列表的列表(foreach $i)。

$currentLotsList = $dataNew[$i]['message'];

//iterating over the lots of the current sample - which are a chain of strings seperated by commas
foreach($currentLotsList as $csvLots){ 
    //getting the lots into an array
    $currentLotsArray = explode(",", $csvLots); 

    //adding this list to the major list of $i, 
    if(!isset($dataNew[$i]['Lots']) ){

        //createing the array if not set now.
        $dataNew[$i]['Lots'] = [];
    }

     $dataNew[$i]['Lots'] = array_merge($dataNew[$i]['Lots'], $currentLotsArray); 
}