Sorting multidimensional arrays with search criteria

I am working with a FLAT FILE database.

OK Just eliminate all the "why not use SQL". I have. This is a personal (training) project. I like to work with text files and want to mess around.

So, in 2 days I searched google and SO and tried many variations of the methods presented. However, they do not get what I need. But I correctly understood the question :-)

Now <in SQL you can do it this way (since sql is basically a fancy array)

SELECT * FROM [tablename] WHERE `some_field`='user_input1' ORDER BY `sequence_opt`;

I want to do this in a database of over 2000 entries. (they were serialized for easy storage in a text file)

I have currently done this: ($ data_lib - 2000+ records with 30 fields each, but received as

$data_lib[1]['a_field_name1']
$data_lib[2]['a_field_name2']
$data_lib[3]['a_field_name3']
$data_lib[4]['a_field_name etc...'])


 foreach($data_lib as $linenum=>$row)
            $sort[$linenum] = $row['some_field'];
            array_multisort($sort,SORT_DESC,$data_lib);

SELECT * FROM [tablename] ORDER BY `some_field '

. , , "WHERE" . SELECT. WHERE .

, , - - , ? , , , .

, .

!

+3
1

- :

$results = array_filter($records, function ($row) {
     return $row['some_field'] == 'user input';
});
usort($results, function ($a, $b) {
     return $a['sequence_opt'] - $b['sequence_opt'];  // assuming numeric field

     // alternatively something like:
     // return $a['sequence_opt'] == $b['sequence_opt'] ? 0 :
     //        ($a['sequence_opt'] < $b['sequence_opt'] ? -1 : 1);
});

, , SQL.

If this still messes up for you, provide a more detailed entry and the expected result.

+1
source

All Articles