循环数组以获取数据


Looping arrays to obtain data

我调用 API 以返回作业列表。 我得到的输出如下所示

SimpleXMLElement {#357 ▼
  +"Jobs": SimpleXMLElement {#368 ▼
    +"Job": array:13 [▼
      0 => SimpleXMLElement {#371 ▼
        +"ID": "J000006"
        +"Name": "HTML Website"
        +"Client": SimpleXMLElement {#387 ▶}
        +"Budget": "5000.00"
        +"State": "In Progress"
        +"StartDate": "2016-03-31T00:00:00"
        +"DueDate": "2016-03-31T00:00:00"
      }
      1 => SimpleXMLElement {#372 ▶}
      2 => SimpleXMLElement {#373 ▶}
      3 => SimpleXMLElement {#374 ▶}
      4 => SimpleXMLElement {#375 ▶}
      5 => SimpleXMLElement {#376 ▶}
    ]
  }
}

我现在正在尝试获取与工作相关的报价。 所以我做了一个 API 调用来获取一个报价列表,它会产生这样的东西

SimpleXMLElement {#358 ▼
  +"Quotes": SimpleXMLElement {#366 ▼
    +"Quote": array:12 [▼
      0 => SimpleXMLElement {#369 ▼
        +"ID": "Q0019"
        +"Type": "Quote"
        +"State": "Accepted"
        +"Name": "HTML Website"
        +"Budget": "5000.00"
        +"LeadID": "1232718"
        +"Date": "2016-04-21T00:00:00"
        +"ValidDate": "2016-05-19T00:00:00"
        +"Amount": "1950.00"
        +"AmountTax": "390.00"
        +"AmountIncludingTax": "2340.00"
        +"Client": SimpleXMLElement {#384 ▶}
      }
      1 => SimpleXMLElement {#370 ▶}
      2 => SimpleXMLElement {#371 ▶}
      3 => SimpleXMLElement {#372 ▶}
      4 => SimpleXMLElement {#373 ▶}
      5 => SimpleXMLElement {#374 ▶}
    ]
  }
}

所以我现在有两个XMLElements,我现在正在尝试创建一个具有以下内容的数组

Job -> ID
Job -> Name
Job -> Client
Quote -> Amount
Quote -> AmountTax
Quote -> AmountIncludingTax

所以我创建一个空数组

$finalArray = array();
$iterator = 0;

使用上面的 XML,可以将报价与作业匹配的内容是 Name 属性。 所以我开始循环工作和报价来填充我的数组包含我需要的数据

foreach ($currentJobsXML->Jobs->Job as $job) {
    $seconditerator = 0;
    foreach($jobsQuoteXML->Quotes->Quote as $quote) {
        if((string)$quote->State == 'Accepted') {
            if ((string)$job->Name == (string)$quote->Name) {
                $finalArray[$iterator]['TEST'][$seconditerator] = array(
                    'Job ID' => (string)$job->ID,
                    'Project Name' => (string)$job->Name,
                    'Client' => (string)$job->Client->Name,
                    'Quote Exc VAT' => (string)$quote->Amount,
                    'VAT Amount' => (string)$quote->AmountTax,
                    'Total Amount' => (string)$quote->AmountIncludingTax
                );
                $seconditerator++;
            }
        }
    }
}

使用上面的代码,我似乎只在我的数组中得到一个输出

array:1 [▼
  0 => array:1 [▼
    "TEST" => array:1 [▼
      0 => array:6 [▼
        "Job ID" => "J000006"
        "Project Name" => "HTML Website"
        "Client" => "Prospect 1"
        "Quote Exc VAT" => "1950.00"
        "VAT Amount" => "390.00"
        "Total Amount" => "2340.00"
      ]
    ]
  ]
]
有很多

报价已被接受,它们与乔布斯名称同名,所以我应该得到所有这些数据太。

使用上面的代码,为什么我的数据被覆盖?

谢谢

我想你忘了增加你的$iterator

foreach ($currentJobsXML->Jobs->Job as $job) {
    $seconditerator = 0;
    foreach($jobsQuoteXML->Quotes->Quote as $quote) {
        if((string)$quote->State == 'Accepted') {
            if ((string)$job->Name == (string)$quote->Name) {
                $finalArray[$iterator]['TEST'][$seconditerator] = array(
                    'Job ID' => (string)$job->ID,
                    'Project Name' => (string)$job->Name,
                    'Client' => (string)$job->Client->Name,
                    'Quote Exc VAT' => (string)$quote->Amount,
                    'VAT Amount' => (string)$quote->AmountTax,
                    'Total Amount' => (string)$quote->AmountIncludingTax
                );
                $seconditerator++;
            }
        }
    }
    $iterator++;
}

我实际上也不认为你真的需要那个$seconditerator,如果你只使用[]它会自动像这样递增该数组

foreach ($currentJobsXML->Jobs->Job as $job) {
    //$seconditerator = 0;
    foreach($jobsQuoteXML->Quotes->Quote as $quote) {
        if((string)$quote->State == 'Accepted') {
            if ((string)$job->Name == (string)$quote->Name) {
                //$finalArray[$iterator]['TEST'][$seconditerator] = array(
                $finalArray[$iterator]['TEST'][] = array(
                    'Job ID' => (string)$job->ID,
                    'Project Name' => (string)$job->Name,
                    'Client' => (string)$job->Client->Name,
                    'Quote Exc VAT' => (string)$quote->Amount,
                    'VAT Amount' => (string)$quote->AmountTax,
                    'Total Amount' => (string)$quote->AmountIncludingTax
                );
                //$seconditerator++;
            }
        }
    }
    $iterator++;
}