K
K
Konstantin Prudnikov2018-01-31 02:07:34
PHP
Konstantin Prudnikov, 2018-01-31 02:07:34

How to see if the passed numeric value of the variable lies between the values ​​of the array?

The essence of the task is as follows:
The site has a slider that adjusts the price range and transmits 2 values, for example - "price from=1000" and "price to=2000",
in turn, these values ​​are elements, for example, of such an array:

$filters = [
    'Categories (xyz..)' => 'Обувь,Свадебная обувь по 1-ой паре',
    'Color' => ['белый', 'бежевый'],
    'Size' => '35',
    'Manufacturer' => 'кожзам',
    'Wholesale price' => [1000, 2000],
];

We have a two-dimensional array of such content, in which we will search for the specified price range:
array (size=22)
  'A072C02' => 
    array (size=2)
      1 => 
        array (size=67)
          'ID' => string 'A072C02' (length=7)
          'Wholesale price' => float 1404
          'Color' => string 'белый' (length=10)
          'Size' => float 35
          '' => null
      2 => 
        array (size=67)
          'ID' => string 'A072C02' (length=7)
          'Wholesale price' => float 2100
          'Color' => string 'белый' (length=10)
          'Size' => float 36 '' => null

So, the task is: if the price (Wholesale price) is not suitable, then we delete this internal element of the array (the 2nd, for example). Made the task as easy as possible. I would be grateful for the right direction of thought.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2018-01-31
@Stalker_RED

$range = [1000, 2000];
foreach($items as $key => $item) {
  $price = $item['price'];
  if ($price < $range[0] || $price > $range[1]) {
   unset($items[$key]);
  }
}
But if this array is selected from the base, then it's probably better to set it in the request.

V
Vasiliy_M, 2018-01-31
@Vasiliy_M

This is a trivial task and is not done at the php level - it is done at the base level. Those. it is the task of the base to give the necessary selection, to form an array. Why else would SQL be needed at all?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question