douzhang2092 2014-01-13 17:43
浏览 13
已采纳

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

I have Json data coming in following format:

    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
)

and I am trying to filter it and create (multi-dimensional) array that should look like following:

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

        )

)

but from my code, I am ending up getting following result:

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
                )

        )

)

Here is my code, I cannot figure out how to fix this.

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;


}

Thanks

  • 写回答

1条回答 默认 最新

  • dongshou9878 2014-01-13 17:50
    关注

    Rather than indexing each element in $options[$source->name] to the next available index ([]), you want to index it to the id of the $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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号