Intereting Posts

Как передать переменную в foreach .., чтобы получить все индексы массива

Не уверен, что это возможно, но здесь идет … Мне нужно получить все indexes массива, содержащегося в object

для каждого

 <?php foreach($all_orders as $key => $val) : ?> <?php $cart = unserialize($val->cart); $keys = array_keys($cart); $x = count($keys); $index = $keys[$x]; $cart = $cart[$index]; ?> <?php $val->field_name; ?> <?php endforeach; ?> 

var_dump

 array (size=3) 0 => object(stdClass)[22] public 'tracking_num' => string '1Z12345E1512345676' (length=18) public 'order_status' => string '1' (length=1) public 'cust_id' => string '10' (length=2) public 'cart' => string 'a:1:{s:32:"d9082c0e32bb353796d3bf8ebba34f71";a:9:{s:5:"rowid";s:32:"d9082c0e32bb353796d3bf8ebba34f71";s:2:"id";s:9:"101_44068";s:3:"qty";s:1:"1";s:5:"price";s:5:"104.5";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=784) public 'shipping_type' => string 'Overnight' (length=9) public 'shipping_cost' => string '26.27' (length=5) public 'order_sub_total' => string '104.50' (length=6) public 'order_total' => string '130.77' (length=6) public 'id' => string '30' (length=2) public 'timestamp' => string '2012-10-13 00:52:30' (length=19) 1 => object(stdClass)[23] public 'tracking_num' => string '' (length=0) public 'order_status' => string '0' (length=1) public 'cust_id' => string '10' (length=2) public 'cart' => string 'a:2:{s:32:"d7f14d223f7873bf93a3c0670b01609b";a:9:{s:5:"rowid";s:32:"d7f14d223f7873bf93a3c0670b01609b";s:2:"id";s:9:"101_96866";s:3:"qty";s:1:"1";s:5:"price";s:5:"84.25";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=1582) public 'shipping_type' => string 'Ground' (length=6) public 'shipping_cost' => string '37.62' (length=5) public 'order_sub_total' => string '622.25' (length=6) public 'order_total' => string '659.87' (length=6) public 'id' => string '31' (length=2) public 'timestamp' => string '2012-10-13 00:53:20' (length=19) 2 => object(stdClass)[24] public 'tracking_num' => string '' (length=0) public 'order_status' => string '0' (length=1) public 'cust_id' => string '10' (length=2) public 'cart' => string 'a:4:{s:32:"7670d6182aaad174954c5619079f48a1";a:9:{s:5:"rowid";s:32:"7670d6182aaad174954c5619079f48a1";s:2:"id";s:9:"101_32840";s:3:"qty";s:1:"1";s:5:"price";s:5:"84.25";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=3145) public 'shipping_type' => string 'Overnight' (length=9) public 'shipping_cost' => string '224.46' (length=6) public 'order_sub_total' => string '1,000.75' (length=8) public 'order_total' => string '1225.21' (length=7) public 'id' => string '32' (length=2) public 'timestamp' => string '2012-10-13 01:11:03' (length=19) - array (size=3) 0 => object(stdClass)[22] public 'tracking_num' => string '1Z12345E1512345676' (length=18) public 'order_status' => string '1' (length=1) public 'cust_id' => string '10' (length=2) public 'cart' => string 'a:1:{s:32:"d9082c0e32bb353796d3bf8ebba34f71";a:9:{s:5:"rowid";s:32:"d9082c0e32bb353796d3bf8ebba34f71";s:2:"id";s:9:"101_44068";s:3:"qty";s:1:"1";s:5:"price";s:5:"104.5";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=784) public 'shipping_type' => string 'Overnight' (length=9) public 'shipping_cost' => string '26.27' (length=5) public 'order_sub_total' => string '104.50' (length=6) public 'order_total' => string '130.77' (length=6) public 'id' => string '30' (length=2) public 'timestamp' => string '2012-10-13 00:52:30' (length=19) 1 => object(stdClass)[23] public 'tracking_num' => string '' (length=0) public 'order_status' => string '0' (length=1) public 'cust_id' => string '10' (length=2) public 'cart' => string 'a:2:{s:32:"d7f14d223f7873bf93a3c0670b01609b";a:9:{s:5:"rowid";s:32:"d7f14d223f7873bf93a3c0670b01609b";s:2:"id";s:9:"101_96866";s:3:"qty";s:1:"1";s:5:"price";s:5:"84.25";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=1582) public 'shipping_type' => string 'Ground' (length=6) public 'shipping_cost' => string '37.62' (length=5) public 'order_sub_total' => string '622.25' (length=6) public 'order_total' => string '659.87' (length=6) public 'id' => string '31' (length=2) public 'timestamp' => string '2012-10-13 00:53:20' (length=19) 2 => object(stdClass)[24] public 'tracking_num' => string '' (length=0) public 'order_status' => string '0' (length=1) public 'cust_id' => string '10' (length=2) public 'cart' => string 'a:4:{s:32:"7670d6182aaad174954c5619079f48a1";a:9:{s:5:"rowid";s:32:"7670d6182aaad174954c5619079f48a1";s:2:"id";s:9:"101_32840";s:3:"qty";s:1:"1";s:5:"price";s:5:"84.25";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=3145) public 'shipping_type' => string 'Overnight' (length=9) public 'shipping_cost' => string '224.46' (length=6) public 'order_sub_total' => string '1,000.75' (length=8) public 'order_total' => string '1225.21' (length=7) public 'id' => string '32' (length=2) public 'timestamp' => string '2012-10-13 01:11:03' (length=19) - array (size=3) 0 => object(stdClass)[22] public 'tracking_num' => string '1Z12345E1512345676' (length=18) public 'order_status' => string '1' (length=1) public 'cust_id' => string '10' (length=2) public 'cart' => string 'a:1:{s:32:"d9082c0e32bb353796d3bf8ebba34f71";a:9:{s:5:"rowid";s:32:"d9082c0e32bb353796d3bf8ebba34f71";s:2:"id";s:9:"101_44068";s:3:"qty";s:1:"1";s:5:"price";s:5:"104.5";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=784) public 'shipping_type' => string 'Overnight' (length=9) public 'shipping_cost' => string '26.27' (length=5) public 'order_sub_total' => string '104.50' (length=6) public 'order_total' => string '130.77' (length=6) public 'id' => string '30' (length=2) public 'timestamp' => string '2012-10-13 00:52:30' (length=19) 1 => object(stdClass)[23] public 'tracking_num' => string '' (length=0) public 'order_status' => string '0' (length=1) public 'cust_id' => string '10' (length=2) public 'cart' => string 'a:2:{s:32:"d7f14d223f7873bf93a3c0670b01609b";a:9:{s:5:"rowid";s:32:"d7f14d223f7873bf93a3c0670b01609b";s:2:"id";s:9:"101_96866";s:3:"qty";s:1:"1";s:5:"price";s:5:"84.25";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=1582) public 'shipping_type' => string 'Ground' (length=6) public 'shipping_cost' => string '37.62' (length=5) public 'order_sub_total' => string '622.25' (length=6) public 'order_total' => string '659.87' (length=6) public 'id' => string '31' (length=2) public 'timestamp' => string '2012-10-13 00:53:20' (length=19) 2 => object(stdClass)[24] public 'tracking_num' => string '' (length=0) public 'order_status' => string '0' (length=1) public 'cust_id' => string '10' (length=2) public 'cart' => string 'a:4:{s:32:"7670d6182aaad174954c5619079f48a1";a:9:{s:5:"rowid";s:32:"7670d6182aaad174954c5619079f48a1";s:2:"id";s:9:"101_32840";s:3:"qty";s:1:"1";s:5:"price";s:5:"84.25";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=3145) public 'shipping_type' => string 'Overnight' (length=9) public 'shipping_cost' => string '224.46' (length=6) public 'order_sub_total' => string '1,000.75' (length=8) public 'order_total' => string '1225.21' (length=7) public 'id' => string '32' (length=2) public 'timestamp' => string '2012-10-13 01:11:03' (length=19) - array (size=3) 0 => object(stdClass)[22] public 'tracking_num' => string '1Z12345E1512345676' (length=18) public 'order_status' => string '1' (length=1) public 'cust_id' => string '10' (length=2) public 'cart' => string 'a:1:{s:32:"d9082c0e32bb353796d3bf8ebba34f71";a:9:{s:5:"rowid";s:32:"d9082c0e32bb353796d3bf8ebba34f71";s:2:"id";s:9:"101_44068";s:3:"qty";s:1:"1";s:5:"price";s:5:"104.5";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=784) public 'shipping_type' => string 'Overnight' (length=9) public 'shipping_cost' => string '26.27' (length=5) public 'order_sub_total' => string '104.50' (length=6) public 'order_total' => string '130.77' (length=6) public 'id' => string '30' (length=2) public 'timestamp' => string '2012-10-13 00:52:30' (length=19) 1 => object(stdClass)[23] public 'tracking_num' => string '' (length=0) public 'order_status' => string '0' (length=1) public 'cust_id' => string '10' (length=2) public 'cart' => string 'a:2:{s:32:"d7f14d223f7873bf93a3c0670b01609b";a:9:{s:5:"rowid";s:32:"d7f14d223f7873bf93a3c0670b01609b";s:2:"id";s:9:"101_96866";s:3:"qty";s:1:"1";s:5:"price";s:5:"84.25";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=1582) public 'shipping_type' => string 'Ground' (length=6) public 'shipping_cost' => string '37.62' (length=5) public 'order_sub_total' => string '622.25' (length=6) public 'order_total' => string '659.87' (length=6) public 'id' => string '31' (length=2) public 'timestamp' => string '2012-10-13 00:53:20' (length=19) 2 => object(stdClass)[24] public 'tracking_num' => string '' (length=0) public 'order_status' => string '0' (length=1) public 'cust_id' => string '10' (length=2) public 'cart' => string 'a:4:{s:32:"7670d6182aaad174954c5619079f48a1";a:9:{s:5:"rowid";s:32:"7670d6182aaad174954c5619079f48a1";s:2:"id";s:9:"101_32840";s:3:"qty";s:1:"1";s:5:"price";s:5:"84.25";s:4:"name";s:13:"Business Card";s:5:"image";s:18:"business-cards.gif";s:4:"ship";a:3:{s:6:"Ground";d:9.730000000000000426325641456060111522674560546875;s:11:"2nd Day Air";d:18.53999999999999914734871708787977695465087890625;s:9:"Overnight";d:26.269999999999999573674358543939888477325439453125;}s:7:"options";a:2:{s:17:"Print Description";s:16'... (length=3145) public 'shipping_type' => string 'Overnight' (length=9) public 'shipping_cost' => string '224.46' (length=6) public 'order_sub_total' => string '1,000.75' (length=8) public 'order_total' => string '1225.21' (length=7) public 'id' => string '32' (length=2) public 'timestamp' => string '2012-10-13 01:11:03' (length=19) 

Этот код содержится внутри foreach . $cart var $cart содержит объект ARRAY с несколькими indexes . Если я заменил $x на 0 цикл отобразит первый индекс в массиве корзин. Однако в некоторых cart содержится более 1 индекса.

count($keys) правильно отражает количество индексов в массиве корзин. Например, у меня 3 тележки.

  1. cart 1 имеет 1 index
  2. cart 2 имеет 2 index
  3. cart 3 имеет 4 index

Если я делаю echo count($keys) я возвращаюсь 1 2 4

Как передать эти значения (по порядку) отдельно для моего $x var для каждого цикла?

Обратите внимание, что корзина может иметь более одного значения индекса

Ожидаемый результат:

Каждая cart содержит массив с несколькими продуктами. Предпросмотр должен проходить через и отображать каждый продукт в массиве в cart На основе приведенного выше параметра var_dump ожидаемый вывод массива cart должен быть в таблице следующим образом:

 <tr>0</tr> /*cart 1 has 1 index in the array*/ <tr>01</tr> /*cart 2 has 2 index in the array*/ <tr>0123</tr> /*cart 3 has 4 index in the array*/