array_intersect not working with explode

Hello, I’m trying to compare the values of two arrays. If they both have the same value then I want to print a message.

Here is my code:

foreach($booked as $row) {
$explodedDate[] = $row['items'].', '; 
}

$explodedDate = explode(",", implode($explodedDate));
$result = array_intersect($explodedDate, $validDates);
print_r($result);

$explodedDate = Array ( [0] => 14th Oct 2015/20th Oct 2015 [1] => 21st Oct 2015/27th Oct 2015 [2] => 28th Oct 2015/03th Nov 2015 [3] => )
$validDates = Array ( [0] => 21st Oct 2015/27th Oct 2015 )
But $result = Array ( )

$result should have the value 21st Oct 2015/27th Oct 2015

I’m pretty sure it’s something to do with the explode, because if I remove it, then it will work. The reason I need explode and implode is because in the database there could be 1 field with just one date and another field with several dates separated by a comma.

Can anyone help please?

Where is your implode “glue”?
http://php.net/manual/en/function.implode.php

this?

$explodedDate = explode(",", implode("", $explodedDate));

You want $explodedDate to be a String?
14th Oct 2015/20th Oct 201521st Oct 2015/27th Oct 201528th Oct 2015/03th Nov 2015

Let me try and explain better :

The below foreach will return several rows from the database. In this example I have two rows. One row with the value of 14th Oct 2015/20th Oct 2015, 21st Oct 2015/27th Oct 2015 and another with 28th Oct 2015/03th Nov 2015

foreach($booked as $row) {
$explodedDate[] = $row['items'].', '; 
}

I’m then trying to compare these returned values with my array $validDates. But because some results from the above will return more than one date separated by a comma, I need to split them using explode.

So then I combine all the returned values from the database using implode and put each date into $explodedDate array using explode:

$explodedDate = explode(",", implode("", $explodedDate));

So then I can check if the individual dates in each array match

$result = array_intersect($explodedDate, $validDates);

Hope that makes sense.

your issue is the additional space after the comma in the foreach.

I’d omit that completely and do the implode/explode with the comma.

and to make sure run the exploded array through trim (via array_walk() or array_map())

but essentially the real reason seems to be a non-normalised database (having comma-separated values in a single field is a sure sign of that) and hence the problem getting the data into a suitable format.

1 Like

Thank you @Dormilich, it is now working.

The structure of this table isn’t great, I struggled to find a better solution. Perhaps you or someone else could suggests something better?

I currently have a form which contains several text inputs, along with several checkboxes. There is an unknown number of checkboxes in the form since they have been generated with php and there could potentially be up to 100 (the checkboxes are the values of $explodedDate mentioned above). I need to store all the text input values and the checked checkbox values into the database. This is why I stored all the values of checkboxes into one field, on one row, separated with commas to prevent tons of rows being created from just one form submission.

If there is a better solution please do let me know.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.