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

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

PHP:合并數(shù)組并添加鍵值

PHP:合并數(shù)組并添加鍵值

PHP
鴻蒙傳說 2023-04-15 11:02:47
我有一個包含數(shù)組的多維數(shù)組,每個數(shù)組都包含一個artisan和closeness鍵。我想合并多個數(shù)組(如果相同)artisans到一個數(shù)組,它是從中合并的數(shù)組closeness的值的總和。closeness也就是說,這個數(shù)組:Array(    [0] => Array        (            [artisan] => carpenter            [closeness] => 0.8        )    [1] => Array        (            [artisan] => brick layer            [closeness] => 0.4        )    [2] => Array        (            [artisan] => electrician            [closeness] => 0.9        )    [3] => Array        (            [artisan] => carpenter            [closeness] => 0.8            [is_real] => 1        )    [4] => Array        (            [artisan] => brick layer            [closeness] => 0.9            [is_real] => 1        ))應(yīng)該變成:Array(    [0] => Array        (            [artisan] => carpenter            [closeness] => 1.6 //0.8+0.8        )    [1] => Array        (            [artisan] => brick layer            [closeness] => 1.3  //0.4+0.9        )    [2] => Array        (            [artisan] => electrician            [closeness] => 0.9 //remains 0.9 because it is the only array with artisan value of electrician        )
查看完整描述

2 回答

?
慕仙森

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

試試這個循環(huán)


//create new array to hold values in the format you need

$new_array = [];


//loop through old array

foreach($old_array as $arr) {


    $artisan = $arr['artisan'];

    $closeness = $arr['closeness'];


    //check if `$artisan` key does not exists in `$new_array`

    if(!array_key_exists($artisan, $new_array)) {


        //create key in `$new_array` if it doesn't exist already

        //for this key, create an array containing the values in the format you need

        $new_array[$artisan] = [

            'artisan' => $artisan, 

            'closeness' => $closeness

        ];


    } else {


        //increase closeness in `$new_array` with key `$artisan` if key did already exist

        $new_array[$artisan]['closeness'] += $closeness;


    }


}


//replace keys which contain `$artisan` with sequential numbers, as per your expected output.

//if you don't do this, it will still work, but your array keys will be equal to the artisan type.

$new_array = array_values($new_array);

print_r($new_array);

結(jié)果:


Array

(

    [0] => Array

        (

            [artisan] => carpenter

            [closeness] => 1.6

        )


    [1] => Array

        (

            [artisan] => brick layer

            [closeness] => 1.3

        )


    [2] => Array

        (

            [artisan] => electrician

            [closeness] => 0.9

        )


)

如果刪除該array_values()行,結(jié)果將如下所示:


Array

(

    [carpenter] => Array

        (

            [artisan] => carpenter

            [closeness] => 1.6

        )


    [brick layer] => Array

        (

            [artisan] => brick layer

            [closeness] => 1.3

        )


    [electrician] => Array

        (

            [artisan] => electrician

            [closeness] => 0.9

        )


)


查看完整回答
反對 回復(fù) 2023-04-15
?
冉冉說

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個贊

怎么樣:


$results = array(); // create results array


foreach( $array as $k => $i ) {


    $found = false;


    foreach ( $results as $k => $r ) {


        if ( $r[ "artisan" ] == $i[ "artisan" ] ) {


            $results[ $k ][ "closeness" ] += $i[ "closeness" ];


            $found = true;


            break;


        }


    }


    if ( ! $found ) array_push( $results, $i );


}


查看完整回答
反對 回復(fù) 2023-04-15
  • 2 回答
  • 0 關(guān)注
  • 148 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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