Mitchell B Mitchell B - 1 year ago 55
PHP Question

Searching and printing multi-dimensional array

Wondering if I can have some direction and clarity in what I need to do. I am taking an input (text input) and searching a CSV file which I can convert to an array.

I have figured out to search it with this:

$keys = array_keys(array_column($csv, 0), $SearchThis);

is my array (multidimensional), and
is the input to search for.

My problem is that
makes an array with values of which row that data was found in.

I want to be able to display a table with those rows out of the main array
. (Each row has a list of data that will be displayed on the table.)

I am fairly new to PHP, so I hope this is an easy fix?

Any help would be greatly appreciated. Especially the logic behind taking rows from the arrays etc.

Max Max
Answer Source

Well if your code is returning the correct rows already, then all you need to do is filter the array to contain only those rows. For example:

$keys = array_keys(array_column($csv, 0), $SearchThis);  // Your original code
$new_csv = [];                                           // [] is shorthand for an empty array.
foreach($keys as $index) {                               // Iterate over the keys
    $new_csv[] = $csv[$index];                           // Copy the matching rows to our new array

$new_csv will now contain only the matching rows from the original now.