第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

我的多維數(shù)組合并中怎么會出現(xiàn) 2?

我的多維數(shù)組合并中怎么會出現(xiàn) 2?

PHP
動漫人物 2023-10-01 17:21:22
我正在嘗試合并多維數(shù)組中鍵的重復(fù)值。$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',       )        );在上面,我需要找到公共類并將它們的子合并到一個(gè)數(shù)組中。因此,預(yù)期輸出如下: Array    (        [0] => Array            (                [class] => one                [sub] => array( 'music', 'social', 'health', 'science', 'math' )            )            [1] => Array            (                [class] => two                [sub] => music            )        )我正在努力實(shí)現(xiàn)這個(gè)結(jié)果,但我卻一事無成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?我該如何達(dá)到預(yù)期的結(jié)果?謝謝。
查看完整描述

3 回答

?
慕的地10843

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊

array_push返回新數(shù)組中的元素?cái)?shù)量,而不是更改后的數(shù)組,這就是您進(jìn)入2輸出的原因。但這不是您唯一的問題,因?yàn)槟枰紤]到它$sub也可以是一個(gè)數(shù)組,在這種情況下,使用array_push將在輸出中為您提供一個(gè)數(shù)組數(shù)組。相反,使用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']));

}

示例數(shù)據(jù)的輸出:


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 上的演示


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

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊

問題是你使用方法array_push不對。該函數(shù)返回數(shù)組中的元素?cái)?shù)量,而不是數(shù)組本身 - 這些項(xiàng)目在第一個(gè)參數(shù)中添加到數(shù)組中,即$class_sub[ $sub['class'] ]

你的代碼中有這個(gè):

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;

這里發(fā)生的情況是,數(shù)組被添加到一起,然后數(shù)組項(xiàng)的計(jì)數(shù)$new( ) 被添加到$class_sub[ $sub['class'] ]。


試試這個(gè) - 這不會覆蓋$class_sub[ $sub['class'] ],它使用 array_merge 將數(shù)組添加在一起(這替換了整個(gè)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);

}


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

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

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


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;

}

如需演示,請點(diǎn)擊此處: https: //rextester.com/QKULS37967


查看完整回答
反對 回復(fù) 2023-10-01
  • 3 回答
  • 0 關(guān)注
  • 134 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號