Pass an array to a view in cakephp

0

I have an array in this way doing a var_drum.

array (size=1)
  'articles' => 
    array (size=36)
      0 => 
        object(stdClass)[134]
          public 'id' => int 56
          public 'title' => string 'prueba con servicio json es tt aa' (length=33)
          public 'body' => string 'prueba de hoy' (length=13)
          public 'created' => string '2018-06-22T19:49:34+00:00' (length=25)
          public 'modified' => string '2018-06-22T19:49:34+00:00' (length=25)

and I want to pass this array in a foreach in a cakephp view

the controller has it this way

public function index()
{

$http = new Client();
$response = $http->get('http://localhost/paginaws/articles/index.json');
$json = $response->json;
$json = $response->body;
$jarray = json_decode($json);
//$jarray = json_decode( json_encode( $json ), true );
$valor = (array)$jarray;


$this->set(compact(['valor']));


} 
    
asked by Jhon Bernal 27.06.2018 в 18:15
source

1 answer

1

You can use it like any PHP arrangement, keep in mind that the values of the array are objects so you'll have to access its properties by ->

In your index.ctp

<?php foreach ($valor['articles'] as $article): ?>
    <h1><?php echo $article->title ?></h1>
<?php endforeach; ?>
    
answered by 27.06.2018 / 21:19
source