簡體   English   中英

需要幫助來修復使用對象代碼創建的多維數組

[英]Need help to fix my create multi-dimensional array using Object code

我有以下格式的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]中的每個元素索引到下一個可用索引( [] ),而是將其索引為$source->segments[$v->id] )的[$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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM