0

The array:

Array
(
    [0] => Array
        (
            [0] => Service Simulators
            [1] => Array
                (
                    [0] => 1.php
                    [1] => Title 1
                )

        )

    [1] => Array
        (
            [0] => Service Simulators
            [1] => Array
                (
                    [0] => 2.php
                    [1] => Title 2
                )

        )

    [2] => Array
        (
            [0] => Service Simulators
            [1] => Array
                (
                    [0] => 3.php
                    [1] => Title 3
                )

        )

    [3] => Array
        (
            [0] => Service Simulators
            [1] => Array
                (
                    [0] => 4.php
                    [1] => Title 4
                )

        )

    [4] => Array
        (
            [0] => Service Simulators
            [1] => Array
                (
                    [0] => 5.php
                    [1] => Title 5
                )

        )

    [5] => Array
        (
            [0] => Trouble Shooting
            [1] => Array
                (
                    [0] => a.php
                    [1] => Title A
                )

        )

    [6] => Array
        (
            [0] => Trouble Shooting
            [1] => Array
                (
                    [0] => b.php
                    [1] => Title B
                )

        )

    [7] => Array
        (
            [0] => Deprecated
            [1] => Array
                (
                    [0] => 123.php
                    [1] => Title 123
                )

        )

    [8] => Array
        (
            [0] => Admin Only
            [1] => Array
                (
                    [0] => admin.php
                    [1] => Title Admin
                )

        )
)

What I need

Array
(
    [0] => Array
        (
            [0] => Service Simulators
            [1] => Array
                (
                    [0] => 1.php
                    [1] => Title 1
                    [3] => 2.php
                    [4] => Title 2
                    [5] => 3.php
                    [6] => Title 3
                    [7] => 4.php
                    [8] => Title 4
                    [9] => 5.php
                    [10] => Title 5
                )

        )

    [1] => Array
        (
            [0] => Trouble Shooting
            [1] => Array
                (
                    [0] => a.php
                    [1] => Title A
                    [3] => b.php
                    [4] => Title B
                )

        )

    [2] => Array
        (
            [0] => Deprecated
            [1] => Array
                (
                    [0] => 123.php
                    [1] => Title 123
                )

        )

    [3] => Array
        (
            [0] => Admin Only
            [1] => Array
                (
                    [0] => admin.php
                    [1] => Title Admin
                )

        )
)

Whats happening

Array
(
    [0] => Array
        (
            [0] => Service Simulators
            [1] => Array
                (
                    [0] => 5.php
                    [1] => Title 5
                )

        )

    [1] => Array
        (
            [0] => Trouble Shooting
            [1] => Array
                (
                    [0] => b.php
                    [1] => Title B
                )

        )

    [2] => Array
        (
            [0] => Deprecated
            [1] => Array
                (
                    [0] => 123.php
                    [1] => Title 123
                )

        )

    [3] => Array
        (
            [0] => Admin Only
            [1] => Array
                (
                    [0] => admin.php
                    [1] => Title Admin
                )

        )
)

What I'm trying, it's coming from a query result set

$tmp_arr = array();

while($row = mysql_fetch_row($result))
{
   // $row[1] = groupname
   // $row[2] = pagename
   // $row[3] = pagetitle

   // Push to array
   $build_arr_items = array($row[2],$row[3]);

   array_push($tmp_arr, array($row[1], $build_arr_items)); // This builds the first example

   //$tmp_arr[$row[1]]=$build_arr_items; // this is the overwrite example
}

any thoughts, help in the right direction?

3 Answers 3

1

Are you married to that array structure? You might be better served by an array keyed by the group name ie:

Array(
    'Service Simulators' => array(
                              [0] => 1.php
                              [1] => Title 1
                              [3] => 2.php
                              )
      //etc...
      )

Because then you could code like so:

$tmp_arr = array();

while($row = mysql_fetch_row($result))
{
     // $row[1] = groupname
     // $row[2] = pagename
     // $row[3] = pagetitle

      $tmp_arr[$row[1]][] = $row[2];
      $tmp_arr[$row[1]][] = $row[3];
}

This way also seems a bit less messy.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 very good approach. You can as well do it this way and transform it into your desired format, if you really need to.
And I actually think the inner array could be associative as well $tmp_arr[$row[1]][$row[2]] = $row[3]
0

Fixed pivotal's example to keep a similar structure to the op, but setting each by named key:

$tmp_arr = array();
while($row = mysql_fetch_row($result)){
    // $row[1] = groupname
    // $row[2] = pagename
    // $row[3] = pagetitle

    $tmp_arr[ $row[1] ] = array( $row[2], $row[3] );
}
/*
    Output
    array(
        "Group Name" => Array(
            Array(
                0 => "pagename"
                1 => "pagetitle"
            )
         )
    )
*/

Comments

0

OK I think I found something that works. I just reversed the Key Index relations

$tmp_arr[$row[2]]=$row[1];
$tmp_arr[$row[3]]=$row[1];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.