Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 将数组中的每个字保存到变量中_Php_Arrays - Fatal编程技术网

Php 将数组中的每个字保存到变量中

Php 将数组中的每个字保存到变量中,php,arrays,Php,Arrays,我有一个名为$child的数组,其中至少包含单词。我想循环遍历数组,并将每个单词保存到一个单独的变量中。目前我尝试: for($i = 0; $i < $child->count();$i++) { $var1 = (string) $child[$i]->xpath; $var2 = (string) $child[$i+1]->xpath; $var3 = (string) $child[$i+2]->xpath;

我有一个名为$child的数组,其中至少包含单词。我想循环遍历数组,并将每个单词保存到一个单独的变量中。目前我尝试:

for($i = 0; $i < $child->count();$i++)
        {
     $var1 = (string) $child[$i]->xpath;
     $var2 = (string) $child[$i+1]->xpath;
     $var3 = (string) $child[$i+2]->xpath;

}
for($i=0;$i<$child->count();$i++)
{
$var1=(字符串)$child[$i]->xpath;
$var2=(字符串)$child[$i+1]->xpath;
$var3=(字符串)$child[$i+2]>xpath;
}
这会给出一个错误,表示我正在尝试获取非对象的属性。如果数组中有更多单词,那么它也不是很通用

如有任何建议,将不胜感激


对不起,我弄错了,这不是一个单词数组。基本上,我有一个名为$operation的SimpleXMLElement对象。SimpleXMLElement对象([@attributes]=>Array([type]=>and)[child]=>Array([0]=>SimpleXMLElement对象([xpath]=>noNotification[assert]=>exists)[1]=>SimpleXMLElement对象([xpath]=>dataType/enumRef[@name=>OperState]=>assert]=>exists]))


我希望从中提取非通知、序列和数据类型/enumRef[@name='OperState'],并将它们保存在单独的变量中。但是,我不能直接访问它们,因为它必须动态执行,以防以后添加其他元素。

首先,代码中有一个小字符
存在多个错误

首先,应该使用

考虑到这一点:

foreach($child as $key => $value) {
    ${'word' . $key} = $value;  
}
现在您可以使用从$word0开始的变量

编辑:根据您的评论:

$myArray = array();
foreach($child as $key => $value) {
    $myArray[$key] = array(
        'xpath' => (string)$value->xpath, 
        'assert' => (string)$value-> assert,
    );
}
因为您可能有很多变量,所以最好也将数据保存到数组中


请在foreach之后查看它的内容。

您能给我们展示一下
$child
结构吗?您正在将其用作对象,但它是一个数组。我建议看一下关于和的PHP文档。另外,这里显示的代码不会给出任何错误,它甚至不会运行。(
$i$child->count();
?)对不起,我弄错了,这不是一个单词数组。基本上,我有一个名为$operation的SimpleXMLElement对象。SimpleXMLElement对象([@attributes]=>Array([type]=>and)[child]=>Array([0]=>SimpleXMLElement对象([xpath]=>noNotification[assert]=>exists)[1]=>SimpleXMLElement对象([xpath]=>dataType/enumRef[@name=>OperState]=>assert]=>exists]))(如果您用这些信息编辑您的问题会更好。;)我希望从中提取非通知、序列和数据类型/enumRef[@name='OperState'],并将它们保存在单独的变量中。然而,我不能直接访问它们,因为它必须动态执行,以防在稍后阶段添加其他元素。该代码如何解决试图获取非对象属性的
错误?@caCtus您至少应该在向下投票之前阅读我的评论?我加了一句话,以便更清楚地说明他为什么会这样做。哪句话?我可能错过了。@caCtus没关系。这句话就在那里
你会遇到这段代码的问题,因为你没有检查$child[$i+1]是否真的设置了
,但后来我添加了额外的句子+代码来说明问题。是的,也有这种可能性。如果不知道
$child
OP的结构,我们真的帮不上忙,只是告诉你它不是数组,对不起。刚刚删除了我的否决票(当事情变得更清楚时总是如此)。:)对于
,使用
到底有什么问题?(另外,我不认为你的意思是“解析”)。
$child = array("test1","test2","test3","test4","test5","test6");
for($i = 0; $i < count($child); $i++){
   echo $child[$i]."<br />";
   if (isset($child[$i+1])) echo $child[$i+1]."<br />";
   if (isset($child[$i+2])) echo $child[$i+2]."<br />";
}
$words = array();
for($i = 0; $i < count($operation->child); $i++) {
    if (isset($operation->child[$i]->xpath)) {
        $words[$i] = (string) $operation->child[$i]->xpath;
    }
}
foreach($child as $key => $value) {
    ${'word' . $key} = $value;  
}
$myArray = array();
foreach($child as $key => $value) {
    $myArray[$key] = array(
        'xpath' => (string)$value->xpath, 
        'assert' => (string)$value-> assert,
    );
}