0

これが私の配列です

Array
(
    [0] => Array
        (
            [0] => Jan 2010
            [1] => 65.75
        )

    [1] => Array
        (
            [0] => Jan 2010
            [1] => 211.05
        )

    [2] => Array
        (
            [0] => Jan 2010
            [1] => 582.7
        )

    [3] => Array
        (
            [0] => Feb 2010
            [1] => 136.3
        )

    [4] => Array
        (
            [0] => Feb 2010
            [1] => 215.32
        )

    [5] => Array
        (
            [0] => Feb 2010
            [1] => 413.9
        )

    [6] => Array
        (
            [0] => Mar 2010
            [1] => 156.35
        )

    [7] => Array
        (
            [0] => Mar 2010
            [1] => 210.54
        )

    [8] => Array
        (
            [0] => Mar 2010
            [1] => 585.15
        )

    [9] => Array
        (
            [0] => Apr 2010
            [1] => 126.1
        )

    [10] => Array
        (
            [0] => Apr 2010
            [1] => 255.47
        )

    [11] => Array
        (
            [0] => Apr 2010
            [1] => 329.1
        )

    [12] => Array
        (
            [0] => May 2010
            [1] => 109
        )

    [13] => Array
        (
            [0] => May 2010
            [1] => 170
        )

    [14] => Array
        (
            [0] => May 2010
            [1] => 716.7
        )

)

[0] の同じ値を持つすべての配列をマージする方法はありますか? 私はそれがこのようなものになりたい:

Array[0] = (Jan 2010, 65.75, 211.05, 582.7)
Array[1] = (Feb 2010, 136.3, 215.32, 413.9)

等々...

4

4 に答える 4

3

これを「単純に」実行するために考えられる最も近い方法 (読み取り: ネイティブ PHP 機能を利用する) は、結果のテキスト キー (連想配列) に切り替えることです。サンプルの結果配列では「キー」データと「値」データが混在しているため、これはデータ モデリングの観点からも理にかなっています (たとえば、最初の値はセット == 不良のラベルであるという責任を負います)。[]秘訣は、配列に新しい値を追加する暗黙の「プッシュ」演算子を使用することです。

foreach($sourceArray as $currentSubArray) {
  $resultArray[$currentSubArray[0]][] = $currentSubArray[1];
}

結果は次のようになります。

Array (
  'Jan 2010' => Array (
    0 => 65.75,
    1 => 211.05,
    2 => 582.7,
  )
  ...
)
于 2013-04-10T16:35:55.870 に答える
1

これは、@ctrahey が提案したものの変形であり、入力配列を直接操作します。

foreach($array as $key => &$entry) {
    list($month, $value) = $entry;
    if (isset($ptr[$month])) {
        $ptr[$month][] = $value;
        unset($array[$key]);
    } else {
        $ptr[$month] = &$entry;
    }
}
unset($ptr);

サンプル データを出力します。

Array
(
    [0] => Array
        (
            [0] => Jan 2010
            [1] => 65.75
            [2] => 211.05
            [3] => 582.7
        )

    [3] => Array
        (
            [0] => Feb 2010
            [1] => 136.3
            [2] => 215.32
            [3] => 413.9
        )

    [6] => Array
        (
            [0] => Mar 2010
            [1] => 156.35
            [2] => 210.54
            [3] => 585.15
        )

    [9] => Array
        (
            [0] => Apr 2010
            [1] => 126.1
            [2] => 255.47
            [3] => 329.1
        )

    [12] => Array
        (
            [0] => May 2010
            [1] => 109
            [2] => 170
            [3] => 716.7
        )

)
于 2013-04-10T21:36:06.790 に答える
0

それはかなり恐ろしいですが、うまくいきます...

$resarray=Array(
    0 => Array(0 => 'Jan 2010',1 => 65.75),
    1=> Array(0 => 'Jan 2010',1 => 211.05),
    2 => Array(0 => 'Jan 2010',1 => 582.7),
    3 => Array(0 => 'Feb 2010',1 => 136.3),
    4 => Array(0 => 'Feb 2010',1 => 215.32),
    5 => Array(0 => 'Feb 2010',1 => 413.9),
    6 => Array(0 => 'Feb 2010',1 => 156.35),
    7 => Array(0 => 'Feb 2010',1 => 210.54),
    8 => Array(0 => 'Mar 2010',1 => 585.15),
    9 => Array(0 => 'Apr 2010',1 => 126.1),
    10 => Array(0 => 'Apr 2010',1 => 255.47),
    11 => Array(0 => 'Apr 2010',1 => 329.1),
    12 => Array(0 => 'May 2010',1 => 109),
    13 => Array(0 => 'May 2010',1 => 170),
    14 => Array(0 => 'May 2010',1 => 716.7)

    );
$CatArray=array();
$FinArray=array();
$count=count($resarray);
for($i=0;$i<$count;$i++){
    $index=array_search($resarray[$i][0],$CatArray);
    if(is_numeric($index) && $index>=0)
        $FinArray[$index][]=$resarray[$i][1];
    else{
        $CatArray[]=$resarray[$i][0];
        $FinArray[]=array($resarray[$i][0],$resarray[$i][1]);
    }
}
unset($CatArray);
unset($resarray);
$count=count($FinArray);
for($i=0;$i<$count;$i++){
    $resarray[$i]=implode(',',$FinArray[$i]);
}

出力:

Array
(
    [0] => Jan 2010,65.75,211.05,582.7
    [1] => Feb 2010,136.3,215.32,413.9,156.35,210.54
    [2] => Mar 2010,585.15
    [3] => Apr 2010,126.1,255.47,329.1
    [4] => May 2010,109,170,716.7
)
于 2013-04-10T17:14:36.463 に答える
0
            actionMerge($inputArray);

            function actionMerge($inputArray){


                $month = array();
                $earn = array();
                $parentKey = 0;
                $callback = function ($value, $key) use (&$month, &$earn, &$parentKey) {
                    if(!is_array($value)){
                        if($key == 0){
                            if(!in_array($value, $month)){

                                array_push($month, $value);
                                $earn[$value] =  array();
                            }
                            $parentKey = $value;
                        }elseif($key == 1){
                                array_push($earn[$parentKey], $value);
                        }
                    }

                };

                array_walk_recursive($inputArray, $callback);

                echo 'You should use this array';
                var_dump($earn); // group money by month, I recommend you to use it


                $Array = array();
                foreach($month as $m){
                    $arr = array($m);
                    $arr  = array_merge($arr, $earn[$m]);
                    array_push($Array, $arr);
                }

                echo '...Intead of array result what you expect';
                var_dump($Array); // your expect result

            }

            ?>

結果は次のとおりです。

                        //You should use this array
        array (size=5)
          'Jan 2010' => 
            array (size=3)
              0 => float 65.75
              1 => float 211.05
              2 => float 582.7
          'Feb 2010' => 
            array (size=3)
              0 => float 136.3
              1 => float 215.32
              2 => float 413.9
          'Mar 2010' => 
            array (size=3)
              0 => float 156.35
              1 => float 210.54
              2 => float 585.15
          'Apr 2010' => 
            array (size=3)
              0 => float 126.1
              1 => float 255.47
              2 => float 329.1
          'May 2010' => 
            array (size=3)
              0 => int 109
              1 => int 170
              2 => float 716.7
        //...Intead of array result what you expect
        array (size=5)
          0 => 
            array (size=4)
              0 => string 'Jan 2010' (length=8)
              1 => float 65.75
              2 => float 211.05
              3 => float 582.7
          1 => 
            array (size=4)
              0 => string 'Feb 2010' (length=8)
              1 => float 136.3
              2 => float 215.32
              3 => float 413.9
          2 => 
            array (size=4)
              0 => string 'Mar 2010' (length=8)
              1 => float 156.35
              2 => float 210.54
              3 => float 585.15
          3 => 
            array (size=4)
              0 => string 'Apr 2010' (length=8)
              1 => float 126.1
              2 => float 255.47
              3 => float 329.1
          4 => 
            array (size=4)
              0 => string 'May 2010' (length=8)
              1 => int 109
              2 => int 170
              3 => float 716.7
于 2013-04-10T18:24:04.677 に答える