0

i have an array like this:

$arr = array(array('1', '0', 'nokia'),
             array('2', '0', 'samsung'),
             array('3', '1', 'n90'),
             array('4', '2', 'galaxy note'),
             array('5', '2', 'galaxy nexus'),
             array('6', '3', '8GB'),
             array('7', '3', '16GB'),
             array('8', '4', '8GB'),
             array('9', '4', '16GB'),
             array('10', '4', '32GB'),
             array('11', '10', 'black'),
             array('12', '10', 'red'),
             );

i want a function to change that to this html code:

<ul>
   <li id="1">nokia
      <ul>
         <li id="3">n90
            <ul>
               <li id="6">8GB</li>
               <li id="7">16GB</li>
            </ul>
         </li>
      </ul>
   </li>
   <li id="2">samsung
      <ul>
         <li id="4">galaxy note
            <ul>
               <li id="8">8GB</li>
               <li id="9">16GB</li>
               <li id="10">32GB
                  <ul>
                     <li id="11">black</li>
                     <li id="12">red</li>
                  </ul>
               </li>
            </ul>
         </li>
         <li id="5">galaxy nexus</li>
      </ul>
   </li>
</ul>
hakre
  • 193,403
  • 52
  • 435
  • 836
mohammad falahat
  • 757
  • 1
  • 4
  • 11
  • 2
    this isn't really possible in any realistic way.... you're array values have no relationship to indicate how they fit together. There isn't a programmatic way to understand what the difference is between "brand name", "model", and "feature" – Trey Jun 24 '12 at 07:01
  • 1
    there is a repationship beetween elements of array and it's parent_id the arrays structure is: id, parent_id, title – mohammad falahat Jun 24 '12 at 07:10
  • Also, usually when you have the phrase "I want.." or "I need this to" in your question and you haven't tried to solve it yourself, you're not going to get much help on SO. We're here to help you solve problems with code you've written, not to write code from scratch for you. – Bailey Parker Jun 24 '12 at 07:11
  • i tried to solve with arrays but i couldn't get the result – mohammad falahat Jun 24 '12 at 07:13
  • if you're pulling this info from a db it would be a much better place to start using joins and groups to create a more understandable array structure – Trey Jun 24 '12 at 07:14
  • I have posted answer for this question though. But, you can give your code to generate this array so we can find more better way to display this. – LIGHT Jun 24 '12 at 07:25
  • What you ask for has been already explained in: [How to obtain a nested HTML list from object's array recordset?](http://stackoverflow.com/q/8020947/367456) (Nov 5 '11) – hakre Jun 23 '13 at 21:20

3 Answers3

1

This question doesn't have that realistic answer. however, I got the solution. You have to use nested foreach for this. comparing array elements. may be there is more simpler way to do this. This my my solution though

<?
$arr = array(array('1', '0', 'nokia'),
             array('2', '0', 'samsung'),
             array('3', '1', 'n90'),
             array('4', '2', 'galaxy note'),
             array('5', '2', 'galaxy nexus'),
             array('6', '3', '8GB'),
             array('7', '3', '16GB'),
             array('8', '4', '8GB'),
             array('9', '4', '16GB'),
             array('10', '4', '32GB'),
             array('11', '10', 'black'),
             array('12', '10', 'red'),
             );
echo "<ul>\n";
foreach($arr as $ar){
    if($ar[1]==0){
        echo "<li id=\"".$ar[0]."\">".$ar[2]."\n";
            echo "<ul>";
                foreach($arr as $ar1){
                    if($ar1[1]==$ar[0]){
                        echo "<li id=\"".$ar1[0]."\">".$ar1[2]."</li>\n";
                        echo "<ul>";
                        foreach($arr as $ar2){
                            if($ar2[1]==$ar1[0]){
                                echo "<li id=\"".$ar2[0]."\">".$ar2[2]."</li>\n";                               
                                echo "<ul>";
                                foreach($arr as $ar3){
                                    if($ar3[1]==$ar2[0]){
                                        echo "<li id=\"".$ar3[0]."\">".$ar3[2]."</li>\n";
                                    }
                                }
                                echo "</ul>";
                            }
                        }
                        echo "</ul>";
                    }
                }
            echo "</ul>";
        echo "</li>";
    }
}
echo "</ul>\n";
?>
LIGHT
  • 5,604
  • 10
  • 35
  • 78
1

i solved it with PHP DOMDocument

it is useful also in tables with this structure: id, parent_id, title[, other fields]

$dom = new DOMDocument;
foreach($arr as $row){
    $node[$row[0]] = $dom->createElement('li'); // make an element
    $node[$row[0]]->setAttribute('id', $row[0]); // set id to element
    $node[$row[0]]->nodeValue = $row[2]; // set the value to element
    if(!$row[1])
        $dom->appendChild($node[$row[0]]); // append the element to dom document root
    else
        $node[$row[1]]->appendChild($node[$row[0]]); // append the element to that's parent element
    $ul = $dom->createElement('ul');
    $node[$row[0]] = $node[$row[0]]->appendChild($ul);
}

$string = '<ul>'.str_replace('<ul></ul>', '', $dom->saveHTML()).'</ul>'; // delete empty ul's
echo $string;
mohammad falahat
  • 757
  • 1
  • 4
  • 11
1

There is no pattern in your array on which proper function can be built.

i.e (from what i can deduce)
array('1', '0', 'nokia') stores in it ('id_number', 'number', 'company name')
array('11', '10', 'black') stores ('id_number', 'number', 'colour').

There is also nothing to link 'nokia' with 'n90'. you have to separate things you want. Use a db (mysql) or make proper sets in array.

It is best to use a database and use filters, joines, etc.
If you still want it in array, We can try to make some relations.

This is my opinion
Split your array into 4 arrays

  1. company = array( array('company_id', 'company_name'))
  2. handset = array( array('phone_id','company_id', array('color_ids'), 'phone_name')
  3. color = array( array('color_id', 'color_name'))
  4. size = array( array('size_id', 'size_val')

What i have done is implemented a database design in array. now you have something to link each other with.

This is a Psudo-code, you can derive your actual code based from its workflow

foreach(element in company)
{
 foreach(element in nested-array-of-company-element)
  {
   echo "<li id='company_id'>'company_name'</li>"
   echo "<ul>"

   foreach(elements in handset)
   foreach(elements in elements-of-handset)
   if( inarray(company_id) )
    { 
    echo "<li id='phone_id'>phone_name</li>"
    echo "<ul>"
    foreach(elements in color)
     foreach(elements in elements-of-color)
     if( inarray(color_id-in-handset-array) )
     {
     echo "<li id='color_id'>color_name</li>"
     echo "<ul>"
     }
    echo "</ul>"
    echo "<ul>"
    foreach(elements in size)
     foreach(elements in elements-of-size)
     if( inarray(size_id-in-handset-array) )
     {
     echo "<li id='size_id'>size_val</li>"
     echo "<ul>"
     }
echo "</ul>"
}
}
}

In summery you will need:-

Learn about nesting foreach() in php
Learn about in_array() in php
This will be the workflow
li every company list
li ever handset where company_id is in_array()
li every color available filtered by in_array()
li every size available filtered by in_array()

Abhinav Kulshreshtha
  • 2,147
  • 1
  • 23
  • 38
  • the structure of array is : id, parent_id, title – mohammad falahat Jun 24 '12 at 10:01
  • 1
    it is still difficult to get multilevel relationship (in your expected output, there is multilevel from samsung-> galaxynote-> size-> color ). Try Using the Associative array, so it will be easier to visualize during writing the code. `array ([id]->'12',[pid]->'10',[title]->'red')`. – Abhinav Kulshreshtha Jun 24 '12 at 15:10
  • Still it would be better to split it in different arrays. reduces chances of ambiguity, and other logical error. It will also enable you get many-many relationships. i.e. several handsets can be linked to one color, or many color. Your version of array will get more complicated as you add more element in it. – Abhinav Kulshreshtha Jun 24 '12 at 15:16