Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

$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

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 
like image 453
Artful_dodger Avatar asked Nov 10 '15 23:11

Artful_dodger


People also ask

How do you use key value in foreach?

The foreach looping is the best way to access each key/value pair from an array. array_expr is an array. In every loop the value of the current element of the array is assigned to $value and the internal array pointer is advanced by one and the process continue to reach the last array element. array_expr is an array.

What is php blade?

Blade is the simple, yet powerful templating engine that is included with Laravel. Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates.

What is the function of foreach loop?

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.


1 Answers

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

@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 $oReturn in 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:

// 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 $node would 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 url elements. If you did want to loop through the nodes you'd have to do something like:

@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.

Update

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

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

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

like image 63
morphatic Avatar answered Sep 24 '22 19:09

morphatic