如果数组ID相同,则追加/分组到数组

时间:2013-01-24 07:45:17

标签: php arrays

如何通过数组中的某些值对数组元素进行分组?

例如,考虑以下数组数组。我想按其ID

对数组进行分组/连接/追加
 $array['key1'] => Array (
 [0] => Array(
  'id' => 567,
  'value' => 780
 ),
 [1] => Array(
  'id' => 676,
  'value' => 743
 ),
 [2] => Array(
  'id' => 676,
  'value' => 721
 ),
 [3] => Array(
  'id' => 234,
  'value' => 766
 ),
 [4] => Array(
  'id' => 234,
  'value' => 780
 )
)

期望的输出:

$array['key1'] => Array (
 [0] => Array(
  'id' => 567,
  'value' => 780
 ),
 [1] => Array(
  'id' => 676,
  'value' => Array(
    [0] => 743,
    [1] => 721
  )
 ),
 [2] => Array(
  'id' => 234,
  'value' =>  Array(
    [0] => 766,
    [1] => 680
  )
 ),
)

我该怎么做?

4 个答案:

答案 0 :(得分:2)

当有一个值时,您确定要将值作为整数吗?当有更多值时,您确定要将数组作为整数吗?

<?php
$array = array(
    array('id' => 567, 'value' => 780),
    array('id' => 676, 'value' => 743),
    array('id' => 676, 'value' => 721),
    array('id' => 234, 'value' => 766),
    array('id' => 234, 'value' => 680)
);

foreach ($array as $item) {
    $result[$item['id']][] = $item['value'];
}

foreach ($result as $id => $value) {
    if (count($value) > 1) {
        $output[] = array(
            'id' => $id,
            'value' => $value
        );
    } else {
        $output[] = array(
            'id' => $id,
            'value' => $value[0]
        );
    }
}

echo '<pre>';
print_r($output);
echo '</pre>';
?>

如果不是

<?php
$array = array(
    array('id' => 567, 'value' => 780),
    array('id' => 676, 'value' => 743),
    array('id' => 676, 'value' => 721),
    array('id' => 234, 'value' => 766),
    array('id' => 234, 'value' => 680)
);

foreach ($array as $item) {
    $result[$item['id']][] = $item['value'];
}

foreach ($result as $id => $value) {
    $output[] = array(
        'id' => $id,
        'value' => $value
    );
}

echo '<pre>';
print_r($output);
echo '</pre>';
?>

答案 1 :(得分:0)

在那里,所有的工作都为你完成。这有多容易!

//create the array as you have now
$array[0] = ['id'=>567, 'value'=>780];
$array[1] = ['id'=>676, 'value'=>743];
$array[2] = ['id'=>676, 'value'=>721];
$array[3] = ['id'=>234, 'value'=>766];
$array[4] = ['id'=>234, 'value'=>780];

print_r($array);
print chr(10).chr(10);

//create a new array with the values combined on key
$concat = array();
foreach($array as $val) {
  $i = $val['id'];
  $v = $val['value'];

  if (!is_array($concat[$i]))
    $concat[$i] = array();

  $concat[$i][] = $v;
}

print_r($concat);
print chr(10).chr(10);

//create a new array to show the data as you want.
$newarray = array();
foreach($concat as $key=>$val) {
  $t = array();
  $t['id'] = $key;

  if (count($val)==1)
    $t['value'] = $val[0];
  else {
    $t['value'] = array();
    foreach($val as $v)
      $t['value'][] = $v;
  } 

  $newarray[] = $t;
}

print_r($newarray);
print chr(10).chr(10);

结果:

Array
(
    [0] => Array
        (
            [id] => 567
            [value] => 780
        )

    [1] => Array
        (
            [id] => 676
            [value] => 743
        )

    [2] => Array
        (
            [id] => 676
            [value] => 721
        )

    [3] => Array
        (
            [id] => 234
            [value] => 766
        )

    [4] => Array
        (
            [id] => 234
            [value] => 780
        )

)


Array
(
    [567] => Array
        (
            [0] => 780
        )

    [676] => Array
        (
            [0] => 743
            [1] => 721
        )

    [234] => Array
        (
            [0] => 766
            [1] => 780
        )

)


Array
(
    [0] => Array
        (
            [id] => 567
            [value] => 780
        )

    [1] => Array
        (
            [id] => 676
            [value] => Array
                (
                    [0] => 743
                    [1] => 721
                )

        )

    [2] => Array
        (
            [id] => 234
            [value] => Array
                (
                    [0] => 766
                    [1] => 780
                )

        )

)

答案 2 :(得分:0)

尝试这个

$array['key1'] = array(
    0=>array('id'=>567, 'value'=>780),
    1=>array('id'=>676, 'value'=>743),
    2=>array('id'=>676, 'value'=>721),
    3=>array('id'=>234, 'value'=>766),
    4=>array('id'=>234, 'value'=>780)
    );

foreach($array['key1'] as $subarray){
    $group_id = $subarray['id'];

    if(!isset($return[$group_id]))
        $return[$group_id] = $subarray;
    else{
        if(is_array($return[$group_id]['value']))
            array_push($return[$group_id]['value'], $subarray['value']);
        else 
            $return[$group_id]['value'] = array($subarray['value'], $return[$group_id]['value']);
    }
}

// reset grouping keys into 0,1...
$return = array_values($return);
print_r($return);

答案 3 :(得分:0)

<?php
$arr['key1']    = array(
    array(
        'id'    => 567, 
        'value' => 780, 
    ), 
    array(
        'id'    => 676, 
        'value' => 743, 
    ), 
    array(
        'id'    => 676, 
        'value' => 721, 
    ), 
    array(
        'id'    => 234, 
        'value' => 766, 
    ), 
    array(
        'id'    => 234, 
        'value' => 780, 
    ), 
);

/* handle element merge */
function array_internal_merge_func($a, $b) {
    if ( is_array($a['value']) )
        $a['value'][]   = $b['value'];
    else 
        $a['value']     = array($a['value'], $b['value']);
    return $a;
}
/* handle array merge */
function array_internal_merge($array, $key, $merge_func) {
    $hashed = array();
    $result = array();
    foreach ( $array as $idx => $ele ) 
        $hashed[$ele[$key]][]   = $idx;
    foreach ( $hashed as $key => $idxies ) {
        reset($idxies);
        $idx0           = current($idxies);
        $result[$idx0]  = $array[$idx0];
        while ( $idx = next($idxies) ) 
            $result[$idx0]  = $merge_func($result[$idx0], $array[$idx]);
    }
    return $result;
}

print_r(array_internal_merge($arr['key1'], 'id', 'array_internal_merge_func'));