douchen1988 2016-04-20 10:55
浏览 9
已采纳

向阵列添加其他数据

I build up an array like so from a SimpleXML Object like so

$currentXML = new \SimpleXMLElement($getCurrentXML);
$leadArray = array();

foreach($currentXML->Job->Employee as $object) {
    $dataArray[] = array(
        'ID' => (string)$object->ID,
        'Date Identified' => $dateIdentified,
        'Name' => (string)$object->Name,
        'Owner' => (string)$object->Owner->Name,
        'Value' => (string)$object->EstimatedValue
    );
}

If I output the data for the above, I get something like this which is perfect

array:8 [▼
  0 => array:7 [▼
    "ID" => "1230279"
    "Date Identified" => "19/04/2016"
    "Name" => "Some Name"
    "Owner" => "Some Owner"
    "Value" => "Some Value"
  ]
  1 => array:7 [▶]
  2 => array:7 [▶]
  3 => array:7 [▶]
  4 => array:7 [▶]
  5 => array:7 [▶]
  6 => array:7 [▶]
  7 => array:7 [▶]
]

Now within the above foreach loop, I need to use $object->ID to query another API. SO I have

foreach($currentXML->Job->Employee as $object) {
    $dataArray[] = array(
        'ID' => (string)$object->ID,
        'Date Identified' => $dateIdentified,
        'Name' => (string)$object->Name,
        'Owner' => (string)$object->Owner->Name,
        'Value' => (string)$object->EstimatedValue
    );

    $customField = Helper::getCustomFields((string)$object->ID);
    $currentFieldsXML = new \SimpleXMLElement($customField);
}

Now if I output currentFieldsXML, sometimes I am returned and empty SimpleXMLElement Object, sometimes it contains data. What I am trying to do is push the data into the array alongside its other data. So I have this

foreach($currentXML->Job->Employee as $object) {
    $dataArray[] = array(
        'ID' => (string)$object->ID,
        'Date Identified' => $dateIdentified,
        'Name' => (string)$object->Name,
        'Owner' => (string)$object->Owner->Name,
        'Value' => (string)$object->EstimatedValue
    );

    $customField = Helper::getCustomFields((string)$object->ID);
    $currentFieldsXML = new \SimpleXMLElement($customField);

    if(!empty($currentFieldsXML->CustomFields)) {
        foreach($currentFieldsXML->CustomFields as $custom) {
            array_push($dataArray, (string)$custom->CustomField->ID);
            array_push($dataArray, (string)$custom->CustomField->Name);
            array_push($dataArray, (string)$custom->CustomField->Text);
        }
    }
}

The problem with this is that the output is something like this

array:23 [▼
  0 => array:7 [▶]
  1 => array:7 [▶]
  2 => "122156"
  3 => "Some Data"
  4 => "Some more Data"
  5 => array:7 [▶]
  6 => "122156"
  7 => "Date"
  8 => "20 April"
]

So element 0 has no custom fields associated with it. Element 1 does have data, but it is displaying as element 2, 3 and 4. In essence, the above should look something like this

array:8 [▼
  0 => array:7 [▶]
  1 => array:7 [▼
    "ID" => "1230279"
    "Date Identified" => "19/04/2016"
    "Name" => "Some Name"
    "Owner" => "Some Owner"
    "Value" => "Some Value"
    array:3 [
      1 => "122156"
      2 => "Some Data"
      3 => "Some more Data"
    ]
  ]
  ...
]

So how can I add the data I am returned into its appropiate array as shown above?

Thanks

UPDATE Sorry, my mistake

  • 写回答

1条回答 默认 最新

  • dqx36753 2016-04-20 11:12
    关注
    $iterator=0;
    foreach($currentXML->Job->Employee as $object) {
        $dataArray[$iterator] = array(
            'ID' => (string)$object->ID,
            'Date Identified' => $dateIdentified,
            'Name' => (string)$object->Name,
            'Owner' => (string)$object->Owner->Name,
            'Value' => (string)$object->EstimatedValue
         );
    
         $customField = Helper::getCustomFields((string)$object->ID);
         $currentFieldsXML = new \SimpleXMLElement($customField);
    
         if(!empty($currentFieldsXML->CustomFields)) {
             $seconditerator=0;
             foreach($currentFieldsXML->CustomFields as $custom) {
                $dataArray[$iterator][$custom->CustomField->ID][$seconditerator]=(string)$custom->CustomField->ID;
                $dataArray[$iterator][$custom->CustomField->ID][$seconditerator]=(string)$custom->CustomField->Name;
                $dataArray[$iterator][$custom->CustomField->ID][$seconditerator]=(string)$custom->CustomField->Text;
                $seconditerator++;
             }
         }
         $iterator++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?