Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 串联时foreach中的变量擦除_Php_Variables_Foreach - Fatal编程技术网

Php 串联时foreach中的变量擦除

Php 串联时foreach中的变量擦除,php,variables,foreach,Php,Variables,Foreach,我在互联网上查找过这个问题,但问题只发生在试图将数组传递给foreach并在内部修改它的人身上。我的问题要容易得多,也容易混淆。 我有两个全局变量: $type=""; $rule=""; 我有这个密码: foreach($cartasxml->children() as $child) { $str="insert into cards (title,cost,color,loyalty,type,pow,tgh,hand,life,rules,set,rarity,numbe

我在互联网上查找过这个问题,但问题只发生在试图将数组传递给foreach并在内部修改它的人身上。我的问题要容易得多,也容易混淆。 我有两个全局变量:

$type="";
$rule="";
我有这个密码:

foreach($cartasxml->children() as $child) {
    $str="insert into cards (title,cost,color,loyalty,type,pow,tgh,hand,life,rules,set,rarity,number,artist,flavor,cost) values('{{{".$child->name."}}}',";
if(isset($child->typelist))
    {
        foreach($child->typelist as $a)
            $type .= "|" . str_replace("{","[",str_replace("}","]",$a->type));
        $str.="'{{{".substr($type,1)."}}}',";
    }
    else
        $str.="NULL,";
在每节课结束时,我会:

$type="";
$rule="";
因此,当我通过打印$str看到$type的值时,它只显示我包含的最后一个值。也许是一个escope问题,但我仍然不知道如何解决它


某人?

可能是您可以使用数组将所有值推入其中

$type_array = array();
$str_array = array();
foreach($cartasxml->children() as $child) {
    $str="insert into cards (title,cost,color,loyalty,type,pow,tgh,hand,life,rules,set,rarity,number,artist,flavor,cost) values('{{{".$child->name."}}}',";
if(isset($child->typelist))
    {
        foreach($child->typelist as $a)
            $type .= "|" . str_replace("{","[",str_replace("}","]",$a->type));
        $str.="'{{{".substr($type,1)."}}}',";

array_push($type_array,$type);
array_push($str_array,$str);

    }
    else
        $str.="NULL,";


print_r($type_array);
print_r($str_array);
试试这个:

$type = implode('|', 
                array_map(function($a)
                           { return str_replace("{","[",str_replace("}","]",$a->type)) },
                          $child->typelist));
$str .= "'{{{".$type."}}}',";

你能给你的代码plz添加一些返回值吗?$type在foreach中包含什么类型的字符串?每次通过循环初始化
$str
。当你完成后,为什么你希望看到比上一个更多的东西?@Barmar如果你仔细看,你会发现我只清理了外部foreach上的绳子。这是因为我想连接当前->子->的->类型列表,而不是全部->类型列表->children@ArmelL. 连接的字符串应该返回:生物|萨满和仅返回萨满。您的数据结构中一定有问题。。。您是否可以尝试在foreach块内打印$a->键入以检查是否有2次通过?你已经做了吗?伙计,已经做了。使用和数组,然后,在连接之前,我内爆了变量。同样的问题。也许我的机器里有一些PHP配置?