【问题标题】:foreach results as feed_urlforeach 结果为 feed_url
【发布时间】:2012-12-08 20:20:00
【问题描述】:

我正在尝试将我的 foreach 循环的结果放入一个 url 以执行 simplexml_load_file。

所以它是这样的: (...) //SimpleXML_load_file 获取 $feed1

    $x=1;
    $count=$feed1->Count; //get a count for total number of loop from XML
    foreach ($feed1->IdList->Id as $item1){

    echo $item1;
    if($count > $x) {
    echo ',';} //Because I need coma after every Id, except the last one.
    $x++;
    }

两个echo只是为了看结果。它给了我类似的东西:

22927669,22039496,21326191,18396266,18295747,17360921,15705350,15681025,15254092,12939407,11943825,11495650,10964843

我想把它放在一个 url 中,像这样制作一个 simplexml_load_file

$feed_url = 'http://www.whatevergoeshere'. $RESULT_OF_FOREACH . 'someothertext';

所以它看起来像:

$feed_url = 'http://www.whatevergoeshere22927669,22039496,21326191,18396266,18295747,17360921,15705350,15681025,15254092,12939407,11943825,11495650,10964843someothertext';     

我尝试将其存储到数组或函数中,然后将其调用到 feed_url,但它并没有像我尝试的那样工作。

我希望它很清楚,如果没有,我会快速回答问题。

谢谢。

【问题讨论】:

    标签: php arrays function foreach simplexml


    【解决方案1】:

    很难弄清楚你想要什么,所以我猜你想将列表作为逗号分隔的字符串存储在变量中。最简单的方法是implode ids 数组

    $ids = array();
    foreach ($feed1->IdList->Id as $item1){
        $ids[] = (string) $item1;
    }
    $RESULT_OF_FOREACH = implode(',', $ids);
    
    $feed_url = 'http://www.whatevergoeshere'. $RESULT_OF_FOREACH . 'someothertext';
    

    【讨论】:

    • 感谢您快速回答 Crip。我想将它作为逗号分隔的字符串存储在变量中。但是,按照您的建议内爆阵列并不能正常工作。我也无法回显它或将其放入 feed_url...
    • var_dump($RESULT_OF_FOREACH) 揭示了什么?你能把结果粘贴一下吗?
    • 好吧,试试上面的,基本上先建id数组。
    • 像魅力一样工作。非常感谢,我已经为此苦苦挣扎了很长时间。
    猜你喜欢
    • 2017-05-03
    • 1970-01-01
    • 2018-07-13
    • 2018-02-01
    • 2018-04-25
    • 2020-02-21
    • 2021-06-21
    • 2014-02-12
    • 1970-01-01
    相关资源
    最近更新 更多