Change language

How do I concatenate a duplicate value in a multidimensional array in PHP?

The example below illustrates the above approach in a more meaningful way:Example 1:In this example, the duality is at most two. $arr = array ( array ( ’ Roll’ = > 43, ’name’ = > ’ Geeeks’ , ’subject’ = > ’ Course -011’ ), array ( ’Rool’ = > 38, ’ name’ = > ’Gfg’ , ’ subject’ = > ’Course-012’ ), array ( ’Rool’ = > 43, ’ name’ = > ’ Geeks’ , ’subject’ = > ’Course-011’ ) );  
// Create empty array $myarray = array ();  
// Returns array keys $keys = array_keys ( $arr );  
// iterate over a multidimensional array for ( $i = 0; $i < count ( $arr ); $i ++) { $keys = array_keys ( $arr ); // iterate over each element of the array foreach ( $arr [ $keys [ $i ]] as $key = > $value ) { $f = 0; for ( $j = 0; $j < count ( $arr ); $j ++) { if ( $i ! = $j ) { foreach ( $arr [ $keys [ $j
]] as $key1 = > $value1 ) { // Check for duplicates if (( $key1 == $key ) & amp; & amp; ( $value == $value1 )) { $f = 1; // String index where duplicate // array exists $dup_ind = $j ; } } } } } // If a duplicate is found if ( $f == 1) { $temp_arr = array (); array_push ( $temp_arr , $arr [ $i ]); // Merge both arrays array_push ( $temp_arr , $arr [ $dup_ind ]); // Remove duplicate element from original array unset ( $arr [ $dup_ind ]); // Finally insert the array of results array_push ( $myarray , $temp_arr ); } else { // Directly insert into the results array array_push ( $myarray , $arr [ $keys [ $i ]]); } } print_r ( $myarray );  
?>
Output:
 Array ([0] = > Array ([0] = > Array ([Roll] = > 43 [name] = > Geeeks [subject] = > Course-011) [1] = > Array ([Rool] = > 43 [name] = > Geeks [subject] = > Course-011)) [1] = > Array ([Rool] = > 38 [name] = > Gfg [subject] = > Course-012)) 
Example 2:In this example, there are more than two duplicates. $arr = array ( array ( ’Roll’ = > 43, ’name’  = > ’Geeks’ , ’ subject’ = > ’Course-011’ ), array ( ’Roll’ = > 38, ’ name’ = > ’GFG’ , ’ subject’ = > ’Course-011’ ), array ( ’Roll’ = > 26, ’name’ = > ’GeeksforGeeks’ , ’ su bject’ = > ’Course-011’ ), array ( ’Roll’ = > 31, ’ name’ = > ’ gfg’ , ’subject’ = > ’Course-012’ ) ); foreach ( $arr as $k = > $v ) { $new_arr [ $v [ ’subject’ ]] [] = $v ; } print_r ( $new_arr );  
?>
Output:
 Array ([Course-011] = > Array ([0] = > Array ([Roll] = > 43 [name] = > Geeks [subject] = > Course-011) [1 ] = > Array ([Roll] = > 38 [name] = > GFG [subject] = > Course-011) [2] = > Array ([Roll] = > 26 [name] = > GeeksforGeeks [subject] = > Course-011)) [Course-012] = > Array ([0] = > Array ([Roll] = > 31 [name] = > gfg [subject] = > Course-012))) 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method