M
M
MIK Ek2015-11-24 04:52:50
PHP
MIK Ek, 2015-11-24 04:52:50

Is there a PHP library for filtering an sql array with a query?

It often happens that there is an array of data
$arr=[
["name"="Kolya", "age"="13", "sex"="m"],
["name"="Vasya", "age" ="20, "sex"="m"],
["name"="Olya", "age"="10", "sex"="f"],
];
And you need to remove all the boys from it from the beginning up to 15, then all Ol from 5, etc. Is there any PHP
library for this?
,20)->filter("sex","m")
is very hard to read and difficult to understand.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
D', 2015-11-24
@Denormalization

You can use https://github.com/illuminate/support
It has a Collection that can do a lot of useful things with arrays/objects.
There are also filter, find, contains and other functions.

S
Stalker_RED, 2015-11-24
@Stalker_RED

You can create a table, fill in the data there, and select using SQL. Performance is likely to be so-so, but the heroes are not looking for easy ways. And do not care about the curses from those who fit into this code later.
In raw php, it might look something like this:

$result = array_filter($data, function($d){
  return $d['sex'] == 'm' && $d['age'] <= 15;
});

Or even so
$boys = array_filter($data, 'filterBoys');

// а где-то там, в другом месте
function filterBoys($d){
  return $d['sex'] == 'm' && $d['age'] <= 15;
}

And if you know all the selection criteria in advance, you can select all the results in one pass .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question