Cakephp 3 how to show View of an array inside an array?

Hello World,

got a question figured long time, and need help!!!.

In the Orders Array inside contain 2 objects and each object got a order_products array

example

orders [ { id : ‘1’, customer_name: ‘superman’, [ order_products { product_name: ‘phone’ } ] },
{ id:‘2’, customer_name: ‘captain america’, [ order_products { product_name:’'apple } ] } ]

in my view page

i foreach all orders as order

i can display customer name but not able to display product details product name,

  <?php foreach ($allorders as $order): ?>

  <td><?php echo $order->$order['order_products']->product_name ?></td>

  <td><?php echo $order->order['order_products']->product_name ?></td>

  <td><?= $order->customer_name ?></td> 

at Controller the codes

 $query = $this->Orders->find()->contain(['OrderProducts'])
 $this->paginate = ['limit' => 20];
 $allorders = $this->paginate($query);

 $this->set(compact('allorders'));

the above for display product name is not successful with Error. trying to get property product_name of non object

thank you for Hello World guidance
thank you thank you and thank you

It’s not possible to be sure from your explanation. But it is possible that product_details has been returned as an array of entities. If so, you would need something like this:

echo $order->$order['product_details'][0]->product_name;

But $orders->$orders looks very fishy to me. I think you have other problems too.

Where did this data format come from? I cannot make sense of what it’s trying to tell me. And I don’t see how you get from contain(['OrderProducts']) to something that has “product_details” in it instead of “order_products”.

hi, Zuluru,

thank you for the pinpoint, ya, i have made typo error when making the example resulting the confusion, i have edited the question and it should be clearer

thank you and await for your advise and guidance (thank you very much)

hi, Sir

i tried echo $order->$order[‘order_products’][0]->product_name; it’s not working,

but it works when experimenting with

$order->order_products[0]->product_name;

$order->$order is not something that is ever going to work. If you have $x = 'foo', then $order->$x is equivalent to referencing the property with the name that’s stored in $x, i.e. $order->foo. But it really only works if $x is a string. We know that $order is an object, so trying to use it as a property reference is nonsensical.

Hope that makes some sense; it’s a complicated topic.

ya, its complicated when there’s arrays inside array, that’s how life sometimes works.

thank you once again, will like to learn/ask additional cakephp3 stuff, still learning stage

thanks

try
$order->order_products[0]->product_name

$allorders is an array of Order entity objects. Each foreach iteration you’re putting one of them into $order
$order is therefore an Order entity object, whose contents can be accessed either by $entity->field or $entity[“field”] (the second is really syntactic sugar for the first, it’s not really an array)

If Order hasMany OrderProduct (which I deduce from your json-like data), the $order field order_products will be an array of OrderProduct entity objects, whose fields can be accessed as above.

Nested arrays are quite common in CakePHP as the ORM makes it easy to denormalize entities. So you just have to keep clear what’s happening.
A very good tool is the debugger toolbar - click the icon on the bottom right, one of the tabs lets you explore all of the variables in the view. This is useful when you don’t have a debugger.

hello rogerkaplan

ya, it works with $order->order_products[0]->product_name, the homework assignment which i am working on has many hasMany with id and id linking to other Controllers,

now learning how to call from another Controller and link to this controller.

thank you very much for the explanation, the debugger toolbar is nice, the bottom right corner one,