PHP数组(仅一个结果)

时间:2018-09-09 21:57:12

标签: php arrays

在我的循环中,我只有一个结果。

数组结果:

   array(3) {
    [3]=>
    array(1) {
         ["qty"]=> int(2)
       }
    [1]=>
    array(1) {
        ["qty"]=> int(3)
       }
    [2]=>
      array(1) {
        ["qty"]=> int(4)
      }
    }

如果我在循环中编写此代码,它将仅显示一个结果:

var_dump('Key=' . $key .' - Value=' . $value['qty']);

我的循环:

foreach ($this->products as $key => $value) {

        $QProducts = $this->db->prepare('select p.products_id,
                                                pd.products_name,
                                                p.products_model
                                          from :table_products p,
                                               :table_products_description pd
                                          where p.products_id = :products_id
                                          and p.products_id = pd.products_id
                                        ');

        $QProducts->bindInt(':products_id', $key);
        $QProducts->execute();

        echo $QProducts->value('products_name');
}

我该如何解决这个问题?

该函数的主要元素下方。它可以帮助您。

public function OrderProduts() {
          $prod_array = $this->orderResult['Cart']['contents'];

          foreach ($prod_array as $key => $value) {
            $QProducts = $this->db->prepare('select p.products_id,
                                                    pd.products_name
                                              from :table_products p,
                                                   :table_products_description pd
                                              where p.products_id = :products_id
                                              and p.products_id = pd.products_id
                                            ');

            $QProducts->bindInt(':products_id', $key);
            $QProducts->execute();

            echo $Products->value('products_name);
            echo $value ['qty'];
          }
       }

1 个答案:

答案 0 :(得分:0)

products似乎可以通过引用传递。 尝试将products分配给变量,

$prodArray = $this->products

并遍历变量($prodArray)而不是$this->products

调试

将每次迭代中的product分配给这样的临时数组($productsArrayTemp)。

$productsArrayTemp = array(); //a temporary array.   
public function OrderProduts() {
    $prod_array = $this->orderResult['Cart']['contents'];

    foreach ($prod_array as $key => $value) {
        $QProducts = $this->db->prepare('select p.products_id,
                                            pd.products_name
                                      from :table_products p,
                                           :table_products_description pd
                                      where p.products_id = :products_id
                                      and p.products_id = pd.products_id
                                    ');

        $QProducts->bindInt(':products_id', $key);
        $QProducts->execute();

        //I am not too sure about the name of the object. 
        //But you need to push each and every object to an array.
        $this->productsArrayTemp[] = $Products;

  }
}

然后尝试计算该数组,

count($this->productsArrayTemp)

如果此结果带来了预期的结果数量,则可以使用此数组(而不是$this->products)访问其子对象。

这样做的原因是为了消除$this->products被覆盖在代码中某个地方的事实。

我希望这会有所帮助。