doc1355 Posted August 30, 2021 Share Posted August 30, 2021 I have an array as below: Array ( [0] => Array ( [u1] => 6 [u2] => 4 ) [1] => Array ( [u1] => 6 [u2] => 3 ) [2] => Array ( [u1] => 3 [u2] => 4 ) [3] => Array ( [u1] => 3 [u2] => 11 ) [4] => Array ( [u1] => 3 [u2] => 11 ) [5] => Array ( [u1] => 3 [u2] => 4 ) [6] => Array ( [u1] => 4 [u2] => 11 ) [7] => Array ( [u1] => 4 [u2] => 11 ) [8] => Array ( [u1] => 6 [u2] => 3 ) [9] => Array ( [u1] => 6 [u2] => 4 ) [10] => Array ( [u1] => 6 [u2] => 11 ) [11] => Array ( [u1] => 6 [u2] => 11 ) ) What I'm trying to achieve is to create a table as below: 6: 4 3 3 4 11 11 4: 6 3 3 11 11 6 3: 6 4 11 11 4 6 11: 3 3 4 4 6 6 Here is what is happening: It shows the first value of u1 (6) , then searches all the other arrays, if that value (6) exists, it shows the other value one at a time ( 4-3-3-4-11-11). Then starts new row, looks up the next value (which is not same as the first value and goes through the same process, until we have one row for all the unique numbers in the array, then stops. I'm not sure how I can setup my loop to do this and I appreciate any helps that anyone could offer. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/313627-create-a-table-from-pairs-of-array/ Share on other sites More sharing options...
Barand Posted August 30, 2021 Share Posted August 30, 2021 Create an intermediate array which restructures your data foreach ($array as $a) { if (!isset($new[$a['u1']])) { $new[$a['u1']] = []; } $new[$a['u1']][] = $a['u2']; } which gives $new array ... Array ( [6] => Array ( [0] => 4 [1] => 3 [2] => 3 [3] => 4 [4] => 11 [5] => 11 ) [3] => Array ( [0] => 4 [1] => 11 [2] => 11 [3] => 4 ) [4] => Array ( [0] => 11 [1] => 11 ) ) Now it's simple to loop through outputting the rows and columns. 1 Quote Link to comment https://forums.phpfreaks.com/topic/313627-create-a-table-from-pairs-of-array/#findComment-1589469 Share on other sites More sharing options...
doc1355 Posted August 31, 2021 Author Share Posted August 31, 2021 (edited) Thank you so much for your quick reply. This is a great help. By using the loop with the $new variable I can show half of the table (blue) only and not the other half (red). I know this is duplicate of the blue data . How can I show the second half (red) in my loop to make it a full table? 6: 4 3 3 4 11 11 4: 6 3 3 11 11 6 3: 6 4 11 11 4 6 11: 3 3 4 4 6 6 Edited August 31, 2021 by doc1355 Quote Link to comment https://forums.phpfreaks.com/topic/313627-create-a-table-from-pairs-of-array/#findComment-1589471 Share on other sites More sharing options...
Solution Barand Posted August 31, 2021 Solution Share Posted August 31, 2021 Rinse and repeat - exchanging u1 and u2 $new = []; foreach ($array as $a) { if (!isset($new[$a['u1']])) { $new[$a['u1']] = []; } $new[$a['u1']][] = $a['u2']; //repeat exchanging u1 and u2 if (!isset($new[$a['u2']])) { $new[$a['u2']] = []; } $new[$a['u2']][] = $a['u1']; } // // Output $new array // echo '<pre>'; foreach ($new as $u1 => $u2s) { printf('<br><b>%4d</b> | ', $u1); foreach ($u2s as $u) { printf('%4d ⋮', $u); } } 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/313627-create-a-table-from-pairs-of-array/#findComment-1589475 Share on other sites More sharing options...
doc1355 Posted August 31, 2021 Author Share Posted August 31, 2021 Brilliant. This worked like a charm. Thank you so much. Quote Link to comment https://forums.phpfreaks.com/topic/313627-create-a-table-from-pairs-of-array/#findComment-1589477 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.