$AA = Array(
[r,99] = b [i,100] = A [a,101] + D [s,102] = C [e,103] + D [c,104] and b [R , [+] = A);
$CC = Array ("A" = "1", "B" = "2", "C" = "3", "D" = "4", "E" = "5", "F" = "6", "G" = "7"
Requirements::: The AA array inside the r=b, and then match the value inside the cc array, b corresponds to the value of 2, if AA has two r then add their values
$str = ""; foreach ($aa as $key = = $v) {$kk = substr ($key, 0,1); $str. = substr ($key, 0, 1). " = ". $cc [$v].", ";}
Get str= ' r=2,i=1,a=4,s=3,e=4,c=2,r=1 ';
The STR string is then processed into a
Array ( [R] = 3 [I] = 1 [A] = 4 [S] + 3 [E] = 4 [C] = 2)
This format,: note that when stitching arrays here, the key name if the same will be replaced, the requirement is that if the key name is the same, the key value is added
Ideas:
First, Str is separated by commas into the array
Array ( [0] = r=2 [1] = I=1 [2] = A=4 [3] = = S=3 [4] = = E=4 [5] = = c=2
[6] = r=1)
Loop the array, and then loop the value of the new array again, breaking into a new array with =
Array ( [0] = R [1] = 2)
Array ( [0] = I [1] = 1)
Array ( [0] = A [1] = 4)
Array ( [0] = S [1] + 3)
Array ( [0] = = E [1] = 4)
Array ( [0] = C [1] = 2)
Array ( [0] = R [1] = 1)
The value of the new array 0 is then used as the key name to form a new array, and if the key name is the same, the key value is added, otherwise
Code:
$str=RTrim($str,",");$arr 1=Explode(‘,‘,$str);$arr 2=[];foreach($arr 1 as $k=$va) { $arr 3=Explode(' = ',$va); AAA ($arr 3); if(isset($arr 2[$arr 3[0]])) { $arr 2[$arr 3[0]] + =$arr 3[1]; }Else{ $arr 2[$arr 3[0]] =$arr 3[1]; }}
function aaa ($CCD) {
echo "<pre>";
Print_r ($CCD);
echo "</pre>";
}
Optimization ideas: No more splicing strings, direct judgment add
$AA = Array ( [r,99] + B [i,100] = A [a,101] + D [s,102] = c [e,103] + d [ C , 104] = B [r,105] = A);
$CC = Array ( [A] = 1 [B] = 2 [C] = 3 [D] = 4 [E] = 5 [F] = 6 [G] + = 7);
$arr 5 = [];foreach ($aa as $key = = $v) {$kk = substr ($key, 0,1), if (Isset ($arr 5[$kk]) {//If the key name exists, the value is added $arr5[$kk] + = $CC [$v] ;} else{$arr 5[$kk] = $cc [$v];}}
Processing string stitching into desired array