De7ilx
De7ilx

Reputation: 45

PHP Nested arrays with while inside while loop

I am trying to achieve the following:

Array (
    [1] => Array (
        [0] => 1
        [1] => 2
    )
    [3] => Array (
        [0] => 5
    )
    [2] => Array (
        [0] => 3
    )
)

This is my current code:

function create_role_node_array()
{
    if($_SESSION['userid'])
    {
        $query1 = "SELECT * FROM auth_user_role_relation_list WHERE user_id='".$_SESSION['userid']."' ORDER BY creation_time ASC";
        $result1 = mysqli_query($GLOBALS['c'], $query1);
        $rows='';
        $data=array();
        if(!empty($result1))
            $rows=mysqli_num_rows($result1);
        else
            $rows='';
        if(!empty($rows))
        {
            while($rows=mysqli_fetch_assoc($result1))
            {
                $query2 = "SELECT * FROM auth_node_role_relation_list WHERE role_id='".$rows['role_id']."' ORDER BY group_id ASC, creation_time ASC";
                $result2 = mysqli_query($GLOBALS['c'], $query2);
                $rows2='';
                $data2=array();
                if(!empty($result2))
                    $rows2=mysqli_num_rows($result2);
                else
                    $rows2='';
                if(!empty($rows2))
                {
                    while($rows2=mysqli_fetch_assoc($result2))
                    {
                        $data2[0].=$rows2['node_id'];
                    }
                }
                $data[$rows['role_id']].=$data2[0];
            }
            return $data;
        }
    }
}
print_r(create_role_node_array());

And the result I am currently getting:

Array ( [1] => 12 [3] => 5 [2] => 3 )

This 12 in first Array section is actually two separate results next to each other.

I checked other topics here but none can be applied to my case. Can anyone knows a solution here? Any help is appreciated.

Upvotes: 0

Views: 312

Answers (1)

mertizci
mertizci

Reputation: 528

Try this one;

<?php
   function create_role_node_array()
{
    if($_SESSION['userid'])
    {
        $query1 = "SELECT * FROM auth_user_role_relation_list WHERE user_id='".$_SESSION['userid']."' ORDER BY creation_time ASC";
        $result1 = mysqli_query($GLOBALS['c'], $query1);
        $rows='';
        $data=array();
        if(!empty($result1))
            $rows=mysqli_num_rows($result1);
        else
            $rows='';
        if(!empty($rows))
        {
            while($rows=mysqli_fetch_assoc($result1))
            {
                $query2 = "SELECT * FROM auth_node_role_relation_list WHERE role_id='".$rows['role_id']."' ORDER BY group_id ASC, creation_time ASC";
                $result2 = mysqli_query($GLOBALS['c'], $query2);
                $rows2='';
                $data2=array();
                if(!empty($result2))
                    $rows2=mysqli_num_rows($result2);
                else
                    $rows2='';
                if(!empty($rows2))
                {
                    $i=0;
                    while($rows2=mysqli_fetch_assoc($result2))
                    {
                        $data2[]=$rows2['node_id'];
                        $i++;
                    }
                }
                $data[$rows['role_id']]=$data2;
                unset($data2);
            }
            return $data;
        }
    }
}
print_r(create_role_node_array());

Upvotes: 1

Related Questions