M
M
MagoVinch2015-07-31 13:52:18
Programming
MagoVinch, 2015-07-31 13:52:18

How to implement filtering an array of objects with multiple parameters, which can have multiple values?

There is an array of objects, an object has several parameters, and each parameter can have several values. You need to do a search in this array with the given parameter values. For example
, the array consists of objects of the type Car
Parameters of the car
weight - 2000kg
color - red,
speed blue - 1,2,3, back
request example all - [unspecified], color - red, speed - 2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Ivanov, 2015-07-31
@MagoVinch

I can offer you 2 solutions.
The first banal foreach with comparison.
The second one will require PC memory, but can greatly speed up the filtering process.
At the moment of collecting objects into an array, you need to create another one in which the property will be the key, and the object references will be the values.

$arrayProp = array(
  'red' => array( &link_to_object ),
  '2'   => array( &link_to_object )
);

Then you only need to find unique objects in this array by the necessary keys.
The memory for the second array will hardly go away, since a reference to the object will be stored there. the link is very small

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question