XF 2.1 implode or json_encode maybe something else

Ozzy47

Well-known member
Okay, in a addon I am working on to log user searches, I have this in the php file
PHP:
$data = $search['search_constraints'];
$searchConstraints = implode(", ",$data);

Which will return searches with constraints like this, 2020-01-15, 1, Ozzy47

Now if I change my PHP file to this
PHP:
$data = $search['search_constraints'];
$searchConstraints = json_encode($data);

Returns the searches with constraints like this, {"newer_than":"2020-01-15","title_only":"1","users":"Ozzy47"}

How I would like the constraints to be stored is like this, newer_than 2020-01-15, title_only 1, users Ozzy47
 
Last edited:
I think the simplest would be something like this (depending):
PHP:
$myConstaints = 'newer_than ' . $data['newer_than'] . ', title_only ' . $data['title_only'] . ', users ' . $data['users']
 
That seems to work, but if there is missing constraints then you get a error returned such as, [E_NOTICE] Undefined index: newer_than
 
Yeah, I assumed all the keys would always be there, so if not:


PHP:
$myConstraints = '';
foreach ($data as $key => $value)
{
    $myConstraints .= $key . ' ' . $value . ', ';
}

then remove the last ', ' from $myConstraints.
 
Back
Top Bottom