如何将相同名称与其在数组中的值组合在一起

时间:2014-01-16 08:10:59

标签: php arrays

嗨我有这样的数组

   Array
(
 [0] => stdClass Object
    (
        [name] => text_input
        [value] => kalpit
    )

  [1] => stdClass Object
    (
        [name] => wpc_chkbox[]
        [value] => Option two
    )

[2] => stdClass Object
    (
        [name] => wpc_chkboxasdf[]
        [value] => Option one
    )

[3] => stdClass Object
    (
        [name] => wpc_chkboxasdf[]
        [value] => Option two
    )

[4] => stdClass Object
    (
        [name] => wpc_inline_chkbox[]
        [value] => 1
    )

[5] => stdClass Object
    (
        [name] => wpc_inline_chkbox[]
        [value] => 2
    )

[6] => stdClass Object
    (
        [name] => wpc_inline_chkbox[]
        [value] => 3
    )

[7] => stdClass Object
    (
        [name] => wpc_radios
        [value] => Option one
    )

 )

现在我的问题是如何在onc位置组合相同的名称值,在上面的数组中我有 wpc_inline_checkbox [] 重复3次所以我想要它...我可以使用array_uniqe( )但我想要其他重复索引的价值......

  [4] => stdClass Object
    (
        [name] => wpc_inline_chkbox[]
        [value] => 1,2,3
    )

任何人都可以帮我解决这个问题

提前致谢

4 个答案:

答案 0 :(得分:0)

对不起,你的案子并不普通,所以你需要手工完成

$properArray = array();
foreach ($originArray as $value) {
    if ( ! isset($properArray[$value['name']])) {
        $properArray[$value['name']] = array(
            'name' = $value['name'],
            'value' = ''
        );
    }
    if (isset($properArray[$value['name']]['value'])) {
         $properArray[$value['name']]['value'] = $properArray[$value['name']]['value'] . ',' .$value['value']; //better to use array in this place
    } else {
         $properArray[$value['name']]['value'] = $value['value']
    }

}

$originArray = array_values($properArray);

问候,

答案 1 :(得分:0)

这假设名称/值都是字符串

<?php 

  $objects;    // This is your array
  $sorted = array();  // This is the sorted array

  // Loop over your array
  foreach($objects as $object)
  {
    // Check if object exists in sorted array
    if( array_key_exists($object->name, $sorted) )
    {
      // Object with this name is already in sorted array simply add to it
      $obj = $sorted[$object->name];

      // Update the values
      $obj->value = $obj->value . ',' . $object->value;

    } else {

      // Object is not in the sorted array add it now
      $sorted[$object->name] = $object;
    }
  }

?>

答案 2 :(得分:0)

你有一个对象数组。您将不得不循环并分配它们。像

这样的东西
$newArray = [];
foreach ($array as $obj) {
  if (!isset($newArray[$obj->name])) {
    $newArray[$obj->name] = $obj;
  } else {
    $newArray[$obj->name]->value .= ",{$obj->value}";
  }
}

新数组应该看起来像

["wpc_inline_chkbox[]"] => stdClass Object
    (
        [name] => wpc_inline_chkbox[]
        [value] => "1,2,3"
    )

但是你应该将[value]从字符串更改为数组。

答案 3 :(得分:0)

##使用此##

$arr = array(
        array(
            'name' => 'hi'
            ,'value' => 1
        )
        ,array(
            'name' => 'hi'
        ,'value' => 2
        )
        , array(
            'name' => 'hi2'
        ,'value' => 1
        )
        , array(
            'name' => 'hi4'
        ,'value' => 1
        )
        ,array(
            'name' => 'hi0'
        ,'value' => 4
        )
        , array(
            'name' => 'hi0'
        ,'value' => 3
        )
        , array(
            'name' => 'hi1'
        ,'value' => 10
        )
    );
    print_r($arr);
    $arrTracker = array();
    for ($i=0; $i <sizeof($arr) ;$i++) {
        for($j = $i+1; $j<sizeof($arr);$j++){
            if($arr[$i]['name'] == $arr[$j]['name']){
                $arr[$i]['value'] .= ','.$arr[$j]['value'];
                $arrTracker[$j] = $j;
            }
        }
    }
    // if you want to unset duplicate name and corresponding value, use below forloop, otherwise it's unnecessary
    foreach($arrTracker as $tracker){
        unset($arr[$tracker]);
    }
    $arr = array_values($arr);
    print_r($arr);