PHP将数组推入现有数组

时间:2014-11-18 21:29:45

标签: php arrays json

我有一个用于创建JSON输出的php文件。你可以在下面的代码中看到我的循环:

foreach($objQueueData->event as $event){ 

            $output[] = array(
                "eventID" => (int)$event->trainingEventID,
                "eventTitle" => (string)$event->teTitle,
                "eventDescription" => (string)$event->teDesc,
                "eventSource" => (string)$event->teSource,
                "eventType" => (string)$event->teType,
                "eventMedium" => (string)$event->teMedium,
                "eventRequestor" => (string)$event->creatorFirst . ' ' . $event->creatorLast,
                "eventRequestorNTID" => (string)$event->creatorNTID,
                "eventRequestDate" => (string)$event->teCreated,
                "eventDirector" => (string)$event->teDirector,
                "eventTeammateImpact" => (string)$event->teTeammateImpact,
                "eventCustomerImpact" => (string)$event->teCustomerImpact,
                "eventComplexity" => (string)$event->teComplexity,
                "eventInitiativeID" => (int)$event->intID,
                "eventNeededBy" => (string)$event->teNeededBy
            );
            $impactedRegions = array();
            if(isset($event->regionalImpact->option)){
                foreach ($event->regionalImpact->option as $region) {
                    $impactedRegions[] = $region->impact;
                }
                array_push($output, $impactedRegions);
            }
        }
        // Set the content type to JSON  for jquery to understand
        header('Content-Type: text/json');

        // Print the response to the page
        print json_encode($output);         

我的问题在于第二个数组impactedRegions。这应该是output的子数组,但它没有按预期工作。

我试图让它成为output数组的一部分。

以下是当前JSON输出的内容:

    [

   {

      "eventID": 25,

      "eventTitle": "Monday",

      "eventDescription": "Testing Monday",

      "eventSource": "OE",

      "eventType": "New Hire",

      "eventMedium": "ILT",

      "eventRequestor": "Carl",

      "eventRequestorNTID": "ch123",

      "eventRequestDate": "Nov 17 2014  4:58PM",

      "eventDirector": "",

      "eventTeammateImpact": "Medium",

      "eventCustomerImpact": "High",

      "eventComplexity": "Low",

      "eventInitiativeID": 1069,

      "eventNeededBy": "2014-11-18"

   },

   [

      {

         "0": "Americas"

      }

   ],

有人能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:0)

foreach ($event->regionalImpact->option as $region) {
     $output['regions'][] = $region->impact;
}

答案 1 :(得分:0)

我认为您遇到的问题是您正在使用带有关联数组的array_push()。我相信array_push用于将值推送到indexed数组。

尝试使用$output['impactedRegions'] = $impactedRegions;来指定密钥。

        $impactedRegions = array();
        if(isset($event->regionalImpact->option)){
            foreach ($event->regionalImpact->option as $region) {
                $impactedRegions[] = $region->impact;
            }
            $output['impactedRegions'] = json_encode($impactedRegions);
        }

此外,json_encode($impactedRegions)将对数组进行编码,使其作为原始数组中的json对象包含在内。

答案 2 :(得分:0)

试试这个

foreach($objQueueData->event as $key => $event){ 

        $output[$key] = array(
            "eventID" => (int)$event->trainingEventID,
            "eventTitle" => (string)$event->teTitle,
            "eventDescription" => (string)$event->teDesc,
            "eventSource" => (string)$event->teSource,
            "eventType" => (string)$event->teType,
            "eventMedium" => (string)$event->teMedium,
            "eventRequestor" => (string)$event->creatorFirst . ' ' . $event->creatorLast,
            "eventRequestorNTID" => (string)$event->creatorNTID,
            "eventRequestDate" => (string)$event->teCreated,
            "eventDirector" => (string)$event->teDirector,
            "eventTeammateImpact" => (string)$event->teTeammateImpact,
            "eventCustomerImpact" => (string)$event->teCustomerImpact,
            "eventComplexity" => (string)$event->teComplexity,
            "eventInitiativeID" => (int)$event->intID,
            "eventNeededBy" => (string)$event->teNeededBy
        );
        $impactedRegions = array();

        if(isset($event->regionalImpact->option)){

            foreach ($event->regionalImpact->option as $region) {
                $impactedRegions[] = $region->impact;
            }                
        }

        $output[$key]['impactedRegions'] = $impactedRegions;
    }
    // Set the content type to JSON  for jquery to understand
    header('Content-Type: text/json');

    // Print the response to the page
    print json_encode($output);