这是我拥有的数组:
Array(
[0] => textfield_1
[1] => ssss
[2] => checkboxgroup_1
[3] => Option 1
[4] => checkboxgroup_1
[5] => Option 2
[6] => checkboxgroup_1
[7] => Option 3
[8] => radiobutton_1
[9] => Option 1
[10] => radiobutton_1
[11] => Option 2
[12] => radiobutton_1
[13] => Option 3
[14] => dropdown_1
[15] => Option 1
[16] => dropdown_1
[17] => Option 2
[18] => dropdown_1
[19] => Option 3
)
我希望将其显示如下:
Array(
[0] => textfield_1|ssss
[1] => checkboxgroup_1|Option 1,Option 2,Option 3
[2] => radiobutton_1|Option 1,Option 2,Option 3
[3] => dropdown_1|Option 1,Option 2,Option 3
)
有人可以提出任何建议吗?
Note:
输出数组是一个新的数组....
答案 0 :(得分:1)
以下给出了一个我认为比您要求的更容易使用的数组:
$arr = array_chunk($arr, 2);
$out = array();
foreach ($arr as $key => $value){
$out[$value[0]][] = $value[1];
}
print_r($out);
输出:
Array
(
[textfield_1] => Array
(
[0] => ssss
)
[checkboxgroup_1] => Array
(
[0] => Option 1
[1] => Option 2
[2] => Option 3
)
[radiobutton_1] => Array
(
[0] => Option 1
[1] => Option 2
[2] => Option 3
)
[dropdown_1] => Array
(
[0] => Option 1
[1] => Option 2
[2] => Option 3
)
)
如果您需要在问题中显示数据,可以执行以下操作:
foreach ($out as $key => $value){
echo $key."|".implode(',', $value)."\n";
}
输出:
textfield_1|ssss
checkboxgroup_1|Option 1,Option 2,Option 3
radiobutton_1|Option 1,Option 2,Option 3
答案 1 :(得分:0)
这是我快速写的一些代码......
$input = array(
'textfield_1',
'ssss',
'checkboxgroup_1',
'Option 1',
'checkboxgroup_1',
'Option 2',
'checkboxgroup_1',
'Option 3',
'radiobutton_1',
'Option 1',
'radiobutton_1',
'Option 2',
'radiobutton_1',
'Option 3',
'dropdown_1',
'Option 1',
'dropdown_1',
'Option 2',
'dropdown_1',
'Option 3');
// Assumes input array is ordered key, value, key, ...
function parseToFormattedArray($input)
{
$intermediate = array();
$lastKey = '';
// Loop through all the input values
for($i = 0; $i < count($input); ++$i)
{
if(($i % 2) == 0)
{
// Remember the key for next iteration
$lastKey = $input[$i];
}
else
{
if(isset($intermediate[$lastKey]))
{
// This key already exists, so append the value to the existing values
$intermediate[$lastKey] .= ",{$input[$i]}";
}
else
{
// This key doesn't exist yet, so make it
$intermediate[$lastKey] = "{$lastKey}|{$input[$i]}";
}
}
}
// Drop the keys and only return the values.
return array_values($intermediate);
}
$output = parseToFormattedArray($input);
print_r($output);
答案 2 :(得分:0)
作为问题的解决方案,请参阅下面提到的代码段
$a=array('textfield_1',
'ssss',
'checkboxgroup_1',
'Option 1',
'checkboxgroup_1',
'Option 2',
'checkboxgroup_1',
'Option 3',
'radiobutton_1',
'Option 1',
'radiobutton_1',
'Option 2',
'radiobutton_1',
'Option 3',
'dropdown_1',
'Option 1',
'dropdown_1',
'Option 2',
'dropdown_1',
'Option 3'
);
$keys=array();
$values=array();
$res=array();
foreach($a as $key=>$value)
{
if(($key %2==0 || $key==0))
{
array_push($keys,$value);
}
else
{
array_push($values,$value);
}
}
$x=array();
for($i=0;$i<count($keys);$i++)
{
$x[$keys[$i]][]=$values[$i];
}
foreach($x as $key=>$value)
{
array_push($res,$key.'|'.join(',',$value));
}
echo '<pre>';
print_r($res);
答案 3 :(得分:0)
试试这个:
$a = array(
'textfield_1',
'ssss',
'checkboxgroup_1',
'Option 1',
'checkboxgroup_1',
'Option 2',
'checkboxgroup_1',
'Option 3',
'radiobutton_1',
'Option 1',
'radiobutton_1',
'Option 2',
'radiobutton_1',
'Option 3',
'dropdown_1',
'Option 1',
'dropdown_1',
'Option 2',
'dropdown_1',
'Option 3'
);
foreach($a as $k => $v)
{
if(($k%2==0||$k==0))
{
$arr[$v][] = $a[$k+1];
}
}
$kk = 0;
foreach($arr as $k => $v)
{
$arr2[$kk] = $k.'|'.implode('|',$v);
$kk++;
}
print_r($arr2);
Array
(
[0] => textfield_1|ssss
[1] => checkboxgroup_1|Option 1|Option 2|Option 3
[2] => radiobutton_1|Option 1|Option 2|Option 3
[3] => dropdown_1|Option 1|Option 2|Option 3
)