尝试使用 blade 在 foreach 循环中获取密钥以工作
Trying to get key in a foreach loop to work using blade
如果我使用 {{$node[0]->url}}
,那么 Laravel 的模板引擎会显示正确的结果,但我不知道如何在 @foreach 循环中使用 @for $i=0 来显示所有内容这是我的路线文件中的内容
$oReturn = new stdClass();
$fid='endpoints';//sample fid
$url = 'http://localhost:5493/sdata/$system/registry/'.$fid;
$xml = simplexml_load_file($url);
foreach($xml->xpath("//sdata:payload") as $entry) {
// xpath here must be from payload to endPoint--type
$content = $entry->xpath("./sdata:endPoint--type");
foreach($content as $c) {
// Make set of children with prefix sdata
$nodes = $c->children('sdata', true);
}
// add parsed data to the array
$oReturn->entry[] = $nodes;
}
return View::make('index', compact('oReturn'));
这是我在视图文件中尝试过的
@for($i=0; $i < 4; $i++)
@endfor
@foreach ($oReturn as $node)
<li>{{$node[$i]->url}}</li>
@endforeach
抱歉,这是完整的 print_r 结果
Array ( [oReturn] => stdClass Object
( [entry] => Array
(
[0] => SimpleXMLElement Object ( [description] => Sage 50 Accounts [protocol] => http [host] => base_3 [applicationName] => accounts50 [contractName] => SimpleXMLElement Object ( ) [dataSetName] => - [url] => http://base_3:5493/sdata/accounts50 [isBrowsable] => true [aliveStamp] => 2015-11-06T23:31:10.031+00:00 )
[1] => SimpleXMLElement Object ( [endPointType] => dataSet [applicationName] => accounts50 [contractName] => GCRM [dataSetName] => Enter Your Company Name [url] => http://base_3:5493/sdata/accounts50/GCRM/{C22ACA13-3C4C-4E33-A584-CD99BD3002A6} )
[2] => SimpleXMLElement Object ( [endPointType] => dataSet [applicationName] => accounts50 [contractName] => GCRM [dataSetName] => Enter Your Company Name [url] => http://base_3:5493/sdata/accounts50/GCRM/{FF476636-D4AF-4191-BDE4-891EDA349A68} )
[3] => SimpleXMLElement Object ( [endPointType] => dataSet [applicationName] => accounts50 [contractName] => GCRM [dataSetName] => Enter Your Company Name [url] => http://base_3:5493/sdata/accounts50/GCRM/{C62A13D5-3FFE-43B4-9DAF-38F9055A83C7} )
[4] => SimpleXMLElement Object ( [description] => GCRM Contract [endPointType] => contract [protocol] => http [host] => base_3 [applicationName] => accounts50 [contractName] => GCRM [dataSetName] => - [url] => http://base_3:5493/sdata/accounts50/GCRM [aliveStamp] => 2015-11-06T23:31:11.062+00:00 )
)
)
) 1
简单的答案是 foreach
在 Blade 中的工作原理与常规 PHP foreach
相同。您应该能够执行以下操作:
@foreach ($nodes as $node)
<li>{{ $node->url }}</li>
@endforeach
如果您需要访问每个节点的数组键值:
@foreach ($nodes as $key => $node)
<li>{{ $key }}: {{ $node->url }}</li>
@endforeach
但是,我认为问题可能不在于您的 Blade 语法,而在于您创建输入变量的方式。鉴于您在上面的代码中创建 $oReturn
的方式,它不会具有您似乎期望的属性。为了说明,这是您似乎正在创建的内容的简化版本:
// initialize your return variable
$oReturn = new stdClass();
// create a dummy array <sdata:x> nodes,
// to simulate $nodes = $c->children('sdata', true);
$node = new SimpleXMLElement('<sdata:x/>');
$nodes = [ $node, $node, $node ];
// simulate adding nodes to the array of entries
$oReturn->entry[] = [ $node, $node, $node ];
// print out the resulting structure
print_r( compact( 'oReturn' ) );
会return:
Array(
[oReturn] => stdClass Object
(
[entry] => Array
(
[0] => Array
(
[0] => SimpleXMLElement Object()
[1] => SimpleXMLElement Object()
[2] => SimpleXMLElement Object()
)
)
)
)
因此,当您执行 @foreach ($oReturn as $node)
时,$node
的值将是 entry[]
数组,它具有单个元素,即节点数组。从您的输入中不清楚这些节点甚至具有 url
元素。如果您确实想遍历节点,则必须执行以下操作:
@foreach ($oReturn->entry[0] as $node)
<li>{{ $node->url }}</li>
@endforeach
这有意义吗?我认为您需要重新考虑 $oReturn
.
的创作
更新
鉴于下面的反馈和上面 print_r
语句的输出,以下应该有效:
@foreach ($oReturn->entry as $node)
<li>{{ (string) $node->url }}</li>
@endforeach
(string)
将 $node->url
的结果转换为字符串。否则 PHP 可能会将其视为某种对象。 SimpleXMLElement
可能很奇怪。
如果我使用 {{$node[0]->url}}
,那么 Laravel 的模板引擎会显示正确的结果,但我不知道如何在 @foreach 循环中使用 @for $i=0 来显示所有内容这是我的路线文件中的内容
$oReturn = new stdClass();
$fid='endpoints';//sample fid
$url = 'http://localhost:5493/sdata/$system/registry/'.$fid;
$xml = simplexml_load_file($url);
foreach($xml->xpath("//sdata:payload") as $entry) {
// xpath here must be from payload to endPoint--type
$content = $entry->xpath("./sdata:endPoint--type");
foreach($content as $c) {
// Make set of children with prefix sdata
$nodes = $c->children('sdata', true);
}
// add parsed data to the array
$oReturn->entry[] = $nodes;
}
return View::make('index', compact('oReturn'));
这是我在视图文件中尝试过的
@for($i=0; $i < 4; $i++)
@endfor
@foreach ($oReturn as $node)
<li>{{$node[$i]->url}}</li>
@endforeach
抱歉,这是完整的 print_r 结果
Array ( [oReturn] => stdClass Object
( [entry] => Array
(
[0] => SimpleXMLElement Object ( [description] => Sage 50 Accounts [protocol] => http [host] => base_3 [applicationName] => accounts50 [contractName] => SimpleXMLElement Object ( ) [dataSetName] => - [url] => http://base_3:5493/sdata/accounts50 [isBrowsable] => true [aliveStamp] => 2015-11-06T23:31:10.031+00:00 )
[1] => SimpleXMLElement Object ( [endPointType] => dataSet [applicationName] => accounts50 [contractName] => GCRM [dataSetName] => Enter Your Company Name [url] => http://base_3:5493/sdata/accounts50/GCRM/{C22ACA13-3C4C-4E33-A584-CD99BD3002A6} )
[2] => SimpleXMLElement Object ( [endPointType] => dataSet [applicationName] => accounts50 [contractName] => GCRM [dataSetName] => Enter Your Company Name [url] => http://base_3:5493/sdata/accounts50/GCRM/{FF476636-D4AF-4191-BDE4-891EDA349A68} )
[3] => SimpleXMLElement Object ( [endPointType] => dataSet [applicationName] => accounts50 [contractName] => GCRM [dataSetName] => Enter Your Company Name [url] => http://base_3:5493/sdata/accounts50/GCRM/{C62A13D5-3FFE-43B4-9DAF-38F9055A83C7} )
[4] => SimpleXMLElement Object ( [description] => GCRM Contract [endPointType] => contract [protocol] => http [host] => base_3 [applicationName] => accounts50 [contractName] => GCRM [dataSetName] => - [url] => http://base_3:5493/sdata/accounts50/GCRM [aliveStamp] => 2015-11-06T23:31:11.062+00:00 )
)
)
) 1
简单的答案是 foreach
在 Blade 中的工作原理与常规 PHP foreach
相同。您应该能够执行以下操作:
@foreach ($nodes as $node)
<li>{{ $node->url }}</li>
@endforeach
如果您需要访问每个节点的数组键值:
@foreach ($nodes as $key => $node)
<li>{{ $key }}: {{ $node->url }}</li>
@endforeach
但是,我认为问题可能不在于您的 Blade 语法,而在于您创建输入变量的方式。鉴于您在上面的代码中创建 $oReturn
的方式,它不会具有您似乎期望的属性。为了说明,这是您似乎正在创建的内容的简化版本:
// initialize your return variable
$oReturn = new stdClass();
// create a dummy array <sdata:x> nodes,
// to simulate $nodes = $c->children('sdata', true);
$node = new SimpleXMLElement('<sdata:x/>');
$nodes = [ $node, $node, $node ];
// simulate adding nodes to the array of entries
$oReturn->entry[] = [ $node, $node, $node ];
// print out the resulting structure
print_r( compact( 'oReturn' ) );
会return:
Array(
[oReturn] => stdClass Object
(
[entry] => Array
(
[0] => Array
(
[0] => SimpleXMLElement Object()
[1] => SimpleXMLElement Object()
[2] => SimpleXMLElement Object()
)
)
)
)
因此,当您执行 @foreach ($oReturn as $node)
时,$node
的值将是 entry[]
数组,它具有单个元素,即节点数组。从您的输入中不清楚这些节点甚至具有 url
元素。如果您确实想遍历节点,则必须执行以下操作:
@foreach ($oReturn->entry[0] as $node)
<li>{{ $node->url }}</li>
@endforeach
这有意义吗?我认为您需要重新考虑 $oReturn
.
更新
鉴于下面的反馈和上面 print_r
语句的输出,以下应该有效:
@foreach ($oReturn->entry as $node)
<li>{{ (string) $node->url }}</li>
@endforeach
(string)
将 $node->url
的结果转换为字符串。否则 PHP 可能会将其视为某种对象。 SimpleXMLElement
可能很奇怪。