0

所以..我有这个数组,它是由 jquery 的 serializeArray() 生成的,传递给一个 php 页面..,

这是我在 php 中存储在 $data 中的输出数组:

Array
(
    [0] => Array
        (
            [name] => name
            [value] => sample text
        )

    [1] => Array
        (
            [name] => bdate
            [value] => sample date
        )

    [2] => Array
        (
            [name] => addr
            [value] => sample addr
        )

    [3] => Array
        (
            [name] => demo_des
            [value] => A
        )

    [4] => Array
        (
            [name] => demo_des
            [value] => B
        )
    [5] => Array
        (
            [name] => demo_des
            [value] => C
        )
)

这就是 id 所做的:

for($i=0;$i<count($data);$i++){ //loop in $data
    if($data[$i][name]==$data[$i+1][name]||$data[$i][name]==$data[$i-1][name]){
        $impldval[] .= $data[$i][value];
        $impldkey[] .= $data[$i][name];
    }else{
        $arr[$data[$i][name]] .= $data[$i][value];
    }
}

基本上我遍历数据并检查 data[num][name] 是否等于下一个或上一个,如果是则将其存储到数组中,如果不将其存储在不同的数组中..

然后我像这样内爆重复的值:

if(is_array($impldkey)&is_array($impldval)){$arr[$impldkey[0]] .= implode(',',$impldval);}

如果我们打印 $arr 它将如下所示:

Array
(
    [name] => sample text
    [bdate] => sample date
    [addr] => sample addr
    [demo_des] => A,B,C
)

这是正确的,但问题是如果具有相同名称的数组不按顺序排列意味着要检查的增量将不起作用:

Array
(
    [0] => Array
        (
            [name] => name
            [value] => sample text
        )

    [1] => Array
        (
            [name] => demo_des
            [value] => A
        )

    [2] => Array
        (
            [name] => addr
            [value] => sample addr
        )

    [3] => Array
        (
            [name] => demo_des
            [value] => B
        )

    [4] => Array
        (
            [name] => bdate
            [value] => sample date
        )
    [5] => Array
        (
            [name] => demo_des
            [value] => C
        )
)

这看起来像这样,因为 implode 也不起作用:

Array
(
    [name] => sample text
    [bdate] => sample date
    [addr] => sample addr
    [demo_des] => ABC
)

很抱歉,如果以前有人问过这个问题,但是当我搜索时,他们要求的只是 2 个或更多要合并的数组。

有什么建议么?或解决此问题的其他工作...

编辑:好的,这就是我需要做的,我需要将多维数组从 serializeArray() 转换为单个数组并组合重复的名称并内爆值,就像上面的示例一样,我循环遍历数组并添加它们到“$arr”,但如果它有重复,那么我将它存储在不同的数组中,然后将它内爆并添加到“$arr”。

4

3 回答 3

0
$arrayDefaults = array(
    'a' => '1',
    'b' => '10',
    'c' => '10',
    'd' => '10',
    'e' => '10',
);

$arrayPost = array(
    array('name' =>'a', 'value'=>'1'),
    array('name' =>'b', 'value'=>'2'),
    array('name' =>'c', 'value'=>'3'),
    array('name' =>'d', 'value'=>'4'),
    array('name' =>'e', 'value'=>'5'),
    array('name' =>'f', 'value'=>'6')
);

$arrayNew = array();
foreach($arrayPost as $v){
    if(isset($v['name'], $v['value'])){
        //don't forget to sanitize values
        $value = filter_var($v['value'], FILTER_SANITIZE_SPECIAL_CHARS);
        if(isset($arrayNew[$v['name']])){
            $arrayNew[$v['name']] .= ',' . $value;
        }else{
            $arrayNew[$v['name']] = $value;
        }
    }
}

//remove unknown keys
$arrayMatchKeys = array_intersect_key($arrayNew, $arrayDefaults);

//get only changed fields
$arrayUpdate = array_diff_assoc($arrayMatchKeys, $arrayDefaults);

结果:

array (size=4)
  'b' => string '2' (length=1)
  'c' => string '3' (length=1)
  'd' => string '4' (length=1)
  'e' => string '5' (length=1)
于 2013-06-19T11:02:00.880 回答
0

在功能中,请var_export用于显示有问题的数组

$rowsGrouped = array();
$rows = array(
    array(
        'name' => 'first',
        'value' => 1
    ),
    array(
        'name' => 'second',
        'value' => 2
    ),
    array(
        'name' => 'first',
        'value' => 3
    ),
);
foreach ($rows as $row) {
    $name = $row['name'];
    if (isset($rowsGrouped[$name]) === false) {
        $rowsGrouped[$name] = $row;
    } else {
        $rowsGrouped[$name]['value'] .= sprintf(',%s', $row['value']);
    }
}
$rowsGrouped = array_values($rowsGrouped);
于 2013-06-19T11:06:37.180 回答
0
$data =array (array('name'=>'name','value'=>'sample text'),array('name'=>'bdate','value'=>'sample date'),array('name'=>'addr','value'=>'sample addr'),array('name'=>'demo_des','value'=>'A'),array('name'=>'demo_des','value'=>'B'),array('name'=>'demo_des','value'=>'C')   );

foreach ($data as $k=>$v){

    $name=$v['name'];
    $value=$v['value'];

    if($out_put_array["$name"])
    { 
       $out_put_array["$name"].=','.$value;
    }
    else
    {
       $out_put_array["$name"]=$value;
    }

    }

print_r($out_put_array);
Array ( [name] => sample text [bdate] => sample date [addr] => sample addr [demo_des] => A,B,C )
于 2013-06-19T11:45:37.560 回答