Neeraj Sharma
Neeraj Sharma

Reputation: 341

PHP - Replace li of one array with another

I am trying to create a tree structure using ul and li. I need to always show a tree structure no matter whether we have values in database or not. So I created a function and made a tree structure. Now I want to replace li data when we have values for those nodes in database. Here is code that is produced by my arrays.

<li><a href="#">1</a>
        <ul><li><a href="#">2</a>
                <ul><li><a href="#">2.1</a>
                        <ul>
                                <li><a href="#">2.1.1</a>                 
                                </li>
                                <li><a href="#">2.1.2</a>   
                                </li>               
                        </ul>
                </li>
                <li>
                        <a href="#">2.2</a>
                        <ul>
                                <li><a href="#">2.2.1</a>                    
                                </li>
                                <li><a href="#">2.2.2</a>     
                                </li>                   
                        </ul>
                </li></ul>
                </li>
        </ul>
</li>

This is the basic structure and I want to replace nodes like 2.1, 2.1.1,2.2.2 when we have values in another array which is giving output like

<li>
        <a>
                <div class="card">
                        <div class="thumb">grey</div>
                        <div class="id">157</div>
                        <div class="username">okamikid1</div>
                </div>
        </a>
        <ul>
                <li><a><div class="card">
                        <div class="thumb">red</div>
                        <div class="id">158</div>
                        <div class="username">okamikid1</div>
                </div>
        </a>
        <ul></ul></li></ul>
</li>

Basically I have two arrays,both arrays have li in it. I want to replace li of first array with second array and want to keep other values of second array those don't exist in first array.

Here is array structure of first array in json form, so that you can see structure more clearly.

{"157":{"id":"157","username":"okamikid1","parent_id":null,"children":[{"id":"158","parent_id":"157","username":"okamikid1","children":[]},{"id":"160","parent_id":"157","username":"okamikid2","children":[]}]}}

Upvotes: 0

Views: 52

Answers (1)

ash__939
ash__939

Reputation: 1599

From your comments, I assume you want to show a tree structure with data from the database. Also, you want to have the node exists in the tree structure even if the value is not present in the database.

I would suggest the following workaround.

Suppose you have two arrays like

$placeholders = [
    1 => [
        '1.1' => '1.1',
        '1.2' => '1.2',
        '1.3' => '1.3',
    ],
    2 => [
        '2.1' => '2.1',
        '2.2' => '2.2',
        '2.3' => '2.3',
    ],
    3 => [
        '3.1' => '3.1',
        '3.2' => '3.2',
        '3.3' => '3.3',
    ]
];

The $placeholders array defines the tree structure.

Now you have some function to fetch data from the database in the following format.

$data = [
    1 => [
        '1.1' => 'Node 1.1',
        '1.2' => 'Node 1.2',
        '1.3' => 'Node 1.3',
    ],
    2 => [
        '2.1' => 'Node 2.1',
        '2.2' => 'Node 2.2',
        '2.3' => 'Node 2.3',
    ]
];

Now, use the array_replace_recursive function to replace the placeholders with actual values from the database.

$data = array_replace_recursive($placeholders, $data);

Then print the tree structure like:

foreach ($data as $base => $nodes)
{
    echo "<ul>";
    echo "<li>{$base}</li>";
    echo "<ul>";
    foreach ($nodes as $node) {
        echo "<li>{$node}</li>";
    }
    echo "</ul>";
    echo "</ul>";
}

Upvotes: 1

Related Questions