Jump to content

Combine array into inline output


Kemut

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.