【问题标题】:php array foreach loopphp数组foreach循环
【发布时间】:2011-06-09 23:53:00
【问题描述】:

我的购物篮是array,其中的每件商品也是array

在某些时候,我会遍历每个项目以查找 ID 匹配项。当我有匹配时,我需要知道主 basket 数组中的 Items 位置,以便我可以执行更新和删除。

听起来很简单,但我坚持下去。

到目前为止我有这个

//Lets say there are 5 items in this basket array (each item is also an array)
foreach ($_SESSION['basket'] as $basketArray){

        //this loops through the items attributes (size, colour etc)
        //when the ID is a match, i need to find out what position I am at in the main     array
        foreach($basketArray at $key = > $value){

             if ($value == $itemID){

                   //now I just need to know how to return 0, 1, 2, 3,  or 4 so that i can do 'unset' later.
             }
        }
}

感谢您的帮助。

奥兹

【问题讨论】:

  • 你有没有想过使用 itemID 作为数组的键?

标签: php loops foreach return


【解决方案1】:

说这是你的$_SESSION['basket']

Array
(
    [0] => Array
        (
            [id] => 12
            [name] => some name
            [color] => some color
        )

    [1] => Array
        (
            [id] => 8
            [name] => some name
            [color] => some color
        )

    [2] => Array
        (
            [id] => 3
            [name] => some name
            [color] => some color
        )

    [3] => Array
        (
            [id] => 22
            [name] => some name
            [color] => some color
        )

)

首先你需要遍历数组$_SESSION['basket']的所有单个元素:

foreach ($_SESSION['basket'] as $i => $product) {
  /*
  $i will equal 0, 1, 2, etc.
  and is the position of the product within the basket array.

  $product is an array of itself, which will equal e.g.:

  Array
  (
      [id] => 12
      [name] => some name
      [color] => some color
  )
  */
}

现在您想知道产品的 id 是否与您要查找的产品的 ID 匹配。您不需要遍历$product 数组的每个元素来执行此操作,假设您的 ID 将始终命名为“id”。只需检查 id 字段即可:

foreach ($_SESSION['basket'] as $i => $product) {
  if ($product['id'] == $someId) {
    // at this point you want to remove this whole product from the basket
    // you know that this is element no. $i, so unset it:

    unset($_SESSION['basket'][$i]);

    // and stop looping through the rest,
    // assuming there's only 1 product with this id:
    break;
  }
}

请注意,检查值而不是键也存在危险。假设您有一个这样构建的产品:

  Array
(
    [count] => 12
    [id] => 5
    [name] => some name
    [color] => some color
)

如果您检查所有值,就像您现在所做的那样,并尝试将其与某个 id 匹配,那么当该 id 恰好是“12”时会发生什么?

// the id you're looking for:
$someId = 12;

foreach ($product as $key => $value) {
  // first $key = count
  // first $value = 12
  if ($value == $someId) {
    // ...
    // but in this case the 12-value isn't the id at all
  }
}

所以:始终引用数组中的特定元素,在本例中为“id”(或您在应用中使用的任何名称)。不要检查随机值,因为你不能绝对确定它匹配时,这实际上是你正在寻找的正确值。

祝你好运!

【讨论】:

  • 感谢您的详细解释。我有 90% 在那里,但我从未收到索引号,因为我的第一个 foreach 看起来像这样 foreach ($_SESSION['basket'] as $basketArray)。这让我可以访问这些项目,但我丢失了索引号。感谢您的帮助,它现在工作正常。 :)
  • 这一切都有帮助,即使有错误,因为它突出了我们可能会忽略的小事情 :) 让我们保持敏锐:p
猜你喜欢
  • 2020-05-17
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-30
相关资源
最近更新 更多