php循环无法按预期工作 [英] php loop not working as expected

查看:44
本文介绍了php循环无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图一次从'$ old'>'$ new'交换所有属性ID,然后保存xml:

I'm trying to swap all attribute id's at once from '$old' > '$new' then save the xml:

$reorder = array( 9=>"8", 8=>"5", 7=>"4", 6=>"3", 5=>"0", 4=>"1", 3=>"9", 2=>"7", 1=>"2", 0=>"6" );

    $objDOM = new SimpleXMLElement(some.xml, null, true);
    foreach ($reorder as $old => $new) {
       $picture = $objDOM->xpath('picture[@id="'.$old.'"]');
       $picture[0]["id"] = $new;
    }
    echo $objDOM->asXML();

下面的结果(与数组$ reorder不匹配)

The result below (doesn't match Array $reorder)

  • 3> 9
  • 9> 6
  • 8> 8
  • 7> 2
  • 6> 3
  • 5> 5
  • 4> 4
  • 0> 0
  • 1> 1
  • 7> 2

似乎是按顺序切换ID,因此如果刚切换的ID稍后出现在数组中,则会再次切换.

It seems to be switching the id's in sequence, so id's that have just been switched are then switched again if they come up later in the array.

我在那里做错了什么?如何获得一次性切换所有ID的信息?

What I'm doing wrong there? How can I get it to switch ALL the id's in one go?

谢谢...安迪

推荐答案

答案是两个循环

首先,您在xpath中搜索旧ID,并将其存储在数组中然后再次循环以新的ID替换存储的结果

fist you search the xpath for the old id, store it in an array and then loop again to replace the stored results with the new id

$reorder = array(9 => "8", 8 => "5", 7 => "4", 6 => "3", 5 => "0", 4 => "1", 3 => "9", 2 => "7", 1 => "2", 0 => "6");

$objDOM = new SimpleXMLElement(
      '<pictures>
    <picture id="9">id was 9, should be 8 now</picture>
    <picture id="8">id was 8, should be 5 now</picture>
    <picture id="7">id was 7, should be 4 now</picture>
    <picture id="6">id was 6, should be 3 now</picture>
    <picture id="5">id was 5, should be 0 now</picture>
    <picture id="4">id was 4, should be 1 now</picture>
    <picture id="3">id was 3, should be 9 now</picture>
    <picture id="2">id was 2, should be 7 now</picture>
    <picture id="1">id was 1, should be 2 now</picture>
    <picture id="0">id was 0, should be 6 now</picture>
</pictures>');
$oldPicIds = array();

foreach ($reorder as $old => $new) {
   $oldPicIds[$old] = $objDOM->xpath('picture[@id="' . $old . '"]');
}

foreach ($reorder as $old => $new) {
   $oldPicIds[$old][0]['id'] = $new;
}

echo $objDOM->asXML();

输出:

<?xml version="1.0"?>
<pictures>
    <picture id="8">id was 9, should be 8 now</picture>
    <picture id="5">id was 8, should be 5 now</picture>
    <picture id="4">id was 7, should be 4 now</picture>
    <picture id="3">id was 6, should be 3 now</picture>
    <picture id="0">id was 5, should be 0 now</picture>
    <picture id="1">id was 4, should be 1 now</picture>
    <picture id="9">id was 3, should be 9 now</picture>
    <picture id="7">id was 2, should be 7 now</picture>
    <picture id="2">id was 1, should be 2 now</picture>
    <picture id="6">id was 0, should be 6 now</picture>
</pictures>

要保存数组,可以使用 array_pop 来获取上一次出现的picture @ id = xy.哪一个应该是通缉犯(阅读弊端评论)

to save an array you could use array_pop to get the last occurrence of picture@id=xy. which should be the wanted one (read comments for drawbacks)

$reorder = array(9 => "8", 8 => "5", 7 => "4", 6 => "3", 5 => "0", 4 => "1", 3 => "9", 2 => "7", 1 => "2", 0 => "6");

$objDOM = new SimpleXMLElement(
      '<pictures>...</pictures>');

foreach ($reorder as $old => $new) {
   $picture = $objDOM->xpath('picture[@id="' . $old . '"]');
   $picture = array_pop($picture);
   $picture['id'] = $new;
}

echo $objDOM->asXML();

这篇关于php循环无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆