PHP在foreach循环中未设置对象,$ key是名称而不是迭代编号

时间:2018-11-09 19:27:46

标签: php object foreach simplexml

我需要取消设置付款对象之一,但是使用我正在使用的代码,由于$k不是实际的迭代键,它是对象的名称,因此我无法使其正常工作Payment

如何获取迭代的数字键?

    // var_dump of object
    object(add_nodes)#14 (1) {
      ["Payments"]=>
      object(add_nodes)#15 (1) {
        ["Payment"]=>
        array(2) {
          [0]=>
          object(add_nodes)#18 (5) {
            ["PaymentStatus"]=>
            string(9) "Succeeded"
            ["Payer"]=>
            string(10) "podve"
            ["Payee"]=>
            string(11) "PitneyBowes"
            ["PaymentTime"]=>
            string(24) "2018-11-09T09:18:27.000Z"
            ["PaymentAmount"]=>
            string(5) "47.73"
          }
          [1]=>
          object(add_nodes)#19 (7) {
            ["PaymentStatus"]=>
            string(9) "Succeeded"
            ["Payer"]=>
            string(10) "podve"
            ["Payee"]=>
            string(14) "jannesmith"
            ["PaymentTime"]=>
            string(24) "2018-11-09T09:18:27.000Z"
            ["PaymentAmount"]=>
            string(6) "102.99"
            ["ReferenceID"]=>
            string(17) "09273450972340987534"
            ["FeeOrCreditAmount"]=>
            string(4) "4.21"
          }
        }
      }
    }

代码:

    $xml = simplexml_load_string($response, 'add_nodes');       
    $payments = $obj->Payments;
    if(count($payments->Payment) > 1 ) {

        foreach($payments->Payment as $k => $v) {
            echo $k; // This = Payment, not 0 or 1
            if($v->Payee != 'jannesmith') {
                unset($payments->Payment[$k]);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

作为替代,我一直都喜欢DomDocument和XPath,因为它与在JavaScript,Swift等中进行DOM遍历和操作完全相同。您无需学习a的所有复杂技巧。特定语言的实现,例如SimpleXML。

$response = <<< XML
<?xml version="1.0"?>
<DocRoot>
<Payments>
    <Payment>
        <PaymentStatus>Succeeded</PaymentStatus>
        <Payer>podve</Payer>
        <Payee>Pitney Bowes</Payee>
        <PaymentTime>2018-11-09T09:18:27.000Z</PaymentTime>
        <PaymentAmount>47.73</PaymentAmount>
    </Payment>
    <Payment>
        <PaymentStatus>Succeeded</PaymentStatus>
        <Payer>podve</Payer>
        <Payee>jannesmith</Payee>
        <PaymentTime>2018-11-09T09:18:27.000Z</PaymentTime>
        <PaymentAmount>102.99</PaymentAmount>
        <ReferenceID>09273450972340987534</ReferenceID>
        <FeeOrCreditAmount>4.21</FeeOrCreditAmount>
    </Payment>
</Payments>
</DocRoot>
XML;

$doc = new DomDocument;
$doc->loadXML($response);
$xpath = new DomXPath($doc);
$nodes = $xpath->query("/DocRoot/Payments/Payment[not(./Payee='jannesmith')]");
foreach ($nodes as $node) $node->parentNode->removeChild($node);
echo $doc->saveXML();

输出:

<?xml version="1.0"?>
<DocRoot>
<Payments>

    <Payment>
        <PaymentStatus>Succeeded</PaymentStatus>
        <Payer>podve</Payer>
        <Payee>jannesmith</Payee>
        <PaymentTime>2018-11-09T09:18:27.000Z</PaymentTime>
        <PaymentAmount>102.99</PaymentAmount>
        <ReferenceID>09273450972340987534</ReferenceID>
        <FeeOrCreditAmount>4.21</FeeOrCreditAmount>
    </Payment>
</Payments>
</DocRoot>