如何创建一个循环以将数组更改为输出?

发布于 2025-01-29 13:14:16 字数 644 浏览 0 评论 0原文

像这样的数组:

Array(
    0=>google.com,
    1=>microsoft.com,
    2=>array(
            google.com=>cloud,
            microsoft.com=>office),
    3=>array(
            microsoft.com=>azur),
    4=>array(
            office=>array(
                         0=>word,
                         1=>exel)
       )
    )

我想创建一个循环以将数组更改为输出? (在PHP中)。所以结果就像 这: 输出:

    array(
         google.com=>array(0=>cloud),
         microsoft.com=>array(
                             office=>array(0=>word , 1=exel),
                             azur)
    )

My array like this:

Array(
    0=>google.com,
    1=>microsoft.com,
    2=>array(
            google.com=>cloud,
            microsoft.com=>office),
    3=>array(
            microsoft.com=>azur),
    4=>array(
            office=>array(
                         0=>word,
                         1=>exel)
       )
    )

I want to create a loop to change the array to the output? (In PHP). So the result to be like
this:
The Output:

    array(
         google.com=>array(0=>cloud),
         microsoft.com=>array(
                             office=>array(0=>word , 1=exel),
                             azur)
    )

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

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

发布评论

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

评论(1

奶气 2025-02-05 13:14:16

您的输入以及输出数据是一团糟,其中项可能是数组键或数组值。
无论如何,首先,我递归地将输入清楚地清楚为简单的child_term => parent_term数组。
然后,我将此简单的地图转换为树形式:

<?php
$data = [
    'google.com',
    'microsoft.com',
    [
        'google.com' => 'cloud',
        'microsoft.com' => 'office',
    ],
    [
        'microsoft.com' => 'azure',
    ],
    [
        'office' => [
            'word',
            'excel'
        ]
    ]
];

// ----------------------------------------
// Plainify
// ----------------------------------------
$terms = [];
$plainfy = function( $data, $parent_term = null ) use (&$terms, &$plainfy) {
    foreach( $data as $key => $value ){

        if( !is_numeric( $key ) )
            $parent_term = $key;

        if( !is_array($value) )
            $terms[ $value ] = $parent_term;
        else
            $plainfy( $value, $parent_term );

    }
};

// Walk though input and run recursive function for subarrays
foreach( $data as $item => $value ){
    if( is_array( $value ) )
        $plainfy( $value );
    else
        $terms[ $value ] = null;
}

// $terms now contain something like
// ...
// google.com => null,
// cloud => google.com
// ...

// ----------------------------------------
// Convert
// ----------------------------------------
$out = [];

// This is holding references to term's children array
$terms_indices = [];

foreach( $terms as $child => $parent ){
    // Non-root
    if( $parent ){

        // If this element has children itself
        if( in_array($child, $terms) ){
            $terms_indices[ $parent ][ $child ] = [];
            $terms_indices[ $child ] = &$terms_indices[ $parent ][ $child ];
        } 
        // This is a leaf element
        else{
            $terms_indices[ $parent ][] = $child;
        }

    } 
    // Root element
    else {
        $out[ $child ] = [];
        $terms_indices[ $child ] = &$out[ $child ];
    }
}


print_r($out);

Your input aswell as the output data is a mess where term might be an array key or an array value.
Anyways first of all I recursively plainify the input into a simple child_term => parent_term array.
Then I convert this simple map into a tree form:

<?php
$data = [
    'google.com',
    'microsoft.com',
    [
        'google.com' => 'cloud',
        'microsoft.com' => 'office',
    ],
    [
        'microsoft.com' => 'azure',
    ],
    [
        'office' => [
            'word',
            'excel'
        ]
    ]
];

// ----------------------------------------
// Plainify
// ----------------------------------------
$terms = [];
$plainfy = function( $data, $parent_term = null ) use (&$terms, &$plainfy) {
    foreach( $data as $key => $value ){

        if( !is_numeric( $key ) )
            $parent_term = $key;

        if( !is_array($value) )
            $terms[ $value ] = $parent_term;
        else
            $plainfy( $value, $parent_term );

    }
};

// Walk though input and run recursive function for subarrays
foreach( $data as $item => $value ){
    if( is_array( $value ) )
        $plainfy( $value );
    else
        $terms[ $value ] = null;
}

// $terms now contain something like
// ...
// google.com => null,
// cloud => google.com
// ...

// ----------------------------------------
// Convert
// ----------------------------------------
$out = [];

// This is holding references to term's children array
$terms_indices = [];

foreach( $terms as $child => $parent ){
    // Non-root
    if( $parent ){

        // If this element has children itself
        if( in_array($child, $terms) ){
            $terms_indices[ $parent ][ $child ] = [];
            $terms_indices[ $child ] = &$terms_indices[ $parent ][ $child ];
        } 
        // This is a leaf element
        else{
            $terms_indices[ $parent ][] = $child;
        }

    } 
    // Root element
    else {
        $out[ $child ] = [];
        $terms_indices[ $child ] = &$out[ $child ];
    }
}


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