php - 需要帮助来修复我使用对象代码创建的多维数组

标签 php arrays loops

我有以下格式的 Json 数据:

    stdClass Object
(
    [sources] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 147
                    [name] => CatsWhoCode.com
                    [segments] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 104
                                    [name] => Default
                                    [style] => EDUCATIONAL
                                    [targetAudience] => KNOWLEDGEABLE
                                    [isPrimary] => 1
                                )

                        )

                )

            [1] => stdClass Object
                (
                    [id] => 181
                    [name] => Inspire Trends - Your Daily Dose of Design and Development Resources
                    [segments] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 146
                                    [name] => Inspire Trends
                                    [style] => TECHNICAL
                                    [targetAudience] => KNOWLEDGEABLE
                                    [isPrimary] => 1
                                )

                            [1] => stdClass Object
                                (
                                    [id] => 147
                                    [name] => Inspire Trends
                                    [style] => EDUCATIONAL
                                    [targetAudience] => KNOWLEDGEABLE
                                    [isPrimary] => 0
                                )

                        )

                )

            [2] => stdClass Object
                (
                    [id] => 182
                    [name] => Designmodo
                    [segments] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 148
                                    [name] => Design Modo
                                    [style] => INFORMAL
                                    [targetAudience] => KNOWLEDGEABLE
                                    [isPrimary] => 1
                                )

                            [1] => stdClass Object
                                (
                                    [id] => 149
                                    [name] => Design Modo
                                    [style] => INFORMATIVE
                                    [targetAudience] => GENERAL
                                    [isPrimary] => 0
                                )

                        )

                )

        )

    [status] => 10
)

我正在尝试过滤它并创建应如下所示的(多维)数组:

Array
(
    [CatsWhoCode.com] => Array
        (
                    [104] => [PRI] Default
        )

    [Inspire Trends - Your Daily Dose of Design and Development Resources] => Array
        (
                    [146] => [PRI] Inspire Trends
                    [147] =>  Inspire Trends
        )

    [Designmodo] => Array
        (
                    [148] => [PRI] Design Modo
                    [149] =>  Design Modo

        )

)

但是从我的代码中,我最终得到以下结果:

Array
(
    [CatsWhoCode.com] => Array
        (
            [0] => Array
                (
                    [104] => [PRI] Default
                )

        )

    [Inspire Trends - Your Daily Dose of Design and Development Resources] => Array
        (
            [0] => Array
                (
                    [146] => [PRI] Inspire Trends
                )

            [1] => Array
                (
                    [147] =>  Inspire Trends
                )

        )

    [Designmodo] => Array
        (
            [0] => Array
                (
                    [148] => [PRI] Design Modo
                )

            [1] => Array
                (
                    [149] =>  Design Modo
                )

        )

)

这是我的代码,我不知道如何解决这个问题。

function ar_audienceList($AR){
        $audienceList = $AR->getAudienceList();

            foreach ($audienceList->sources AS $key => $source){
            foreach($source->segments AS $v){
                                            if ($v->isPrimary == 1)$pri = "[PRI]";                
                    $options[$source->name][] = array($v->id => $pri." ".$v->name,);
                    $pri = ''; // reset primary
                }
            }

    return $options;


}

谢谢

最佳答案

您不想将 $options[$source->name] 中的每个元素索引到下一个可用索引 ([]),而是希望将其索引到 id $source->segments ([$v->id])。

function ar_audienceList($AR)
{
    $audienceList = $AR->getAudienceList();
    foreach ($audienceList->sources as $key => $source)
    {
        foreach($source->segments AS $v)
        {
            if ($v->isPrimary == 1)
            {
                $pri = "[PRI]";
            }        
            $options[$source->name][$v->id] = $pri." ".$v->name; // Change is here
            $pri = ''; // reset primary
        }
    }
    return $options;
}

关于php - 需要帮助来修复我使用对象代码创建的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21097885/

相关文章:

c++ - 循环真的比递归快吗?

php - 显示正确的数据

php - Laravel 6.8 PUT 方法不起作用,显示空白页

php - 如何在 Laravel 5 (5.2) 中的另一个存储库类中注入(inject)一个存储库

php - Laravel 已安装,但连接已重置/未收到数据

c# - 使用循环填充二维数组

arrays - 在 MIPS 中遍历二维数组

python - 对特征值和特征向量进行排序

arrays - 从类数组列表中检查 "isKind(of: )"

list - 从 IO Int 迭代列表创建,如何?