If one element in the PHP array is the same, how can we splice other elements, such as the original array?
Array (3 ){
[0] => array (6 ){
["Pla_id"] => string (3) "298"
["User_id"] => string (1) "1"
["Due_in"] => string (15) "8.33"
}
[1] => array (6 ){
["Pla_id"] => string (3) "976"
["User_id"] => string (1) "1"
["Due_in"] => string (15) "338.90"
}
[2] => array (6 ){
["Pla_id"] => string (3) "56"
["User_id"] => string (1) "2"
["Due_in"] => string (15) "25.6"
}}
Because the user_id = 1 in the array is the same
To get the final array like this, how do I write (that is, concatenate other elements with the same user_id )?
Array (3 ){
[0] => array (6 ){
["Pla_id"] => string (3) "298,976"
["User_id"] => string (1) "1"
["Due_in"] => string (15) "8.33, 338.90"
}
[1] => array (6 ){
["Pla_id"] => string (3) "56"
["User_id"] => string (1) "2"
["Due_in"] => string (15) "25.6"
}}
Reply to discussion (solution)
$a = array( array( "pla_id" => "298", "user_id" => "1", "due_in" => "8.33", ), array( "pla_id" => "976", "user_id" => "1", "due_in" => "338.90", ), array( "pla_id" => "56", "user_id" => "2", "due_in" => "25.6", ));$b = array();foreach($a as $v) { if(! isset($b[$v['user_id']])) $b[$v['user_id']] = $v; else $b[$v['user_id']]["due_in"] .= ',' . $v["due_in"];}var_dump(array_values($b));
array(2) { [0]=> array(3) { ["pla_id"]=> string(3) "298" ["user_id"]=> string(1) "1" ["due_in"]=> string(11) "8.33,338.90" } [1]=> array(3) { ["pla_id"]=> string(2) "56" ["user_id"]=> string(1) "2" ["due_in"]=> string(4) "25.6" }}
$a = array( array( "pla_id" => "298", "user_id" => "1", "due_in" => "8.33", ), array( "pla_id" => "976", "user_id" => "1", "due_in" => "338.90", ), array( "pla_id" => "56", "user_id" => "2", "due_in" => "25.6", ));$b = array();foreach($a as $v) { if(! isset($b[$v['user_id']])) $b[$v['user_id']] = $v; else $b[$v['user_id']]["due_in"] .= ',' . $v["due_in"];}var_dump(array_values($b));
array(2) { [0]=> array(3) { ["pla_id"]=> string(3) "298" ["user_id"]=> string(1) "1" ["due_in"]=> string(11) "8.33,338.90" } [1]=> array(3) { ["pla_id"]=> string(2) "56" ["user_id"]=> string(1) "2" ["due_in"]=> string(4) "25.6" }}
Thank you. thank you very much.