Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7976635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:57:40+00:00 2026-06-04T08:57:40+00:00

I have php problem with formatting output from a CSV to make it available

  • 0

I have php problem with formatting output from a CSV to make it available as json for jquery ui.autocomplete.

fruit.csv:

apple, bananna, jackfruit, 
... etc

jquery from here:

http://jqueryui.com/demos/autocomplete/#default

$( "#fruits" ).autocomplete({
            source: '/path/to/fruit.json'
        });

PHP to convert CSV into json:

        // Callback function to output CSV as json object
        function _custom_json_from_csv() {
            $fruit_path = '/path/to/fruit.csv';
            $fruits = array_map("str_getcsv", file($fruit_path));
            drupal_json_output(array(array_values($fruits)));
            exit;
        }

       // Below are CMS codes for detailed illustration
            function drupal_json_output($var = NULL) {
              // We are returning JSON, so tell the browser.
              drupal_add_http_header('Content-Type', 'application/json');

              if (isset($var)) {
                echo drupal_json_encode($var);
              }
            }

            function drupal_json_encode($var) {
              // The PHP version cannot change within a request.
              static $php530;

              if (!isset($php530)) {
                $php530 = version_compare(PHP_VERSION, '5.3.0', '>=');
              }

              if ($php530) {
                // Encode <, >, ', &, and " using the json_encode() options parameter.
                return json_encode($var, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
              }

              // json_encode() escapes <, >, ', &, and " using its options parameter, but
              // does not support this parameter prior to PHP 5.3.0.  Use a helper instead.
              include_once DRUPAL_ROOT . '/includes/json-encode.inc';
              return drupal_json_encode_helper($var);
            }

// parts of json-encode.inc - drupal_json_encode_helper(), responsible for json output:
    case 'array':
      // Arrays in JSON can't be associative. If the array is empty or if it
      // has sequential whole number keys starting with 0, it's not associative
      // so we can go ahead and convert it as an array.
      if (empty($var) || array_keys($var) === range(0, sizeof($var) - 1)) {
        $output = array();
        foreach ($var as $v) {
          $output[] = drupal_json_encode_helper($v);
        }
        return '[ ' . implode(', ', $output) . ' ]';
      }
      // Otherwise, fall through to convert the array as an object.

    case 'object':
      $output = array();
      foreach ($var as $k => $v) {
        $output[] = drupal_json_encode_helper(strval($k)) . ':' . drupal_json_encode_helper($v);
      }
      return '{' . implode(', ', $output) . '}';

If there is a solution to directly consume CSV by jquery, that will be great. But no clue by now.

My problem is function _custom_json_from_csv() outputs non-expected format for ui.autocomplete. Note excessive of [[[…]]]:

[[["apple", "bananna", "jackfruit"]]]

While ui.autocomplete wants:

["apple", "bananna", "jackfruit"]

Any direction to format the function as expected by jquery ui.autocomplete?

PS: I don’t use #autocomplete_path form API, and instead using ui.autocomplete, for reasons:

1) the code is stored in a theme settings, no hook_menu is available by theme, I want to avoid a module for this need whenever possible.

2) There is a plan somewhere at d.o. to use ui.autocomplete, so consider this adventurous

3) My previous question from jquery viewpoint has led me to instead correct the output of json, rather than making jquery adapt to json.

4) This is more my php issue rather than drupal

Thanks

UPDATE:
Removing one array from drupal_json_output(array(array_values($fruits))); to drupal_json_output(array_values($fruits)); successfully reduced one [] (what is the name of this?). Obviously a miss from previous format with the leading group.
[[“apple”, “bananna”, “jackfruit”]]

I need to remove one more []

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:57:41+00:00Added an answer on June 4, 2026 at 8:57 am

    I think your code is “correct”, if you had:

    apple, bananna, jackfruit
    peanut, walnut, almont
    carrot, potato, pea
    

    then your function would end up as … etc

    [[apple, bananna, jackfruit],[peanut, walnut, almont],[carrot, potato, pea]]
    

    Which appears sensible.

    If you only want one row, why can’t you just use the result of

    $FileContents = file($fruit_path); 
    $fruits = str_getcsv($FileContents[0]);
    

    as that will turn an array of values in the first row, not an array of arrays of all the rows

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP I have a function, the problem is it will output an extra
I have a PHP/MySQL date formatting problem. Here, i create the date variables: $checkDate
I have PHP generating JSON data but I am having a problem making a
I'm having a strange problem in that I have php inserting text into a
Dear everybody who can help, I have this PHP > MongoDB problem, I want
I have a problem with my path. Say I have a PHP file in
I have a PHP login which sets 2 cookies once someone login. The problem
I have a problem in php code inserting values into database (I use PHPMyAdmin).
I have a problem with php header redirect. I already spent hours trying to
I have quite a strange problem with PHP and Apache on my local testing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.