亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

php語言

php二維數(shù)組怎么求和

時間:2024-07-16 19:12:07 php語言 我要投稿
  • 相關(guān)推薦

php二維數(shù)組怎么求和

  導(dǎo)語:數(shù)組是php語言的一大難題。以下的是百分網(wǎng)小編為大家搜集的php基二維數(shù)組求和的方法,希望對你有所幫助。

  數(shù)組:

  $a = array(array("a"=>"aa","a1"=>1,"a2"=>1),array("a"=>"bb","a1"=>2,"a2"=>2),array("a"=>"cc","a1"=>3,"a2"=>3),

  array("a"=>"aa","a1"=>1,"a2"=>1),array("a"=>"bb","a1"=>5,"a2"=>5))

  第一個鍵值相同的元素 求和

  結(jié)果

  $r = array(array("a"=>"aa","a1"=>2,"a2"=>2),array("a"=>"bb","a1"=>7,"a2"=>7),array("a"=>"cc","a1"=>3,"a2"=>3));

  ------解決方案--------------------

  $a = array(

  array("a" => "aa", "a1" => 1, "a2" => 1),

  array("a" => "bb", "a1" => 2, "a2" => 2),

  array("a" => "cc", "a1" => 3, "a2" => 3),

  array("a" => "aa", "a1" => 1, "a2" => 1),

  array("a" => "bb", "a1" => 5, "a2" => 5),

  );

  $res = array();

  foreach($a as $r) {

  if(! isset($res[$r['a']])) $res[$r['a']] = $r;

  else {

  $res[$r['a']]['a1'] += $r['a1'];

  $res[$r['a']]['a2'] += $r['a2'];

  }

  }

  print_r(array_values($res));

  Array

  (

  [0] => Array

  (

  [a] => aa

  [a1] => 2

  [a2] => 2

  )

  [1] => Array

  (

  [a] => bb

  [a1] => 7

  [a2] => 7

  )

  [2] => Array

  (

  [a] => cc

  [a1] => 3

  [a2] => 3

  )

  )

【php二維數(shù)組怎么求和】相關(guān)文章:

PHP判斷數(shù)組是一維二維或幾維實例09-10

php字符串與數(shù)組怎么轉(zhuǎn)換10-04

C語言二維數(shù)組怎么寫好10-27

c語言怎么定義一維數(shù)組和二維數(shù)組07-22

PHP數(shù)組函數(shù)知識10-24

簡單PHP數(shù)組函數(shù)介紹09-26

php數(shù)組長度的方法10-05

PHP數(shù)組長度的技巧09-18

PHP數(shù)組的交集array-intersect()09-13

php遞歸遍歷多維數(shù)組的方法10-06