搜索和更新多维数组

发布于 2025-01-03 01:54:07 字数 1746 浏览 6 评论 0原文

我搜索了这里的所有帖子,似乎找不到正确的方法来完成我需要的事情。

我有一个通过单独订单从数据库中提取的多维产品数组。

$products = array ();

$i = count ($products);

foreach ($orders as $order):
$res = mysql_query ("SELECT * FROM orders_products AS op LEFT JOIN orders_products_attributes AS opa ON op.orders_products_id = opa.orders_products_id WHERE op.orders_id = '$order'");
while ($row = mysql_fetch_array ($res)):
    $products[$i] = array (
        "model"     => $row['products_model'],
        "name"      => $row['products_name'],
        "option"    => $row['products_options_values'] ?  $row['products_options_values'] : "N/A",
        "qty"       => (int)$row['products_quantity']
    );
    $i++;
endwhile;
endforeach;

有些产品有多个选项,即颜色或盒子尺寸等。

我需要创建一个新数组,其中包含根据选项的每个产品的条目,并更新选项与现有产品数组相同的数量。

这就是我正在尝试的,但我似乎无法做到正确。

$items = array ();

$j = count ($items);

foreach ($products as $product):
      $exists = searchSubArray ($items, "model", $product['model']);
      if ($exists and ($exists['option'] == $product['option'])):
        // no matter what I do here I get either all the products, or
        // products without new quantities
      else:
        $items[$j] = array (
        "model" => $product['model'],
        "name" => $product['name'],
        "option" => $product['option'],
        "qty" => $product['qty']
        );
      endif;
$j++;
endforeach;

这是我在此处搜索答案时遇到的 searchSubArray 函数。

function searchSubArray (Array $array, $key, $value) {   
    foreach ($array as $subarray): 
        if (isset ($subarray[$key]) && $subarray[$key] == $value)
          return $subarray;       
    endforeach;
}

关于我如何实现这一点有什么建议吗?

I've searched through all the posts here and can't seem to find the right method to do what I need.

I have a multi-dimensional array of products pulled from a db, via individual orders.

$products = array ();

$i = count ($products);

foreach ($orders as $order):
$res = mysql_query ("SELECT * FROM orders_products AS op LEFT JOIN orders_products_attributes AS opa ON op.orders_products_id = opa.orders_products_id WHERE op.orders_id = '$order'");
while ($row = mysql_fetch_array ($res)):
    $products[$i] = array (
        "model"     => $row['products_model'],
        "name"      => $row['products_name'],
        "option"    => $row['products_options_values'] ?  $row['products_options_values'] : "N/A",
        "qty"       => (int)$row['products_quantity']
    );
    $i++;
endwhile;
endforeach;

Some products have multiple options ie color or box size etc.

I need to create a new array which contains entries for each product according to option and updates the quantities where the option is the same from the existing products array.

This is what I'm trying but I can't seem to get it right.

$items = array ();

$j = count ($items);

foreach ($products as $product):
      $exists = searchSubArray ($items, "model", $product['model']);
      if ($exists and ($exists['option'] == $product['option'])):
        // no matter what I do here I get either all the products, or
        // products without new quantities
      else:
        $items[$j] = array (
        "model" => $product['model'],
        "name" => $product['name'],
        "option" => $product['option'],
        "qty" => $product['qty']
        );
      endif;
$j++;
endforeach;

And here is the searchSubArray function that I came across while searching here for an answer to this.

function searchSubArray (Array $array, $key, $value) {   
    foreach ($array as $subarray): 
        if (isset ($subarray[$key]) && $subarray[$key] == $value)
          return $subarray;       
    endforeach;
}

Any suggestion on how I might implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文