Rob Morris
Rob Morris

Reputation: 533

Convert array into a string ready ajax data filter using php

Can the following array be converted into the string below...

$eventsfilters = array(
    'language' => $languagePath,
    'page' => 1, 
    'limit' => 9, 
    'start_date' => time()
);

into this:

$ajax_filter = "'language': '" . $languagePath . "', 'page': 1, 'limit': 9, 'start_date': " . time();

This will then go into an ajax data filter.

Upvotes: 0

Views: 208

Answers (2)

David Jones
David Jones

Reputation: 4305

If it is going through an AJAX filter it will probably need to be in a JSON format. The string you posted is not valid json. It is best to just pass the array into json_encode.

$ajax_filter = json_encode($eventsfilters);

Which would return something like this:

{"language":"languagePath","page":1,"limit":9,"start_date":1412241074}

Upvotes: 1

Hardik Solanki
Hardik Solanki

Reputation: 3195

Just try with below code :

$eventsfilters = array(
    'language' => "sdfsd",
    'page' => 1, 
    'limit' => 9, 
    'start_date' => time()
);

foreach($eventsfilters as $key => $val){
    $arr[] = "'$key':".$val; 
}

$arr = implode(",", $arr);

echo $arr;

Upvotes: 0

Related Questions