Jump to content

PHP Multidimensional Arrays


Go to solution Solved by Barand,

Recommended Posts

Hello All,

 

I have a piece of code that generates a multi-dimensional array from values in a database, but I'm wondering how I would search and add a new element to each parent:

Array
(
    [52] => Array
        (
            [Article_ID] => 52
            [Branch_ID] => 0
            [Article_Name] => @Test
            [Children] => Array
                (
                    [53] => Array
                        (
                            [Article_ID] => 53
                            [Branch_ID] => 52
                            [Article_Name] => Testing
                        )

                )

        )

    [46] => Array
        (
            [Article_ID] => 46
            [Branch_ID] => 0
            [Article_Name] => Client Information
            [Children] => Array
                (
                    [54] => Array
                        (
                            [Article_ID] => 54
                            [Branch_ID] => 46
                            [Article_Name] => Testing Audit
                        )

                )

        )

    [1] => Array
        (
            [Article_ID] => 1
            [Branch_ID] => 0
            [Article_Name] => Knowledge Base - How To
            [Children] => Array
                (
                    [38] => Array
                        (
                            [Article_ID] => 38
                            [Branch_ID] => 1
                            [Article_Name] => Bugs
                            [Children] => Array
                                (
                                    [50] => Array
                                        (
                                            [Article_ID] => 50
                                            [Branch_ID] => 38
                                            [Article_Name] => Another Branch
                                        )

                                )

                        )

                    [49] => Array
                        (
                            [Article_ID] => 49
                            [Branch_ID] => 1
                            [Article_Name] => Features
                        )

                )

        )

    [55] => Array
        (
            [Article_ID] => 55
            [Branch_ID] => 0
            [Article_Name] => Testing
            [Children] => Array
                (
                    [57] => Array
                        (
                            [Article_ID] => 57
                            [Branch_ID] => 55
                            [Article_Name] => New Article
                        )

                )

        )

)

For example, how would I search for "Another Branch" and add the element "Active" => true to each parent until it reaches the first parent?

 

 

 

Kind Regards,

Andy

Link to post
Share on other sites

Hello Barand,

 

I don't want it on them all. - Basically, when the page is loaded, the Article_ID is in the URL so I need to find that Article_ID in the array and make it so all of it's parents have the element "Active". I'm trying to make it so I can add a class to all of the current pages parents <ul> tags so that part of the navigation is expanded.

 

I have a jQuery solution in place, but it flickers when the page is loaded to "expand" the tree, which is annoying me, so I'm trying to come up with a solution where jQuery isn't required and it's done through the PHP and CSS.

 

Hope that makes sense?

 

 

 

Kind Regards,

Andy

Link to post
Share on other sites

I don't want it on them all.

 

And who said you wanted it on all of them? He was saying that when you build the array you should make the determination as to which elements to make active.

 

Also, your statements are confusing. In the first post you state you want to search for an element by a string value, but in your last post you state you know the Article_ID. So, why would you be searching for a text value if you know the Article_ID?

Edited by Psycho
Link to post
Share on other sites

As Psycho said.

 

If you have a query like this

SELECT a.article_id as aid
     , a.branch_id      
     , a.article_name  as aname
     , b.article_id as bid
     , b.article_name as bname
     , c.article_id as cid
     , c.article_name as cname
     , CASE
        WHEN 50 IN (a.article_id, b.article_id, c.article_id) THEN 1
        ELSE 0
        END as active
FROM article a
        LEFT JOIN
     article b ON a.article_id = b.branch_id   
        LEFT JOIN
     article c ON b.article_id = c.branch_id
where a.branch_id = 0
ORDER BY aid, bid, cid

which would give you something like this

+-----+-----------+-------------------------+------+---------------+------+----------------+--------+
| aid | branch_id | aname                   | bid  | bname         | cid  | cname          | active |
+-----+-----------+-------------------------+------+---------------+------+----------------+--------+
|   1 |         0 | Knowledge Base - How To |   38 | Bugs          |   50 | Another Branch |      1 |
|   1 |         0 | Knowledge Base - How To |   49 | Features      | NULL | NULL           |      0 |
|  46 |         0 | Client Information      |   54 | Testing Audit | NULL | NULL           |      0 |
|  52 |         0 | @Test                   |   53 | testing       | NULL | NULL           |      0 |
|  55 |         0 | Testing                 |   57 | New Article   | NULL | NULL           |      0 |
+-----+-----------+-------------------------+------+---------------+------+----------------+--------+

Then you can see from the active (first) line that ids 1, 38 and 50 should be marked as active.

Link to post
Share on other sites

Hello,

 

Thank you for the above. - Wouldn't that method require the depth of the array to be hard coded? Currently, the array is built dynamically so it can have 1 child or 100 children. The array is built using this function:

    function BuildTreeArray(array &$Elements, $BranchID = 0) {
        $Branch = array();

        foreach ($Elements as $Element) {
            if ($Element['Branch_ID'] == $BranchID) {
                $Children = $this->BuildTreeArray($Elements, $Element['Article_ID']);
                if ($Children) {
                    $Element['Children'] = $Children;
                }
                $Branch[$Element['Article_ID']] = $Element;
                unset($Elements[$Element['Article_ID']]);
            }
        }
        return $Branch;
    }

and the elements are from:

        $Elements = array();
        
        while($Fetch = $this->Fetch($Query)) {
            $Elements[] = array("Article_ID"=>$Fetch['Article_ID'],
                                "Branch_ID"=>$Fetch['Branch_ID'],
                                "Article_Name"=>$Fetch['Article_Name']);
        }

Sorry if I'm just being stupid, I've gone blind looking at this...

 

 

 

Kind Regards,

Andy

Link to post
Share on other sites

The number of children is irrelevant. Using that method it's the number of generations of children and grandchildren that is hard coded.

 

Your data showed a max of  three generations. Is there a known maximum?

Link to post
Share on other sites
  • Solution

This will set an active flag in your elements array

$res = $db->query("SELECT article_id
                        , branch_id
                        , article_name
                        , 0 as active
                   FROM article");
$Elements = [];
foreach ($res as $el) {
    $Elements[$el['article_id']] = $el;
}


$activeId = 50;

setActiveElement($Elements, $activeId);

function setActiveElement(&$els, $id)
{
    $els[$id]['active'] = 1;
    if ($els[$id]['branch_id'] != 0) {
        setActiveElement($els, $els[$id]['branch_id']);
    }
}

As you can see, 1, 38 and 50 are now set "active"

 

Array
(
[1] => Array
(
[article_id] => 1
[branch_id] => 0
[article_name] => Knowledge Base - How To
[active] => 1                                                          <---------------
)

[38] => Array
(
[article_id] => 38
[branch_id] => 1
[article_name] => Bugs
[active] => 1                                                          <---------------
)

[46] => Array
(
[article_id] => 46
[branch_id] => 0
[article_name] => Client Information
[active] => 0
)

[49] => Array
(
[article_id] => 49
[branch_id] => 1
[article_name] => Features
[active] => 0
)

[50] => Array
(
[article_id] => 50
[branch_id] => 38
[article_name] => Another Branch
[active] => 1                                                          <---------------
)

[52] => Array
(
[article_id] => 52
[branch_id] => 0
[article_name] => @Test
[active] => 0
)

[53] => Array
(
[article_id] => 53
[branch_id] => 52
[article_name] => testing
[active] => 0
)

[54] => Array
(
[article_id] => 54
[branch_id] => 46
[article_name] => Testing Audit
[active] => 0
)

[55] => Array
(
[article_id] => 55
[branch_id] => 0
[article_name] => Testing
[active] => 0
)

[57] => Array
(
[article_id] => 57
[branch_id] => 55
[article_name] => New Article
[active] => 0
)

)
  • Like 1
Link to post
Share on other sites

Don't seem to have an edit option on my previous post but I would just like to say thank you for all your help Barand. - I had to modify my array generation slightly to match your code as it didn't work in the multi-dimensional array, but I've now got it working and achieved what I wanted to do.

 

Thank you again!

 

 

 

Kind Regards,

Andy

  • Like 1
Link to post
Share on other sites

I get the impression you are just posting here for posting's sake. Posting's fine if you are offering up something constructive but if you just feel that you have to post every thought that comes into your head, go to Twitter.

Link to post
Share on other sites
This thread is more than a year old.

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.