结合多维的价值观

时间:2013-06-19 10:20:42

标签: php jquery

所以..我有这个数组是由serializeArray()从jquery传递到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];
    }
}

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

然后我崩溃重复的值如下:

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
        )
)

这将是这样的,因为内爆也不起作用:

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

我很抱歉,如果之前已经询问过,但是当我搜索所有他们要求的是2个或更多阵列要合并时..

有什么建议吗?或其他相关工作...

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

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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);

答案 2 :(得分: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 )