PHP数组覆盖值但需要添加到数组

发布于 2024-08-08 13:35:12 字数 4702 浏览 8 评论 0原文

数组:

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
                )

        )
)

我需要什么

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
                )

        )
)

发生了

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
                )

        )
)

什么我正在尝试,它来自查询结果集

$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
}

任何想法,在正确的方向上有帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太傻旳人生 2024-08-15 13:35:12

您是否已与该数组结构结婚了?您可能会更好地使用以组名称为键的数组,即:

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

因为这样您就可以像这样编写代码:

$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];
}

这种方式看起来也不太混乱。

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.

可遇━不可求 2024-08-15 13:35:12

修复了 Pivotal 的示例以保持与操作类似的结构,但通过命名键设置每个:

$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"
            )
         )
    )
*/

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"
            )
         )
    )
*/
ゃ懵逼小萝莉 2024-08-15 13:35:12

好吧,我想我找到了有用的东西。我刚刚颠倒了关键索引关系

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文