创建时的多维数组键命名

时间:2014-11-13 16:14:48

标签: php arrays multidimensional-array

我正在从UPS返回给我的对象上运行foreach循环,该对象获取客户的货件历史记录。我试图使用具有父键的跟踪号嵌套所有信息。我似乎无法让它发挥作用。

public function getUpsHistoryArray($tracking_number)
{

    $upsParams = $this->trackUps($tracking_number, '1');

    if ($upsParams->Shipment->Package->Activity->ActivityLocation)
    {
        foreach ($upsParams->Shipment->Package->Activity as $activity)
            if ($activity->ActivityLocation->Address->City)
                $newParams[] = array(
                    'location' => $activity->ActivityLocation->Address->City.', ' . $activity->ActivityLocation->Address->StateProvinceCode,
                    'date' => date('n/d/Y', strtotime($activity->Date)),
                    'time' => date('g:i a', strtotime($activity->Time)),
                    'activity' => (string)$activity->Status->StatusType->Description
                    );
    }

    return $newParams;
}

array(2) {
[0]=>
  array(10) {
  [0]=>
   array(4) {
   ["location"]=>
   string(10) "GOLDEN, CO"
   ["date"]=>
   string(9) "9/15/2014"
   ["time"]=>
   string(8) "11:27 am"
   ["activity"]=>
   string(9) "DELIVERED"
 }
 [1]=>
  array(4) {
   ["location"]=>
   string(17) "COMMERCE CITY, CO"
   ["date"]=>
   string(9) "9/15/2014"
   ["time"]=>
   string(7) "6:00 am"
   ["activity"]=>
   string(16) "OUT FOR DELIVERY"
}
}
 [1]=>
  array(10) {
  [0]=>
  array(4) {
  ["location"]=>
  string(10) "GOLDEN, CO"
  ["date"]=>
  string(9) "9/15/2014"
  ["time"]=>
  string(8) "11:27 am"
  ["activity"]=>
  string(9) "DELIVERED"
  }
  [1]=>
  array(4) {
  ["location"]=>
  string(17) "COMMERCE CITY, CO"
  ["date"]=>
  string(9) "9/15/2014"
  ["time"]=>
  string(7) "6:00 am"
  ["activity"]=>
  string(16) "OUT FOR DELIVERY"
}

我希望它显示

array(2) {
[1ZXXXXXXXXXXXX]=>
  array(10) {
  [0]=>
   array(4) {
   ["location"]=>
   string(10) "GOLDEN, CO"
   ["date"]=>
   string(9) "9/15/2014"
   ["time"]=>
   string(8) "11:27 am"
   ["activity"]=>
   string(9) "DELIVERED"
 }
 [1]=>
  array(4) {
   ["location"]=>
   string(17) "COMMERCE CITY, CO"
   ["date"]=>
   string(9) "9/15/2014"
   ["time"]=>
   string(7) "6:00 am"
   ["activity"]=>
   string(16) "OUT FOR DELIVERY"
}
}
 [1ZXXXXXXXXXXXX]=>
  array(10) {
  [0]=>
  array(4) {
  ["location"]=>
  string(10) "GOLDEN, CO"
  ["date"]=>
  string(9) "9/15/2014"
  ["time"]=>
  string(8) "11:27 am"
  ["activity"]=>
  string(9) "DELIVERED"
  }
  [1]=>
  array(4) {
  ["location"]=>
  string(17) "COMMERCE CITY, CO"
  ["date"]=>
  string(9) "9/15/2014"
  ["time"]=>
  string(7) "6:00 am"
  ["activity"]=>
  string(16) "OUT FOR DELIVERY"
}

我试过了

public function getUpsHistoryArray($tracking_number)
{

    $upsParams = $this->trackUps($tracking_number, '1');

    if ($upsParams->Shipment->Package->Activity->ActivityLocation)
    {
        foreach ($upsParams->Shipment->Package->Activity as $activity)
            if ($activity->ActivityLocation->Address->City)
                $newParams[$upsParams->Shipment->Package->TrackingNumber] = array(
                    'location' => $activity->ActivityLocation->Address->City.', ' . $activity->ActivityLocation->Address->StateProvinceCode,
                    'date' => date('n/d/Y', strtotime($activity->Date)),
                    'time' => date('g:i a', strtotime($activity->Time)),
                    'activity' => (string)$activity->Status->StatusType->Description
                    );
    }

    return $newParams;
}

2 个答案:

答案 0 :(得分:0)

尝试:

$newParams[$upsParams->Shipment->Package->TrackingNumber][] = array(
                    'location' => $activity->ActivityLocation->Address->City.', '
                     . $activity->ActivityLocation->Address->StateProvinceCode,
                    'date' => date('n/d/Y', strtotime($activity->Date)),
                    'time' => date('g:i a', strtotime($activity->Time)),
                    'activity' => (string)$activity->Status->StatusType->Description
                    );

但是你不会得到你所展示的内容,因为多个密钥(跟踪号码)不能具有相同的价值。

答案 1 :(得分:0)

是$ tracking_number == $ upsParams->发货 - >包裹 - > TrackingNumber?

如果是这样的话......试试

public function getUpsHistoryArray($tracking_number)
{

    $upsParams = $this->trackUps($tracking_number, '1');

    if ($upsParams->Shipment->Package->Activity->ActivityLocation)
    {

        $new_params = array($tracking_number => array());

        foreach ($upsParams->Shipment->Package->Activity as $activity)
            if ($activity->ActivityLocation->Address->City)
                $newParams[$tracking_number][] = array(
                    'location' => $activity->ActivityLocation->Address->City.', ' . $activity->ActivityLocation->Address->StateProvinceCode,
                    'date' => date('n/d/Y', strtotime($activity->Date)),
                    'time' => date('g:i a', strtotime($activity->Time)),
                    'activity' => (string)$activity->Status->StatusType->Description
                    );
    }

    return $newParams;
}

关键区别是newParams [$ tracking_number] [] = ...