php 尝试在 foreach 循环中获取密钥以使用刀片工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33641912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:34:18  来源:igfitidea点击:

Trying to get key in a foreach loop to work using blade

phplaravelbladelaravel-blade

提问by Artful_dodger

if I use {{$node[0]->url}}then Laravel's templating engine dislays the correct result but I can not figure out how to display all using the @for $i=0 within a @foreach loop this is what I have in my routes file

如果我使用,{{$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'));

and this is what I have tried in my view file

这就是我在视图文件中尝试过的

@for($i=0; $i < 4; $i++)
@endfor
@foreach ($oReturn as $node)
   <li>{{$node[$i]->url}}</li>
@endforeach

sorry here is the complete print_r result

抱歉,这是完整的 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

回答by morphatic

The simple answer is that foreachin Blade works the same as a regular PHP foreach. You should be able to do something like:

简单的答案是foreach在 Blade 中的工作方式与常规 PHP 相同foreach。您应该能够执行以下操作:

@foreach ($nodes as $node)
    <li>{{ $node->url }}</li>
@endforeach

If you need access to the array key value for each node:

如果您需要访问每个节点的数组键值:

@foreach ($nodes as $key => $node)
    <li>{{ $key }}: {{ $node->url }}</li>
@endforeach

However, I think the problem may not be with your Blade syntax, but with the way you created your input variables. Given the way you created $oReturnin the code above, it won't have the properties you appear to be expecting. To illustrate, here's a simplified version of what you appear to be creating:

但是,我认为问题可能不在于您的 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' ) );

would return:

会返回:

Array(
    [oReturn] => stdClass Object
        (
            [entry] => Array
                (
                    [0] => Array
                        (
                            [0] => SimpleXMLElement Object()
                            [1] => SimpleXMLElement Object()
                            [2] => SimpleXMLElement Object()
                        )
                )
        )
)

So when you do @foreach ($oReturn as $node)the value of $nodewould be the entry[]array, which has a single element, that is an array of nodes. It is not clear from your input that these nodes even have urlelements. If you did want to loop through the nodes you'd have to do something like:

因此,当您执行@foreach ($oReturn as $node)的值$node将是entry[]具有单个元素的数组,即节点数组。从您的输入中不清楚这些节点甚至具有url元素。如果您确实想遍历节点,则必须执行以下操作:

@foreach ($oReturn->entry[0] as $node)
    <li>{{ $node->url }}</li>
@endforeach

Does this make sense? I think you need to rethink your creation of $oReturn.

这有意义吗?我认为您需要重新考虑您对$oReturn.

Update

更新

Given feedback below and the output of your print_rstatement above, the following should work:

鉴于下面的反馈和print_r上面语句的输出,以下应该有效:

@foreach ($oReturn->entry as $node)
    <li>{{ (string) $node->url }}</li>
@endforeach

The (string)casts the result of $node->urlto string. Otherwise PHP may treat it as some kind of object. SimpleXMLElementcan be weird.

(string)结果转换$node->url为字符串。否则 PHP 可能会将其视为某种对象。SimpleXMLElement可能很奇怪。