亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我的多維數組合并中怎么會出現 2?

我的多維數組合并中怎么會出現 2?

PHP
動漫人物 2023-10-01 17:21:22
我正在嘗試合并多維數組中鍵的重復值。$subjects = array(       array(           'class' => 'one',           'sub' => 'music',       ),           array(           'class' => 'one',           'sub' => array( 'social', 'health', 'science' ),       ),          array(           'class' => 'two',           'sub' => 'music',       ),                      array(           'class' => 'one',           'sub' => 'math',       )        );在上面,我需要找到公共類并將它們的子合并到一個數組中。因此,預期輸出如下: Array    (        [0] => Array            (                [class] => one                [sub] => array( 'music', 'social', 'health', 'science', 'math' )            )            [1] => Array            (                [class] => two                [sub] => music            )        )我正在努力實現這個結果,但我卻一事無成2。    $class_sub = array();    $result = array();        foreach( $subjects as $sub ) {        if ( ! isset( $class_sub[ $sub['class'] ] ) ) {            $class_sub[ $sub['class'] ] = $sub['sub'];        } else {            if ( is_array( $class_sub[ $sub['class'] ] ) ) {                $new = array_push( $class_sub[ $sub['class'] ], $sub['sub'] );            } else {                $new[] = $sub['sub'];            }                        $class_sub[ $sub['class'] ] = $new;        }    }        foreach( $class_sub as $class => $sub ) {        $result[] = array(            'class' => $class,            'sub' => $sub        );    }    echo "<pre>"; print_r( $result ); echo "</pre>";    在這里我得到了2子: Array    (        [0] => Array            (                [class] => one                [sub] => 2            )            [1] => Array            (                [class] => two                [sub] => music            )        )這是怎么回事2?我該如何達到預期的結果?謝謝。
查看完整描述

3 回答

?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

array_push返回新數組中的元素數量,而不是更改后的數組,這就是您進入2輸出的原因。但這不是您唯一的問題,因為您需要考慮到它$sub也可以是一個數組,在這種情況下,使用array_push將在輸出中為您提供一個數組數組。相反,使用array_merge

if ( is_array( $class_sub[ $sub['class'] ] ) ) {

? ? $new = array_merge( $class_sub[ $sub['class'] ], is_array($sub['sub']) ?? $sub['sub'] : array($sub['sub']));

} else {

? ? $new = array_merge( array($class_sub[ $sub['class'] ]),? is_array($sub['sub']) ?? $sub['sub'] : array($sub['sub']));

}

示例數據的輸出:


Array

(

? ? [0] => Array

? ? ? ? (

? ? ? ? ? ? [class] => one

? ? ? ? ? ? [sub] => Array

? ? ? ? ? ? ? ? (

? ? ? ? ? ? ? ? ? ? [0] => music

? ? ? ? ? ? ? ? ? ? [1] => social

? ? ? ? ? ? ? ? ? ? [2] => health

? ? ? ? ? ? ? ? ? ? [3] => science

? ? ? ? ? ? ? ? ? ? [4] => math

? ? ? ? ? ? ? ? )? ??

? ? ? ? )

? ? [1] => Array

? ? ? ? (

? ? ? ? ? ? [class] => two

? ? ? ? ? ? [sub] => music

? ? ? ? )

)

3v4l.org 上的演示


查看完整回答
反對 回復 2023-10-01
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

問題是你使用方法array_push不對。該函數返回數組中的元素數量,而不是數組本身 - 這些項目在第一個參數中添加到數組中,即$class_sub[ $sub['class'] ]

你的代碼中有這個:

if ( is_array( $class_sub[ $sub['class'] ] ) ) {

? ? $new = array_push( $class_sub[ $sub['class'] ], $sub['sub'] );

} else {

? ? $new[] = $sub['sub'];

}

$class_sub[ $sub['class'] ] = $new;

這里發生的情況是,數組被添加到一起,然后數組項的計數$new( ) 被添加到$class_sub[ $sub['class'] ]。


試試這個 - 這不會覆蓋$class_sub[ $sub['class'] ],它使用 array_merge 將數組添加在一起(這替換了整個foreach( $subjects as $sub )塊,而不僅僅是上面的代碼):


foreach( $subjects as $sub ) {

? ? /* prepare subjects as an array */

? ? if ( is_array( $sub['sub'] ) )? $subjects = $sub['sub'];

? ? else? ? ? ? ? ? ? ? ? ? ? ? ? ? $subjects = array($sub['sub']);


? ? if ( ! isset( $class_sub[ $sub['class'] ] ) )

? ? ? ? /* if this is the first subject for this class, there is no array to merge with */

? ? ? ? $class_sub[ $sub['class'] ] = $subjects;?

? ? else?

? ? ? ? /* We know both class and subject are arrays so we can merge them */

? ? ? ? $class_sub[$sub['class']] = array_merge( $class_sub[ $sub['class'] ], $subjects);

}


查看完整回答
反對 回復 2023-10-01
?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

出于好奇,我編寫了您自己的函數版本。因此,這不是對您問題的直接答案,但也許,這里的其他人仍然感興趣,他們試圖按照您指定的方式“合并”數組。


function amerge($arr){

  foreach ($arr as $e){

     $sub=is_array($e["sub"])?$e["sub"]:array($e["sub"]);

     foreach ($sub as $s) $p[$e["class"]][$s]=1;

   }

  $classes=array_keys($p);

  foreach ($classes as $c) 

  $out[]=array("class"=>$c,"sub"=>array_keys($p[$c]));

  return $out;

}

如需演示,請點擊此處: https: //rextester.com/QKULS37967


查看完整回答
反對 回復 2023-10-01
  • 3 回答
  • 0 關注
  • 138 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號