本文介绍了如何在foreach中传递一个变量来得到一个数组的所有索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道这是否可能,但在这里...我需要获取包含在对象中的数组的所有索引 / code>



foreach



 <?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'=>字符串'1Z12345E1512345676'(长度= 18)
public'order_status'=>字符串'1'(长度= 1)
public'cust_id'=>字符串'10'(长度= 2)
public'cart'=>字符串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: ; S:5: 图像; S:18: 商务cards.gif; S:4: 船舶;一个:3:{S:6: 接地; d:9.730000000000000426325641456060111522674560546875; S:11: 第二天空气; d:18.53999999999999914734871708787977695465087890625; s:9:隔夜; d:26.269999999999999573674358543939888477325439453125;} s:7:选项; a:2:{s:17:打印说明; s:16'。 ..(length = 784)
public'shipping_type'=>字符串'过夜'(长度= 9)
public'shipping_cost'=>字符串'26 .27'(长度= 5)
public'order_sub_total'=>字符串'104.50'(长度= 6)
public'order_total'=>字符串'130.77'(长度= 6)
public'id'=>字符串'30'(长度= 2)
public'timestamp'=>字符串'2012-10-13 00:52:30'(length = 19)
1 =>
object(stdClass)[23]
public'tracking_num'=>字符串''(长度= 0)
public'order_status'=>字符串'0'(长度= 1)
public'cust_id'=>字符串'10'(长度= 2)
public'cart'=>字符串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:名片 ; S:5: 图像; S:18: 商务cards.gif; S:4: 船舶;一个:3:{S:6: 接地; d:9.730000000000000426325641456060111522674560546875; S:11: 第二天空气; d:18.53999999999999914734871708787977695465087890625; s:9:隔夜; d:26.269999999999999573674358543939888477325439453125;} s:7:选项; a:2:{s:17:打印说明; s:16'。 ..(length = 1582)
public'shipping_type'=>字符串'Ground'(长度= 6)
public'shipping_cost'=>字符串'37.62'(长度= 5)
public'order_sub_total'=>字符串'622.25'(长度= 6)
public'order_total'=>字符串'659.87'(长度= 6)
public'id'=>字符串'31'(长度= 2)
public'timestamp'=> string'2012-10-13 00:53:20'(length = 19)
2 =>
object(stdClass)[24]
public'tracking_num'=>字符串''(长度= 0)
public'order_status'=>字符串'0'(长度= 1)
public'cust_id'=>字符串'10'(长度= 2)
public'cart'=>字符串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:名片 ; S:5: 图像; S:18: 商务cards.gif; S:4: 船舶;一个:3:{S:6: 接地; d:9.730000000000000426325641456060111522674560546875; S:11: 第二天空气; d:18.53999999999999914734871708787977695465087890625; s:9:隔夜; d:26.269999999999999573674358543939888477325439453125;} s:7:选项; a:2:{s:17:打印说明; s:16'。 ..(长度= 3145)
public'shipping_type'=>字符串'过夜'(长度= 9)
public'shipping_cost'=>字符串'224.46'(长度= 6)
public'order_sub_total'=>字符串'1,000.75'(长度= 8)
public'order_total'=>字符串'1225.21'(长度= 7)
public'id'=>字符串'32'(长度= 2)
public'timestamp'=>字符串'2012-10-13 01:11:03'(length = 19)

包含在 foreach中 var $ cart 包含一个 ARRAY 与多个索引。如果我用 0 替换 $ x ,循环将显示 cart 数组。然而,一些购物车包含多于> 1的索引。
$ b

count($ keys)正确地回显了 cart 数组中索引的数目。例如,我有3辆车。


  1. cart 1 has 1 index

  2. cart 2 has 2 index

  3. cart 3 has 4 index

如果我做 echo count($ keys)我得到 1 2 4



如何将每个循环的这些值(按顺序)分别传递给我的 $ x var? / p>

请注意购物车可能具有多于一个的索引值

期望输出: / p>

每个 cart 包含一个包含多个产品的数组。 foreach必须迭代并显示 cart中的每个产品基于上面显示的 cart 数组应该在如下表格中:

 < tr> 0< / tr> / * cart 1在数组中有1个索引* / 
< tr> 01< / tr> / * cart 2在数组中有2个索引* /
< tr> 0123< / tr> / * cart 3在数组中有4个索引* /


解决方案

您可以尝试如下



您有每个订单可以有多个购物车,每个购物车有多个items.What你想要显示每个购物车的项目#。 / p>

 <?php 
cart = unserialize($ val-> cart);
$ keys = array_keys($ cart); //这会给你一个订单
//如果现在你要计算每个购物车的物品数量然后
foreach($键为$ val){
回声计数($购物[$ VAL]);
}

?>


Not sure if this is possible but here goes... i need to get all of the indexes of an array contained within an object

foreach

<?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)

This code is contained inside of a foreach The var $cart contains an object of an ARRAY with multiple indexes. If I replace $x with 0 the loop will display the first index in the cart array. However some cart contain more than > 1 index.

count($keys) is properly echoing out the number of indexes in the cart array. For example I have 3 carts.

  1. cart 1 has 1 index
  2. cart 2 has 2 index
  3. cart 3 has 4 index

If i do echo count($keys) I get back 1 2 4

How do I pass these values (in order) individually to my $x var for each loop?

Notice how cart may have more than 1 index value

Expect Output:

Each cartcontains an array with multiple products. The foreach must iterate through and display each product in the array within cart Based on the var_dump shown above the expected output of the cart array should be in a table as follows:

<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*/
解决方案

You can give try as below

You have each order can have multiple carts and each cart have multiple items.What you want to display # of items into each cart.

<?php 
    $cart = unserialize($val->cart);  
    $keys = array_keys($cart); // this will give you # of carts into one order
    //if now you want to count # of items into each cart then
    foreach($keys as $val){
      echo count($cart[$val]);
    }

?>

这篇关于如何在foreach中传递一个变量来得到一个数组的所有索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 16:10