doc1355 Posted August 31, 2021 Share Posted August 31, 2021 I'm working on a code for my WordPress and need to organize my array so that I can use the data for output in a table. In my WP, I have custom posts (called match) that has custom fields (user1, user2, s1p1, s1p2, s2p1, s2p2, s3p1, s3p2). Here is my script: $match_query = new WP_Query( $match_args ); // If the query is empty, then don't do anything. Otherwise start the loop if ( $match_query->have_posts() ) { // Start the loop while ( $match_query->have_posts() ) { $match_query->the_post(); $match_id = get_the_ID(); // Get the ID of each Match post in this event. $related_user_1 = get_post_meta( $match_id, 'related_user_1' ); $related_user_2 = get_post_meta( $match_id, 'related_user_2' ); $set_1_player_1 = get_post_meta( $match_id, 'set_1_player_1' ); $set_1_player_2 = get_post_meta( $match_id, 'set_1_player_2' ); $set_2_player_1 = get_post_meta( $match_id, 'set_2_player_1' ); $set_2_player_2 = get_post_meta( $match_id, 'set_2_player_2' ); $set_3_player_1 = get_post_meta( $match_id, 'set_3_player_1' ); $set_3_player_2 = get_post_meta( $match_id, 'set_3_player_2' ); $pair_id[] = array( $related_user_1[ 0 ][ 'ID' ] => array( 'u2' => $related_user_2[ 0 ][ 'ID' ], 's1p1' => $set_1_player_1, 's1p2' => $set_1_player_2, 's2p1' => $set_2_player_1, 's2p2' => $set_2_player_2, 's3p1' => $set_3_player_1, 's3p2' => $set_3_player_2, ), ); } } else { // No posts Found. echo 'No Matches Found.'; } wp_reset_postdata(); My script is pulling all the data correctly and generating the following array: Array ( [0] => Array ( [6] => Array ( [u2] => 4 [s1p1] => Array ( ) [s1p2] => Array ( ) [s2p1] => Array ( ) [s2p2] => Array ( ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) ) [1] => Array ( [6] => Array ( [u2] => 3 [s1p1] => Array ( ) [s1p2] => Array ( ) [s2p1] => Array ( ) [s2p2] => Array ( ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) ) [2] => Array ( [3] => Array ( [u2] => 4 [s1p1] => Array ( ) [s1p2] => Array ( ) [s2p1] => Array ( ) [s2p2] => Array ( ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) ) [3] => Array ( [3] => Array ( [u2] => 11 [s1p1] => Array ( ) [s1p2] => Array ( ) [s2p1] => Array ( ) [s2p2] => Array ( ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) ) [4] => Array ( [3] => Array ( [u2] => 11 [s1p1] => Array ( ) [s1p2] => Array ( ) [s2p1] => Array ( ) [s2p2] => Array ( ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) ) [5] => Array ( [3] => Array ( [u2] => 4 [s1p1] => Array ( ) [s1p2] => Array ( ) [s2p1] => Array ( ) [s2p2] => Array ( ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) ) [6] => Array ( [4] => Array ( [u2] => 11 [s1p1] => Array ( ) [s1p2] => Array ( ) [s2p1] => Array ( ) [s2p2] => Array ( ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) ) [7] => Array ( [4] => Array ( [u2] => 11 [s1p1] => Array ( [0] => 11 ) [s1p2] => Array ( [0] => 12 ) [s2p1] => Array ( ) [s2p2] => Array ( ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) ) [8] => Array ( [6] => Array ( [u2] => 3 [s1p1] => Array ( ) [s1p2] => Array ( ) [s2p1] => Array ( ) [s2p2] => Array ( ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) ) [9] => Array ( [6] => Array ( [u2] => 4 [s1p1] => Array ( ) [s1p2] => Array ( ) [s2p1] => Array ( ) [s2p2] => Array ( ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) ) [10] => Array ( [6] => Array ( [u2] => 11 [s1p1] => Array ( ) [s1p2] => Array ( ) [s2p1] => Array ( ) [s2p2] => Array ( ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) ) [11] => Array ( [6] => Array ( [u2] => 11 [s1p1] => Array ( ) [s1p2] => Array ( ) [s2p1] => Array ( ) [s2p2] => Array ( ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) ) ) The issue is that I need to setup a table to have one unique user in the first column, with all the data associated with that in the following column in front of it. Like the attached image I am not sure how I can generate the above table with my current array and I don't know how to rearrange my array to do this. Thank you again for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/ Share on other sites More sharing options...
gw1500se Posted August 31, 2021 Share Posted August 31, 2021 asort Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589489 Share on other sites More sharing options...
doc1355 Posted August 31, 2021 Author Share Posted August 31, 2021 2 minutes ago, gw1500se said: asort Thank you for your reply. With asort, I can only sort my array, but I won't be able to use a loop to create the table. The data in the table is repeated twice, once for each user in their own row. Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589490 Share on other sites More sharing options...
Barand Posted August 31, 2021 Share Posted August 31, 2021 3 hours ago, doc1355 said: I am not sure how I can generate the above table with my current array Me neither. Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589491 Share on other sites More sharing options...
doc1355 Posted August 31, 2021 Author Share Posted August 31, 2021 13 minutes ago, Barand said: Me neither. So, does it mean that it can't be done? !!! Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589492 Share on other sites More sharing options...
gw1500se Posted August 31, 2021 Share Posted August 31, 2021 If I understand correctly, you loop on the outer sorted array. The within that loop you sort each inner array and loop on that to output the table. Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589495 Share on other sites More sharing options...
doc1355 Posted August 31, 2021 Author Share Posted August 31, 2021 2 minutes ago, gw1500se said: If I understand correctly, you loop on the outer sorted array. The within that loop you sort each inner array and loop on that to output the table. I'm not looking to sort the data. I can show the data in any order and that's OK. I apologize if I didn't explain better. The final table is to show a an entire sport event results. . The first column on the left, is each player in that event [shown as number]. In front of each player, each cell, will represent one match, and each have data associated with that match (opponent name [shown as ID], match scores[s1-p1, ..... ] ). So, when player 4 plays a match with player 6, that match data will be shown in two boxes, once in front of player 4 and once in front of player 6. @Barand helped me to create a table for the players and that is working fine The problem that I have is to add the match results to each cell. That's what I'm trying to do. I hope this explains better. Thank you again for your time. Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589497 Share on other sites More sharing options...
gw1500se Posted August 31, 2021 Share Posted August 31, 2021 Are you asking how to create a table within a cell of an outer table? Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589499 Share on other sites More sharing options...
doc1355 Posted August 31, 2021 Author Share Posted August 31, 2021 15 minutes ago, gw1500se said: Are you asking how to create a table within a cell of an outer table? No, I'm asking how to put all the data from one match inside one cell using a loop. The following code works to create the table as in the previous thread: foreach ( $user as $u1 => $u2s ) { echo '<tr><td>' . $u1 . '</td>'; foreach ( $u2s as $x ) { echo '<td>' . $x . '</td>'; } echo '</tr>'; } What I need is on line #4 where I echo $x to show the opponents name, I also want to show the match results (s1p1, s2p2, ..... ) Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589501 Share on other sites More sharing options...
Barand Posted August 31, 2021 Share Posted August 31, 2021 (edited) Too many ambiguities for me. Where you say you want outputting, I assume s1p1 is the value from the subarray with that key. Am I correct? If I am, do you want s1p1 minus s1p2 or do you want them displaying as game results (eg 10 - 12)? The values for s1p1 etc are all arrays which suggests there could be multiple values. If there are multiple values, how should they be procesed? If there aren't multiple values (as your sample only ever shows 0 or 1 values) why is it an array? Finally, in your topic you posted earlier I showed you the technique to get to the output. This is only an extension of that, so what exactly are you having a problem with? PS If you are posting a large array with the expectation of some else testing with it, use var_export($array), not print_r() Edited August 31, 2021 by Barand PS Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589504 Share on other sites More sharing options...
doc1355 Posted August 31, 2021 Author Share Posted August 31, 2021 1 minute ago, Barand said: Too many ambiguities for me. Where you say you want outputting, I assume s1p1 is the value from the subarray with that key. Am I correct? If I am, do you want s1p1 minus s1p2 or do you want them displaying as game results (eg 10 - 12)? The values for s1p1 etc are all arrays which suggests there could be multiple values. If there are multiple values, how should they be procesed? If there aren't multiple values (as your sample only ever shows 0 or 1 values) why is it an array? Finally, in your topic you posted earlier I showed you the technique to get to the output. This is only an extension of that, so what exactly are you having a problem with? Yes, S1P1 means Set1-Player1. It will show the result of the first set for the Player1 (which is the player in the first column). S1P2 is the result of Set1 for Player2. S2P1 is the result of Second set for player 1 S2P2 is the result of Second set for player 2. There are maximum of three sets per match. If there is no results it will show "-" until the result is posted. This is the how the final result should look like. I color coded the matches that are the same. For example, Kim and Tom played two matches. The result of first match are in the yellow box and the second match in the purple box. First match scores were as Tom 2 - Kim 6 Tom 1 - Kim 6 Second Match Scores Tom 6 - Kim 3 Tom 7 - Kim 6 They did not play the third set. Other scenarios in the table: - Mike and Kim only played once. They haven't played the second match yet. That's why they both have one box without match results (light green) - John and Tom played twice (Green and light blue). One match, they played three sets (Green) and one they played two sets (light blue). Also, the scores in each box are in specific order. The first score is for the player in the first column, the second score is for the player inside the cell. I hope this explains better. Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589507 Share on other sites More sharing options...
Barand Posted August 31, 2021 Share Posted August 31, 2021 (edited) You don't explain why the scores are in arrays Edited August 31, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589509 Share on other sites More sharing options...
doc1355 Posted August 31, 2021 Author Share Posted August 31, 2021 (edited) 5 minutes ago, Barand said: You don't explain why the scores are in arrays That's my problem with the code. I don't know how to fix or make the array so that I can use the data to generate the table. It does't have to be if you know a way to get the the table. Please feel free to change my code that I posted above to whatever you think it will work. Edited August 31, 2021 by doc1355 Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589510 Share on other sites More sharing options...
Barand Posted August 31, 2021 Share Posted August 31, 2021 2 minutes ago, doc1355 said: Please feel free to change my code that I posted above to whatever you think it will work. I can't. I haven't a clue what those WP functions are doing. Is a var_export() of that array imminent? I'm not going to waste time editing it to make it usable again. Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589511 Share on other sites More sharing options...
doc1355 Posted August 31, 2021 Author Share Posted August 31, 2021 (edited) You won't make it worse. Let me explain, The line get_post_meta( $match_id, 'related_user_1' ) captures the user array for player 1. ( we need to add [ 0 ][ 'ID' ] to get the ID) change 'related_user_1' and you will get other results from the match: related_user_2 will be player 2set_1_player_1 is the value for first set, player 1. This is not an array. It is just the value itself. set_1_player_2 is the value for first set, player 2. This is not an array. It is just the value itself. and so on. All the above are running in a while() loop so that it captures all the results for the event. Each time this loop runs, it collects the data for one match, which should go into one cell in my table (except for the player 1 that goes to the first column). This part below ($pair_id .....) , is my attempt to create an array. This is where I need the help to setup appropriate array so that I can create my table. $pair_id[] = array( $related_user_1[ 0 ][ 'ID' ] => array( 'u2' => $related_user_2[ 0 ][ 'ID' ], 's1p1' => $set_1_player_1, 's1p2' => $set_1_player_2, 's2p1' => $set_2_player_1, 's2p2' => $set_2_player_2, 's3p1' => $set_3_player_1, 's3p2' => $set_3_player_2, ), ); Edited August 31, 2021 by doc1355 Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589516 Share on other sites More sharing options...
Barand Posted August 31, 2021 Share Posted August 31, 2021 It looks like get_post_meta always returns an array. Third and final time... var_export() ??? Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589517 Share on other sites More sharing options...
doc1355 Posted August 31, 2021 Author Share Posted August 31, 2021 I created some dummy users, matches and results to make it easier to read the output. Here is the var_export for $pair_id. Please let me know if you need other information: array ( 0 => array ( 'Sam Bina' => array ( 'u2' => 'David Smith', 's1p1' => array ( ), 's1p2' => array ( ), 's2p1' => array ( ), 's2p2' => array ( ), 's3p1' => array ( ), 's3p2' => array ( ), ), ), 1 => array ( 'Sam Bina' => array ( 'u2' => 'Fred Thompson', 's1p1' => array ( ), 's1p2' => array ( ), 's2p1' => array ( ), 's2p2' => array ( ), 's3p1' => array ( ), 's3p2' => array ( ), ), ), 2 => array ( 'Fred Thompson' => array ( 'u2' => 'Frank Molino', 's1p1' => array ( ), 's1p2' => array ( ), 's2p1' => array ( ), 's2p2' => array ( ), 's3p1' => array ( ), 's3p2' => array ( ), ), ), 3 => array ( 'Frank Molino' => array ( 'u2' => 'David Smith', 's1p1' => array ( ), 's1p2' => array ( ), 's2p1' => array ( ), 's2p2' => array ( ), 's3p1' => array ( ), 's3p2' => array ( ), ), ), 4 => array ( 'Sam Bina' => array ( 'u2' => 'Fred Thompson', 's1p1' => array ( 0 => '6', ), 's1p2' => array ( 0 => '2', ), 's2p1' => array ( 0 => '3', ), 's2p2' => array ( 0 => '6', ), 's3p1' => array ( ), 's3p2' => array ( ), ), ), 5 => array ( 'Frank Molino' => array ( 'u2' => 'David Smith', 's1p1' => array ( 0 => '6', ), 's1p2' => array ( 0 => '1', ), 's2p1' => array ( 0 => '6', ), 's2p2' => array ( 0 => '2', ), 's3p1' => array ( ), 's3p2' => array ( ), ), ), 6 => array ( 'Fred Thompson' => array ( 'u2' => 'Frank Molino', 's1p1' => array ( 0 => '6', ), 's1p2' => array ( 0 => '2', ), 's2p1' => array ( 0 => '7', ), 's2p2' => array ( 0 => '5', ), 's3p1' => array ( ), 's3p2' => array ( ), ), ), 7 => array ( 'Fred Thompson' => array ( 'u2' => 'David Smith', 's1p1' => array ( 0 => '5', ), 's1p2' => array ( 0 => '7', ), 's2p1' => array ( 0 => '7', ), 's2p2' => array ( 0 => '5', ), 's3p1' => array ( 0 => '6', ), 's3p2' => array ( 0 => '3', ), ), ), 8 => array ( 'Fred Thompson' => array ( 'u2' => 'David Smith', 's1p1' => array ( 0 => '6', ), 's1p2' => array ( 0 => '2', ), 's2p1' => array ( 0 => '6', ), 's2p2' => array ( 0 => '1', ), 's3p1' => array ( ), 's3p2' => array ( ), ), ), 9 => array ( 'Sam Bina' => array ( 'u2' => 'Frank Molino', 's1p1' => array ( 0 => '1', ), 's1p2' => array ( 0 => '6', ), 's2p1' => array ( 0 => '2', ), 's2p2' => array ( 0 => '6', ), 's3p1' => array ( ), 's3p2' => array ( ), ), ), 10 => array ( 'Sam Bina' => array ( 'u2' => 'David Smith', 's1p1' => array ( 0 => '3', ), 's1p2' => array ( 0 => '6', ), 's2p1' => array ( 0 => '6', ), 's2p2' => array ( 0 => '3', ), 's3p1' => array ( 0 => '6', ), 's3p2' => array ( 0 => '2', ), ), ), 11 => array ( 'Sam Bina' => array ( 'u2' => 'Frank Molino', 's1p1' => array ( 0 => '6', ), 's1p2' => array ( 0 => '4', ), 's2p1' => array ( 0 => '6', ), 's2p2' => array ( 0 => '4', ), 's3p1' => array ( ), 's3p2' => array ( ), ), ), ) Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589521 Share on other sites More sharing options...
doc1355 Posted August 31, 2021 Author Share Posted August 31, 2021 Ok, let's try one more time. I changed my array script to this: $match[] = array( 'p1' => $related_user_1[ 0 ][ 'ID' ], 'p2' => $related_user_2[ 0 ][ 'ID' ], 's1p1' => $set_1_player_1, 's1p2' => $set_1_player_2, 's2p1' => $set_2_player_1, 's2p2' => $set_2_player_2, 's3p1' => $set_3_player_1, 's3p2' => $set_3_player_2, ); This will give me the following var_export: array ( 0 => array ( 'p1' => 'Sam Bina', 'p2' => 'David Smith', 's1p1' => array ( ), 's1p2' => array ( ), 's2p1' => array ( ), 's2p2' => array ( ), 's3p1' => array ( ), 's3p2' => array ( ), ), 1 => array ( 'p1' => 'Sam Bina', 'p2' => 'Fred Thompson', 's1p1' => array ( ), 's1p2' => array ( ), 's2p1' => array ( ), 's2p2' => array ( ), 's3p1' => array ( ), 's3p2' => array ( ), ), 2 => array ( 'p1' => 'Fred Thompson', 'p2' => 'Frank Molino', 's1p1' => array ( ), 's1p2' => array ( ), 's2p1' => array ( ), 's2p2' => array ( ), 's3p1' => array ( ), 's3p2' => array ( ), ), 3 => array ( 'p1' => 'Frank Molino', 'p2' => 'David Smith', 's1p1' => array ( ), 's1p2' => array ( ), 's2p1' => array ( ), 's2p2' => array ( ), 's3p1' => array ( ), 's3p2' => array ( ), ), 4 => array ( 'p1' => 'Sam Bina', 'p2' => 'Fred Thompson', 's1p1' => array ( 0 => '6', ), 's1p2' => array ( 0 => '2', ), 's2p1' => array ( 0 => '3', ), 's2p2' => array ( 0 => '6', ), 's3p1' => array ( ), 's3p2' => array ( ), ), 5 => array ( 'p1' => 'Frank Molino', 'p2' => 'David Smith', 's1p1' => array ( 0 => '6', ), 's1p2' => array ( 0 => '1', ), 's2p1' => array ( 0 => '6', ), 's2p2' => array ( 0 => '2', ), 's3p1' => array ( ), 's3p2' => array ( ), ), 6 => array ( 'p1' => 'Fred Thompson', 'p2' => 'Frank Molino', 's1p1' => array ( 0 => '6', ), 's1p2' => array ( 0 => '2', ), 's2p1' => array ( 0 => '7', ), 's2p2' => array ( 0 => '5', ), 's3p1' => array ( ), 's3p2' => array ( ), ), 7 => array ( 'p1' => 'Fred Thompson', 'p2' => 'David Smith', 's1p1' => array ( 0 => '5', ), 's1p2' => array ( 0 => '7', ), 's2p1' => array ( 0 => '7', ), 's2p2' => array ( 0 => '5', ), 's3p1' => array ( 0 => '6', ), 's3p2' => array ( 0 => '3', ), ), 8 => array ( 'p1' => 'Fred Thompson', 'p2' => 'David Smith', 's1p1' => array ( 0 => '6', ), 's1p2' => array ( 0 => '2', ), 's2p1' => array ( 0 => '6', ), 's2p2' => array ( 0 => '1', ), 's3p1' => array ( ), 's3p2' => array ( ), ), 9 => array ( 'p1' => 'Sam Bina', 'p2' => 'Frank Molino', 's1p1' => array ( 0 => '1', ), 's1p2' => array ( 0 => '6', ), 's2p1' => array ( 0 => '2', ), 's2p2' => array ( 0 => '6', ), 's3p1' => array ( ), 's3p2' => array ( ), ), 10 => array ( 'p1' => 'Sam Bina', 'p2' => 'David Smith', 's1p1' => array ( 0 => '3', ), 's1p2' => array ( 0 => '6', ), 's2p1' => array ( 0 => '6', ), 's2p2' => array ( 0 => '3', ), 's3p1' => array ( 0 => '6', ), 's3p2' => array ( 0 => '2', ), ), 11 => array ( 'p1' => 'Sam Bina', 'p2' => 'Frank Molino', 's1p1' => array ( 0 => '6', ), 's1p2' => array ( 0 => '4', ), 's2p1' => array ( 0 => '6', ), 's2p2' => array ( 0 => '4', ), 's3p1' => array ( ), 's3p2' => array ( ), ), ) Can this be used to create the table? Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589526 Share on other sites More sharing options...
Barand Posted August 31, 2021 Share Posted August 31, 2021 Getting better!. Change to $match[] = array( 'p1' => $related_user_1[ 0 ][ 'ID' ], 'p2' => $related_user_2[ 0 ][ 'ID' ], 's1p1' => $set_1_player_1[0] ?? '', 's1p2' => $set_1_player_2[0] ?? '', 's2p1' => $set_2_player_1[0] ?? '', 's2p2' => $set_2_player_2[0] ?? '', 's3p1' => $set_3_player_1[0] ?? '', 's3p2' => $set_3_player_2[0] ?? '', ); That should change the array from [11] => Array ( [p1] => Sam Bina [p2] => Frank Molino [s1p1] => Array ( [0] => 6 ) [s1p2] => Array ( [0] => 4 ) [s2p1] => Array ( [0] => 6 ) [s2p2] => Array ( [0] => 4 ) [s3p1] => Array ( ) [s3p2] => Array ( ) ) to [11] => Array ( [p1] => Sam Bina [p2] => Frank Molino [s1p1] => 6 [s1p2] => 4 [s2p1] => 6 [s2p2] => 4 [s3p1] => [s3p2] => ) 1 Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589530 Share on other sites More sharing options...
Solution doc1355 Posted September 1, 2021 Author Solution Share Posted September 1, 2021 @Barand Thanks to your last suggestion, I was able to make the table the way I wanted. Here is what I did: I add all the players to an array. I created an array to collect all the players ID, and a separate array to collect all the data for each match: while ( $match_query->have_posts() ) { $match_query->the_post(); $match_id = get_the_ID(); // Get the ID of each Match post in this event. $related_user_1 = get_post_meta( $match_id, 'related_user_1' ); $related_user_2 = get_post_meta( $match_id, 'related_user_2' ); $set_1_player_1 = get_post_meta( $match_id, 'set_1_player_1' ); $set_1_player_2 = get_post_meta( $match_id, 'set_1_player_2' ); $set_2_player_1 = get_post_meta( $match_id, 'set_2_player_1' ); $set_2_player_2 = get_post_meta( $match_id, 'set_2_player_2' ); $set_3_player_1 = get_post_meta( $match_id, 'set_3_player_1' ); $set_3_player_2 = get_post_meta( $match_id, 'set_3_player_2' ); $all_players[] = $related_user_1[ 0 ][ ' ID' ]; $all_players[] = $related_user_2[ 0 ][ ' ID' ]; $match_array[] = array( 'p1' => $related_user_1[ 0 ][ ' ID' ], 'p2' => $related_user_2[ 0 ][ ' ID' ], 's1p1' => $set_1_player_1[ 0 ] ?? '', 's1p2' => $set_1_player_2[ 0 ] ?? '', 's2p1' => $set_2_player_1[ 0 ] ?? '', 's2p2' => $set_2_player_2[ 0 ] ?? '', 's3p1' => $set_3_player_1[ 0 ] ?? '', 's3p2' => $set_3_player_2[ 0 ] ?? '', ); } Then I removed the duplicate players from the player array: $cleaned_player_id = array_unique( $all_players ); Now here comes the table: First I enter the unique player from the cleaned list into the first column. Then I loop through match data with if statement and show the results accordingly. $output = '<table class="uk-table uk-table-striped uk-table-small"><tbody>'; foreach ( $cleaned_player_id as $c ) { $output .= '<tr><td>' . $c . '</td>'; foreach ( $match_array as $m ) { if ( $m[ 'p1' ] == $c ) { $output .= '<td>' . $m[ 'p2' ] . '<br>' . $m[ 's1p1' ] . '-' . $m[ 's1p2' ] . ', ' . $m[ 's2p1' ] . '-' . $m[ 's2p2' ] . ', ' . $m[ 's3p1' ] . '-' . $m[ 's3p2' ] . '</td>'; } elseif ( $m[ 'p2' ] == $c ) { $output .= '<td>' . $m[ 'p1' ] . '<br>' . $m[ 's1p2' ] . '-' . $m[ 's1p1' ] . ', ' . $m[ 's2p2' ] . '-' . $m[ 's2p1' ] . ', ' . $m[ 's3p2' ] . '-' . $m[ 's3p1' ] . '</td>'; } } $output .= '</tr></tbody></table>'; } Please let me know your thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589537 Share on other sites More sharing options...
doc1355 Posted September 1, 2021 Author Share Posted September 1, 2021 Correction the closing tag for the tbody and table should be outside the the last foreach loop. $output = '<table class="uk-table uk-table-striped uk-table-small"><tbody>'; foreach ( $cleaned_player_id as $c ) { $output .= '<tr><td>' . $c . '</td>'; foreach ( $match_array as $m ) { if ( $m[ 'p1' ] == $c ) { $output .= '<td>' . $m[ 'p2' ] . '<br>' . $m[ 's1p1' ] . '-' . $m[ 's1p2' ] . ', ' . $m[ 's2p1' ] . '-' . $m[ 's2p2' ] . ', ' . $m[ 's3p1' ] . '-' . $m[ 's3p2' ] . '</td>'; } elseif ( $m[ 'p2' ] == $c ) { $output .= '<td>' . $m[ 'p1' ] . '<br>' . $m[ 's1p2' ] . '-' . $m[ 's1p1' ] . ', ' . $m[ 's2p2' ] . '-' . $m[ 's2p1' ] . ', ' . $m[ 's3p2' ] . '-' . $m[ 's3p1' ] . '</td>'; } } $output .= '</tr>'; } $output .= '</tbody></table>'; Quote Link to comment https://forums.phpfreaks.com/topic/313633-organize-my-array/#findComment-1589539 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.