需要循环逻辑帮助 - 无法正确比较

时间:2013-11-18 16:23:47

标签: php mysql arrays loops foreach

我有下表:

The table

我使用此函数从中获取数据:

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,依此类推。

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

我的代码是否失败?为什么只有前两个项目被“标记”?我现在很匆忙,一旦我从会议中回来,我会尝试进一步解释我的问题。

1 个答案:

答案 0 :(得分:2)

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

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中的某个位置。无论阵列中的哪个位置。