SimpleXML 函数中罕见的数组变量索引

发布于 2024-09-24 05:20:50 字数 507 浏览 1 评论 0原文

我在表单选择上有一个标签,我用 $ _POST 得到这个值,如下所示:

$gallery = array($_POST['gallery']);

并且该值将把它放在这里:

$image = $sitemap->gallery[$gallery]->addChild('image');

问题是给我错误如下:

Fatal error: Call to a member function addChild() on a non-object in

我不明白的是,如果我直接输入一个值我这样做:

$gallery = 0;
$Image = $sitemap->gallery[$gallery]->addChild('image');

我做得很好,发生的事情是我希望用户选择, 有点奇怪,因为它可能会修复。

I have a label on a form select and I get that value with $ _POST like this:

$gallery = array($_POST['gallery']);

and that value will put it here:

$image = $sitemap->gallery[$gallery]->addChild('image');

the problem is giving me error is as follows:

Fatal error: Call to a member function addChild() on a non-object in

I do not understand is that if I put a value directly asin me do it like so:

$gallery = 0;
$Image = $sitemap->gallery[$gallery]->addChild('image');

I do well, what happens is that I want the user to choose,
Kind of strange as it may fix.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感悟人生的甜 2024-10-01 05:20:50

了解数组:

$gallery = array($_POST['gallery']);
echo "Gallery Array: <pre>".print_r($gallery,true)."</pre><br />";

输出:

Array
(
    [0] => 'value in array'
)

如何从数组中获取值:

echo "Get Array Value: ".$gallery[0]."<br />"; // You should be displaying the array index 0

添加自定义索引

$gallery = array('gallery' => $_POST['gallery']);
echo "Gallery Array: <pre>".print_r($gallery,true)."</pre><br />";

输出:
Array

(
    [gallery] => 'value in array'
)

从客户索引数组中获取值

echo "Get Array Value: ".$gallery['gallery']."<br />"; // You should be displaying the array index gallery

Understanding Arrays:

$gallery = array($_POST['gallery']);
echo "Gallery Array: <pre>".print_r($gallery,true)."</pre><br />";

Output:

Array
(
    [0] => 'value in array'
)

How to get the value out of an array:

echo "Get Array Value: ".$gallery[0]."<br />"; // You should be displaying the array index 0

Adding a custom index

$gallery = array('gallery' => $_POST['gallery']);
echo "Gallery Array: <pre>".print_r($gallery,true)."</pre><br />";

Output:
Array

(
    [gallery] => 'value in array'
)

Getting the value from the customer index array

echo "Get Array Value: ".$gallery['gallery']."<br />"; // You should be displaying the array index gallery
淡水深流 2024-10-01 05:20:50

使用这个:

$gallery = $_POST['gallery'];

而不是这个:

$gallery = array($_POST['gallery']);

您传递了一个没有正确索引的数组

或者您可以尝试这种方式:

$gallery = array('gallery' => $_POST ['gallery']);

或者

$image = $sitemap->gallery[$gallery[0]]->addChild('image');

任何一种方式都可以解决问题

use this:

$gallery = $_POST['gallery'];

instead of this:

$gallery = array($_POST['gallery']);

You passing an array that you did not index properly

Or you can try it this way:

$gallery = array('gallery' => $_POST ['gallery']);

or

$image = $sitemap->gallery[$gallery[0]]->addChild('image');

either way should fix the problem

一笔一画续写前缘 2024-10-01 05:20:50

非常感谢,但我对此进行了测试,并给了我同样的错误

  $gallery=array('gallery'=>$_POST['galeria']);
$image = $sitemap->gallery[$gallery]->addChild('image');

,我不明白我的选择形式如下:

<select id="textfield" name="galeria">
        <option id="textfield" value="">Escoger de la Lista</option>
        <?php
    $source = 'content.xml';
    // load as string
    $xmlstr = file_get_contents($source);
    $sitemap = new SimpleXMLElement($xmlstr);
    // load as file
    $sitemap = new SimpleXMLElement($source,null,true);
    //$bar_count = $sitemap->gallery->count();
    //for($i=0;$i<$bar_count;$i++){
    $contador="0";
    foreach($sitemap->gallery as $content) {
    $atributo = $content->attributes();
    echo "<option id='textfield' value='".$contador."'>".$atributo['Name']. "</option>";
    //}
    $contador++; 
    } 
    ?>
      </select>

无论如何,我已经尝试了回显,我得到了结果,不太明白

thank you very much, but I tested this and is giving me the same error

  $gallery=array('gallery'=>$_POST['galeria']);
$image = $sitemap->gallery[$gallery]->addChild('image');

I do not understand the select form that I have is as follows:

<select id="textfield" name="galeria">
        <option id="textfield" value="">Escoger de la Lista</option>
        <?php
    $source = 'content.xml';
    // load as string
    $xmlstr = file_get_contents($source);
    $sitemap = new SimpleXMLElement($xmlstr);
    // load as file
    $sitemap = new SimpleXMLElement($source,null,true);
    //$bar_count = $sitemap->gallery->count();
    //for($i=0;$i<$bar_count;$i++){
    $contador="0";
    foreach($sitemap->gallery as $content) {
    $atributo = $content->attributes();
    echo "<option id='textfield' value='".$contador."'>".$atributo['Name']. "</option>";
    //}
    $contador++; 
    } 
    ?>
      </select>

anyway I've tried has done an echo and I get the result, do not really understand

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文