mardi 26 janvier 2016

PHP Create Binary Tree from array

The array:

array:5 [▼
  0 => array:1 [▼
    0 => array:2 [▼
      "right" => CustomerModel {▶} // customer_id = 4
      "left" => CustomerModel {▶} // customer_id = 5
    ]
  ]
  1 => array:2 [▼
    4 => array:2 [▼
      "left" => array:2 [▼
        "parent_customer_id" => 4
        "is_empty" => true
      ]
      "right" => array:2 [▼
        "parent_customer_id" => 4
        "is_empty" => true
      ]
    ]
    5 => array:2 [▼
      "left" => array:2 [▼
        "parent_customer_id" => 5
        "is_empty" => true
      ]
      "right" => array:2 [▼
        "parent_customer_id" => 5
        "is_empty" => true
      ]
    ]
  ]
  2 => null
  3 => null
  4 => null
]

Expected result (let's call the array above $node):

<ul>
  <li> customer_id_4, from $node[0][0]['left']
    <ul>
      <li> * result from $node[1][4]['left'] * </li>
      <li> * result from $node[1][4]['right'] * </li>
    </ul>
  </li>

  <li>
    customer_id_5, from $node[0][0]['right']
    <ul>
      <li> * result from $node[1][5]['left'] * </li>
      <li> * result from $node[1][5]['right'] * </li>
    </ul>
  </li>
</ul>

Is it possible to echo that array to that expected result?

EDIT

Current solution (Laravel Blade):

<ul>
                <li>{{ $user->first_name . ' ' . $user->last_name }}
                    <ul>
                        <?php $node = $nodes[0][0]; ?>
                        @if ( ! is_object($node['left']) )
                        <li>
                            <a href="#">Add new account</a>
                        </li>
                        @else
                        <li>
                            {{ $node['left']->id }}
                            <?php $next = isset($nodes[1][$node->id]) ? null; ?>
                            @if ( ! is_null($next))
                                <ul>
                                    @if ( ! is_object($next['left']) )
                                    <li>
                                        <a href="#">Add New Account</a>
                                    </li>
                                    @else
                                    <li>
                                        {{ $next->id }}
                                        <!-- And next again on 2nd level-->
                                    </li>
                                    @endif
                                </ul>
                            @endif
                        </li>
                        @endif

                        @if ( ! is_object($node['right']) )
                        <li>
                            <a href="#">Add new account</a>
                        </li>
                        @else
                        <li>
                            {{ $node['right']->id }}
                        </li>
                        @endif
                    </ul>
                </li>
            </ul>

As you can see, to iterate to 5 levels, I have to do it manually, any chance to use @for or @foreach?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire