Can php be used to keyword search a csv file and return a row which contains it?

  1. profile image51
    laikeiposted 6 years ago

    Can php be used to keyword search a csv file and return a row which contains it?

    I need to search a large csv file and return the row which contains a form submitted keyword and print it to a results div on the same php page. If that is possible, please display sample code. many thanks

  2. profile image0
    jlcustompcposted 5 years ago

    Something like this:

    if (file_exists('YOURFILE.csv') != FALSE) {
        $handle  = file_get_contents('YOURFILE.csv') or exit;
        $handle_row = explode("\n", $handle);
        foreach ($handle_row as $key => $val) {
            $row_array = explode(',', $val);
            foreach ($row_array as $key => $val) {
                $row_array[$key] = trim(str_replace('"', '', $val));
                }
            $complete[] = $row_array;
            }
        }
    Then, pass $complete through the following function to sort by your second tier arrays' values.

    $array_elem_01 = 2; // Which Column to Sort By
    $array_elem_02 = 3; //  $val) {
            $sort_01[$key] = $val[$array_elem_01] . $val[$array_elem_02]; //  $val) {
            $output[] = $complete[$key];
            }
        }
    return $output;

    1. profile image51
      laikeiposted 5 years agoin reply to this

      Thank you much. I'll give this a go.

 
working