0

配列内のいくつかの値で配列要素をグループ化するにはどうすればよいですか?

たとえば、次の配列の配列を考えてみましょう。配列を 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

4 に答える 4

2

値が 1 つある場合は整数として、値が複数ある場合は配列として値を取得しますか?

<?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>';
?>
于 2013-01-24T08:05:44.257 に答える
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'));
于 2013-01-24T08:14:40.903 に答える
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
                )

        )

)
于 2013-01-24T07:58:17.520 に答える
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);
于 2013-01-24T08:01:22.453 に答える