PHP - 从API obj响应中嵌套数组

时间:2018-05-14 15:25:24

标签: php arrays object nested

我有一个大对象API响应,我正在尝试将数据加载到嵌套数组中,以便稍后我可以使用它。以下是我从API中获取的对象示例。

SObject Object
(
    [type] => AggregateResult
    [fields] => stdClass Object
        (
            [expr0] => 12
            [Name] => Performance Reviews
            [Status] => Closed - Approved
            [expr1] => 30
        )

)
SObject Object
(
    [type] => AggregateResult
    [fields] => stdClass Object
        (
            [expr0] => 12
            [Name] => Performance Reviews
            [Status] => Closed - Attempted
            [expr1] => 11
        )

)
SObject Object
(
    [type] => AggregateResult
    [fields] => stdClass Object
        (
            [expr0] => 12
            [Name] => Performance Reviews
            [Status] => Closed - Contact Declined
            [expr1] => 13
        )

正如我所说,目标是建立一个类似于此的嵌套数组:

Array
    (
        [January] => Array
        (
            [0] => Array
            (
                [0] => January
                [1] => Closed - Approved
                [2] => 28
            )
            [1] => Array
            (
                [0] => January
                [1] => Closed - Approved
                [2] => 28
            )
        )
    )

这是我的代码:

$query = 
     "SELECT CALENDAR_MONTH(closedDate), recordType.name,status,count(id) 
      FROM case 
      WHERE owner.name ='" . $SFName . "' AND recordType.name IN('DT Case','Performance Reviews') AND closedDate = LAST_N_MONTHS:6 
      GROUP BY CALENDAR_MONTH(closedDate),recordType.Name,status ORDER BY CALENDAR_MONTH(closedDate)";

    $counter     = 0;
    $mprArray    = array(); //instantiate our Array
    $response    = $mySforceConnection->query($query); 
    $queryResult = new QueryResult($response); 

    foreach ($queryResult->records as $case) { 
        //turn our query Result into an Obj
        $sObject    = new SObject($case);
        $recordType = $sObject->Name; 
        $status     = $sObject->Status;
        $month      = $sObject->expr0;
        $count      = $sObject->expr1;

        //this is a filter to weed out a portion of the cases.
        if ($recordType == "Performance Reviews") {
            foreach($sObject as $record) { 
                //change the month's number to a month's name
                $dateObj   = DateTime::createFromFormat('!m', $month);
                $monthName = $dateObj->format('F');
                // Create the nested array, it should end up looking like $mprArray[January]. 
                // This is a dynamic name since we're creating an array for each status that exists in performance Reviews
                $mprArray[$monthName] = array($monthName,$status,$count); 
                // Trying to append our nested array onto the $mprArray so we can work with it later.
                array_push($mprArray,$mprArray[$monthName]);
            }

        }
        //increase counter
        $counter = $counter++; 
    }

} catch (Exception $e) {
    print_r($mySforceConnection->getLastRequest());
    echo $e->faultstring;
}

而不是嵌套数组,只有数组中的第一项被命名为'January',其余的基于计数器命名,直到我们到达下个月的'二月',这就是它的样子。 / p>

Array
(
    [January] => Array
        (
            [0] => January
            [1] => Closed - Contact Declined
            [2] => 15
        )

    [0] => Array
        (
            [0] => January
            [1] => Closed - Approved
            [2] => 28
        )
)

1 个答案:

答案 0 :(得分:0)

因为array_push只将数据插入下一个索引; 我觉得你应该只使用$mprArray[$monthName][] = array($monthName,$status,$count);;并且在那之后不需要array_push