O
O
Oleg2018-12-14 17:32:31
PHP
Oleg, 2018-12-14 17:32:31

How to succinctly specify a variable as an iterator in SimpleXML (without xPath and DOM)?

XML file:

<?xml version="1.0" encoding="utf-8"?>
<top>
...
<block>
<id>0</id>
<n>Procyon1</n>
<f>f</f>
<t>d</t>
<r>r</r>
<l>112</l>
<q>u</q>
<e>?</e>
<b>?</b>
<p>?</p>
<s>?</s>
<c>?</c>
<sp>1</sp>
<bm>4</bm>
</block>
...
</top>

There are 100 tags filled with content from and other literal tags.
A complete set is sent from the site form via a GET request to change the contents of one of these containers.
...script.php?id=2&n=Procyon1&f=f&t=d&r=r&l=314&q=u&e=%3F&b=%3F&p=%3F&s=%3F&c=%3F&sp=1&bm=4...

And I need to replace the contents of the containers with the incoming data from the form.
So far, I did this (through the content during the cycle I find a link to the desired one:
$sdb = simplexml_load_file('db.xml');
$i=$_GET['id'];
      foreach($sdb->block as $block){
        
        if ($i==$block->id){
          
          foreach ($_GET as $key=>$value)
          {	
            $block->{$key}=$value;

          };
        }
      };
      $sdb->asXml('db.xml');

Question: how to build a search structure for a block by an iterator, which is expressed by a variable that stores a number,
so that instead of two loops with getting a reference to $block in $block->{$key}=$value
, you can use something like $sdb-> block[ $i ]->{$key}=$value similar to $sdb->block[2]->{$key}=$value ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2019-01-04
@lolzqq

Save a link to a temporary variable $tmp = &$sdb->block[0]; $tmp->{$key}=$value

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question