D
D
Denis Vyazemsky2015-09-09 15:49:57
PHP
Denis Vyazemsky, 2015-09-09 15:49:57

How to compare variable and multidimensional array?

I have such a problem, I have a variable and a multidimensional array
The array is stored in the session

Array
(
[107] => Array
(
[SIMPLE_BASKET_TITLE] => Black Burger
[SIMPLE_BASKET_QUO] => 1
[SIMPLE_BASKET_PRICE] => 180
[SIMPLE_BASKET_CATEGORY] => kfc
)
)

// сам массив $this->items <b>(Результат вывода написан выше)</b>
// $catraw - ПЕРЕМЕННАЯ которая допустим хранит в себе значение <b>(kfc)</b>

// Если такой элемент уже есть...
    if (array_key_exists($id, $this->items))
    {			
      // Увеличим количество
      $this->items[$id][self::QUO]++;	
    }
    else 
    {
//Проверка на то что в корзине присутствует товар из категории, 
//которой мы добаляем товары, если же нет, то очищаем корзину 
//и добавляем товар из другой категории

    if(in_array($catraw, $this->items)
    {
      // Добавим элемент
      $this->items[$id] = array
      (
        self::TITLE => $title,
        self::QUO	=> 1,
        self::PRICE => $price,
        self::CATEGORY => $category
      );			
    } else {
      $this->items = array(); // ЭТО СТРОКА ОЧИЩАЕТ КОРЗИНУ
      // Добавим элемент
      $this->items[$id] = array
      (
        self::TITLE => $title,
        self::QUO	=> 1,
        self::PRICE => $price,
        self::CATEGORY => $category
      );
    }

The task is this, we have categories of goods, only one category can be placed in the basket, if the category already contains goods of one category (for example, kfc) and we add a product of another category, our basket is reset and a product of another category is added!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arris, 2015-09-09
@didjit94

php.net/manual/en/function.in-array.php ?
PS Do not lie, but put.

D
Denis Vyazemsky, 2015-09-09
@didjit94

Thanks everyone! All options are great! Found another way, pushed the category into the session, and compare it!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question