-1

这是我拥有的数组:

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:

输出数组是一个新数组......

4

4 回答 4

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
于 2013-08-10T11:15:50.137 回答
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);
于 2013-02-18T11:14:53.153 回答
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);
于 2013-02-18T11:37:35.110 回答
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
)
于 2013-02-18T15:02:09.020 回答