Kemut Posted January 11, 2013 Share Posted January 11, 2013 How do I combine this array and display an output inline. <?php $array = array( '0' => array( 'id' => 1, 'name' => 'Sizes', 'parent' => 0, 'children' => array( '0' => array('id' => 4, 'name' => 'S', 'parent' => 1), '1' => array('id' => 5, 'name' => 'L', 'parent' => 1), '2' => array('id' => 6, 'name' => 'M', 'parent' => 1) ) ), '1' => array( 'id' => 2, 'name' => 'Colors', 'parent' => 0, 'children' => array( '0' => array('id' => 7, 'name' => 'White', 'parent' => 2), '1' => array('id' => 8, 'name' => 'Black', 'parent' => 2) ) ), '2' => array( 'id' => 3, 'name' => 'Types', 'parent' => 0, 'children' => array( '0' => array('id' => 9, 'name' => 'Polyester', 'parent' => 3), '1' => array('id' => 10, 'name' => 'Lycra', 'parent' => 3) ) ) ); My desire output should be like this <table> <tr><td>Sizes, Colors & Types</td></tr> <tr><td>[s] [White] [Polyester]</td></tr> <tr><td>[s] [White] [Lycra]</td></tr> <tr><td>[s] [black] [Polyester]</td></tr> <tr><td>[s] [black] [Lycra]</td></tr> <tr><td>[L] [White] [Polyester]</td></tr> <tr><td>[L] [White] [Lycra]</td></tr> <tr><td>[L] [black] [Polyester]</td></tr> <tr><td>[L] [black] [Lycra]</td></tr> <tr><td>[M] [White] [Polyester]</td></tr> <tr><td>[M] [White] [Lycra]</td></tr> <tr><td>[M] [black] [Polyester]</td></tr> <tr><td>[M] [black] [Lycra]</td></tr> </table> Let me know how to build the recursive function to display the output Quote Link to comment https://forums.phpfreaks.com/topic/272987-combine-array-into-inline-output/ Share on other sites More sharing options...
requinix Posted January 11, 2013 Share Posted January 11, 2013 You'd use a recursive function to turn that array into something easier to manage. Recursion isn't suitable for actually creating that output. Function takes one $argument in the form of the array you have there. It returns two pieces of information: an array with the parts of the title (ie, ["Sizes", "Colors", "Types"]) and an array of arrays containing the individual values. 0. If the $argument is empty then abort 1. If the $argument has one items then 1. Make the sole item in the $argument be the $current array 2. Make $title and $values both empty arrays Else it has more than one item so 1. Pop off the first one as the $current array 2. Call the function recursively with what is left 3. Grab the $title and $values the function returned 2. Put the $current title at the beginning of the $title array 3. Set up a $new empty array 4. Loop through each $child in the $current array 1. Loop through each $value in the array of $values 1. Since $value is just a copy, put $child at the beginning of it 2. Add the entire modified $value to the $new array 5. Return the $new array To print it out, 1. Grab the $title and $values array that the function returned 2. Because there is no comma before the & you need some special treatment to display the $title properly: 1. If there is one item then 1. Display it as is Else if there are two items then 1. Display them as "First & Second" Else there are more than two items so [*] 1. Display everything except the last two items in the array as "Item, " 2. Display the second-to-last item as "Item & " 3. Display the last item 3. Loop through each $value in the $values array 1. Display each item in $value as "[item]" with spaces between them [**] * array_slice and implode may be handy here ** implode() will make this much easier Quote Link to comment https://forums.phpfreaks.com/topic/272987-combine-array-into-inline-output/#findComment-1404839 Share on other sites More sharing options...
Christian F. Posted January 11, 2013 Share Posted January 11, 2013 You don't happen to get these values from a database, or similar, are you? Quote Link to comment https://forums.phpfreaks.com/topic/272987-combine-array-into-inline-output/#findComment-1404895 Share on other sites More sharing options...
Kemut Posted January 11, 2013 Author Share Posted January 11, 2013 You don't happen to get these values from a database, or similar, are you? The array generate from databases. It's dynamic values. Quote Link to comment https://forums.phpfreaks.com/topic/272987-combine-array-into-inline-output/#findComment-1404907 Share on other sites More sharing options...
Kemut Posted January 11, 2013 Author Share Posted January 11, 2013 You'd use a recursive function to turn that array into something easier to manage. Recursion isn't suitable for actually creating that output. Function takes one $argument in the form of the array you have there. It returns two pieces of information: an array with the parts of the title (ie, ["Sizes", "Colors", "Types"]) and an array of arrays containing the individual values. 0. If the $argument is empty then abort 1. If the $argument has one items then 1. Make the sole item in the $argument be the $current array 2. Make $title and $values both empty arrays Else it has more than one item so 1. Pop off the first one as the $current array 2. Call the function recursively with what is left 3. Grab the $title and $values the function returned 2. Put the $current title at the beginning of the $title array 3. Set up a $new empty array 4. Loop through each $child in the $current array 1. Loop through each $value in the array of $values 1. Since $value is just a copy, put $child at the beginning of it 2. Add the entire modified $value to the $new array 5. Return the $new array To print it out, 1. Grab the $title and $values array that the function returned 2. Because there is no comma before the & you need some special treatment to display the $title properly: 1. If there is one item then 1. Display it as is Else if there are two items then 1. Display them as "First & Second" Else there are more than two items so [*] 1. Display everything except the last two items in the array as "Item, " 2. Display the second-to-last item as "Item & " 3. Display the last item 3. Loop through each $value in the $values array 1. Display each item in $value as "[item]" with spaces between them [**] * array_slice and implode may be handy here ** implode() will make this much easier Great step and I'll try to figure it out Quote Link to comment https://forums.phpfreaks.com/topic/272987-combine-array-into-inline-output/#findComment-1404908 Share on other sites More sharing options...
Muddy_Funster Posted January 11, 2013 Share Posted January 11, 2013 The array generate from databases. It's dynamic values. what? Quote Link to comment https://forums.phpfreaks.com/topic/272987-combine-array-into-inline-output/#findComment-1404910 Share on other sites More sharing options...
Christian F. Posted January 11, 2013 Share Posted January 11, 2013 Since you're fetching the values from the database, you should combine (JOIN) them i the query. No need to go about it the hard, and long way, of manually duplicating basic SQL logic. Quote Link to comment https://forums.phpfreaks.com/topic/272987-combine-array-into-inline-output/#findComment-1404917 Share on other sites More sharing options...
Kemut Posted January 11, 2013 Author Share Posted January 11, 2013 Since you're fetching the values from the database, you should combine (JOIN) them i the query. No need to go about it the hard, and long way, of manually duplicating basic SQL logic. Nesty looks like will make more queries to the DB. http://codepad.org/6OFkbWoc example I have done with manually loops. I'm a bit blur to make recursive function to the depth. Quote Link to comment https://forums.phpfreaks.com/topic/272987-combine-array-into-inline-output/#findComment-1404922 Share on other sites More sharing options...
Christian F. Posted January 11, 2013 Share Posted January 11, 2013 (edited) Nope, you actually only need one query to fetch all of the information you want. That's why I mentioned JOINs. Then you can build all three option lists with the same loop. Edited January 11, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/272987-combine-array-into-inline-output/#findComment-1404930 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.