【问题标题】:Loop logic help needed - Can't compare correctly需要循环逻辑帮助 - 无法正确比较
【发布时间】:2013-11-18 16:23:46
【问题描述】:

我有下表:

我使用这个函数从中获取数据:

function get_cart_by($player_id)
{
    global $db;

    $sql = 'SELECT DISTINCT(item_id) FROM ' . PCP_MARKET_CART . ' 
        WHERE player_id = ' . (int) $player_id;
    $result = $db->sql_query($sql);
    $rowset = $db->sql_fetchrowseT($result);
    $db->sql_freeresult($result);

    $cart = array();
    foreach ($rowset as $item)
    {
        $cart[] = $item['item_id']; 
    }

    return $cart;
}

结果如下:

Array
(
    [0] => 16
    [1] => 17
    [2] => 49
    [3] => 48
    [4] => 18
    [5] => 19
    [6] => 51
)

现在我有一个数组,它列出了我在另一个表中的所有产品,而无需查看player_id。我想使用上面演示的数组,并为不使用player_id 的商品添加一个自定义类,例如显示用户在购物车中已有的商品。

另一个列出所有产品的数组如下所示:

Array
(
    [0] => Array
        (
            [item_id] => 16
            [parent_id] => 11
            [cat_position] => 0
            [item_position] => 1
            [item_type] => product
            [item_title] => Custom Business
            [item_description] => Some description
            [item_price] => 9.99
            [item_units] => 500
            [item_preview] => http://i.imgur.com/3eCpMMm.png
            [times_sold] => 0
            [daopay_url] => http://i.imgur.com/QA7bBfJ.jpg
            [public] => 1
            [time] => 1384709635
        )

       [1] => Array
        (
            [item_id] => 17
            [parent_id] => 11
            [cat_position] => 0
            [item_position] => 1
            [item_type] => product
            [item_title] => Custom Business
            [item_description] => Some description
            [item_price] => 9.99
            [item_units] => 500
            [item_preview] => http://i.imgur.com/3eCpMMm.png
            [times_sold] => 0
            [daopay_url] => http://i.imgur.com/QA7bBfJ.jpg
            [public] => 1
            [time] => 1384709635
        )
        [2] => Array
        (
            [item_id] => 49
            [parent_id] => 11
            [cat_position] => 0
            [item_position] => 1
            [item_type] => product
            [item_title] => Custom Business
            [item_description] => Some description
            [item_price] => 9.99
            [item_units] => 500
            [item_preview] => http://i.imgur.com/3eCpMMm.png
            [times_sold] => 0
            [daopay_url] => http://i.imgur.com/QA7bBfJ.jpg
            [public] => 1
            [time] => 1384709635
        )


)

现在基于第一个数组,我想在第二个数组上标记相同的商品 ID,并显示它们是不同的(在购物车上)。

我尝试了很多,但由于某种原因,我只标记了 item_id 16 和 17,其余的由于某种原因没有被“标记”。

这是我使用的代码:

$cartar = $market->get_cart_by($user->data['player_id']);
$cartln = sizeof($cartar) - 1;

// Fetch items of the selected category
    $items = $market->fetch_cat_items($cat_id); // Equivalent to the array above    
    $index  = 0;

    print_r($items);

    foreach ($items as $item)
    {
        $name = $item['item_name'];
                if ($cartln >= $index)
        {
            if ($cartar[$index] == $item['item_id'])
                $name .= $cartar[$index];
        }

        echo $name;

                $index++;
    }

我试图让这个例子以最好的方式解释我的案例。因此,当我回显$name 时,它只输出thename16thename17(这两个),但它不会继续到49 等等。

请注意,包含所有产品的数组非常大,我将其缩短仅用于演示目的。

我的代码是否失败了?为什么只有前两项被“标记”?我现在很着急,等我开完会回来再解释一下我的问题。

【问题讨论】:

  • if ($cartln >= $index) 是什么意思?

标签: php mysql arrays loops foreach


【解决方案1】:

我不知道我是否理解正确,但也许你想这样做:

foreach ($items as $item) {
    $name = $item['item_name'];

    if(in_array($item['item_id'],$cartar)) {
            $name .= $item['item_id'];
    }
    echo $name;
}

使用in_array() 检查item_id 是否存在于$cartar 中的某处。无论在数组中的哪个位置。

【讨论】:

  • 太棒了,你让我开心! :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 1970-01-01
相关资源
最近更新 更多