在php中重组数组

发布于 2024-11-30 05:52:34 字数 1610 浏览 0 评论 0原文

我有以下源数组,我正在尝试使用 group_name 对其进行重组,以提供下面的所需输出。

任何帮助或指示表示赞赏。 谢谢,Lk

来源:

Array
(
[field_a] => Array
    (
        [type] => text
        [group_name] => 
    )

[field_b] => Array
    (
        [type] => number_integer
        [group_name] => group_other
    )

[field_c] => Array
    (
        [type] => text
        [group_name] => 
    )

[field_d] => Array
    (
        [type] => link
        [group_name] => group_web_links
    )

[field_e] => Array
    (
        [type] => link
        [group_name] => group_web_links
    )

[field_f] => Array
    (
        [type] => text
        [group_name] => group_web_links
    )

)

输出:

Array
(
[group_other] => Array
    (
        [field_b] => Array
           (
               [type] => number_integer
               [group_name] => group_other
           )
    )

[group_web_links] => Array
    (
        [field_d] => Array
            (
                [type] => link
                [group_name] => group_web_links
            )

        [field_e] => Array
            (
                [type] => link
                [group_name] => group_web_links
            )

        [field_f] => Array
            (
                [type] => text
                [group_name] => group_web_links
            )
    )

[field_a] => Array
    (
        [type] => text
        [group_name] => 
    )

[field_c] => Array
    (
        [type] => text
        [group_name] => 
    )
)

I have the following source array which I'm trying to restructure using group_name to give me the require output below.

Any help or pointers appreciated.
Thanks, Lk

Source:

Array
(
[field_a] => Array
    (
        [type] => text
        [group_name] => 
    )

[field_b] => Array
    (
        [type] => number_integer
        [group_name] => group_other
    )

[field_c] => Array
    (
        [type] => text
        [group_name] => 
    )

[field_d] => Array
    (
        [type] => link
        [group_name] => group_web_links
    )

[field_e] => Array
    (
        [type] => link
        [group_name] => group_web_links
    )

[field_f] => Array
    (
        [type] => text
        [group_name] => group_web_links
    )

)

Output:

Array
(
[group_other] => Array
    (
        [field_b] => Array
           (
               [type] => number_integer
               [group_name] => group_other
           )
    )

[group_web_links] => Array
    (
        [field_d] => Array
            (
                [type] => link
                [group_name] => group_web_links
            )

        [field_e] => Array
            (
                [type] => link
                [group_name] => group_web_links
            )

        [field_f] => Array
            (
                [type] => text
                [group_name] => group_web_links
            )
    )

[field_a] => Array
    (
        [type] => text
        [group_name] => 
    )

[field_c] => Array
    (
        [type] => text
        [group_name] => 
    )
)

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

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

发布评论

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

评论(4

鸩远一方 2024-12-07 05:52:34

这可以通过一个简单的 foreach 循环来完成:

$start_array; // The array you want to convert
$end_array = array (); // The final array
foreach ($start_array as $index => $value) {
    if (empty($value["group_name"])) {
        $end_array[$index] = $value;
    } else {
        $end_array[$value["group_name"]][$index] = $value;
    }
}

This can be accomplished by a simple foreach loop:

$start_array; // The array you want to convert
$end_array = array (); // The final array
foreach ($start_array as $index => $value) {
    if (empty($value["group_name"])) {
        $end_array[$index] = $value;
    } else {
        $end_array[$value["group_name"]][$index] = $value;
    }
}
空城仅有旧梦在 2024-12-07 05:52:34

如果新数组的键顺序不是那么重要:

$new_array = array();
foreach ($old_array as $key => $value) {
    if (empty($value['group_name'])) $new_array[$key] = $value;
    else {
        if (!array_key_exists($value['group_name'], $new_array) $new_array[$value['group_name']] = array();
        $new_array[$value['group_name']][$key] = $value;
    }
}

If key order of new array is not so important:

$new_array = array();
foreach ($old_array as $key => $value) {
    if (empty($value['group_name'])) $new_array[$key] = $value;
    else {
        if (!array_key_exists($value['group_name'], $new_array) $new_array[$value['group_name']] = array();
        $new_array[$value['group_name']][$key] = $value;
    }
}
寄人书 2024-12-07 05:52:34

像这样的东西:

$array_dst = array();
foreach ($array_src as $group_key=>$group_data){
    $group_name = $group_data['group_name'];
    if (empty($array_dst[$group_name]))
        $array_dst[$group_name] = array();
    $array_dst[$group_name][$group_key] = $group_data;
}

Something like this:

$array_dst = array();
foreach ($array_src as $group_key=>$group_data){
    $group_name = $group_data['group_name'];
    if (empty($array_dst[$group_name]))
        $array_dst[$group_name] = array();
    $array_dst[$group_name][$group_key] = $group_data;
}
不忘初心 2024-12-07 05:52:34
$new_array = array();
foreach( $old_array as $field_id => $field ) {
    if( $field[ 'group_name' ] == '' ) {
       $new_array[ $field_id ] = $field;
    } else {
       $new_array[ $field[ 'group_name' ] ][ $field_id ] = $field;
    }
}

我想那些对此投反对票的人正在这样做,因为我没有初始化 $new_array[ $field[ 'group_name' ] ]。好吧,这在 PHP 中不是必需的,当您执行 $new_array[ $field[ 'group_name' ] ][ $field_id ] = $field; 时,它将自动用空数组初始化。

$new_array = array();
foreach( $old_array as $field_id => $field ) {
    if( $field[ 'group_name' ] == '' ) {
       $new_array[ $field_id ] = $field;
    } else {
       $new_array[ $field[ 'group_name' ] ][ $field_id ] = $field;
    }
}

I suppose those who are down voting this are doing it because I am not initializing $new_array[ $field[ 'group_name' ] ]. Well, that is not necessary in PHP, it will be automatically initialized with an empty array when you do $new_array[ $field[ 'group_name' ] ][ $field_id ] = $field;.

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