-1

haven't touched PHP in ages and am a bit rusty. I need some guidance in where to go from here.

I've got two arrays that I want to compare and find the difference however I can't compare the two as they aren't in the right format.

I'm not exactly sure what the formats are called and don't usually delve into the realm of PHP.

Here's the first array that I want to convert:

    Array (
            [0] => 2018-08-21 10:00:00 
            [1] => 2018-08-28 10:00:00
            [2] => 2018-09-04 10:00:00 
            [3] => 2018-09-11 10:00:00
            [4] => 2018-09-18 10:00:00
          )

That I want to convert into something like this:

   Array ( 
           '0' => '2018-08-21 10:00:00', 
           '1' => '2018-08-28 10:00:00',
           '2' => '2018-09-04 10:00:00', 
           '3' => '2018-09-11 10:00:00', 
           '4' => '2018-09-18 10:00:00'
         )

Once I've done that, I can use array_diff to compare it to another array, however until then I'm a bit stuck. Any help would be appreciated.

The second array I wish to compare is in the same format. But with different values.

Here's my problem: https://3v4l.org/f734u.

Here's an example of what I want it to be like: https://3v4l.org/oeXHR.

Thanks in advance.

Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70
Elliot
  • 928
  • 10
  • 17
  • Both arrays are the same. What are you trying to do? – Felippe Duarte Aug 20 '18 at 22:12
  • I can't compare the two using array_diff as the first array isn't formatted properly. It only works if both array have '' around each of the values. @FelippeDuarte – Elliot Aug 20 '18 at 22:13
  • They are the same, as you can see here: https://3v4l.org/guC6v – Felippe Duarte Aug 20 '18 at 22:14
  • Thanks for your help. I may have not explained it properly. The array is pulled from a WordPress Database. So I am missing the ' ' marks around each of the values. See my example here: https://3v4l.org/f734u. @FelippeDuarte – Elliot Aug 20 '18 at 22:22
  • You may try `$new_array = array_map('strval' , $old_array);` – Shahin Aug 20 '18 at 22:33
  • 1
    key of element can be integer too - so no need to make it a string. However your values are invalid, that's why you get php parsing error. You've got a date, so values should be a string or some DateTime object. Seems that your array pulling code is not correct. Can you show it ? – Agnius Vasiliauskas Aug 21 '18 at 08:53

1 Answers1

0

If you're absolutely certain the data from your first array string is safe, and always looks this way, you could do something like this to convert it into a real array:

$array_string = 'Array ( [0] => 2018-08-21 10:00:00 [1] => 2018-08-28 10:00:00 [2] => 2018-09-04 10:00:00 [3] => 2018-09-11 10:00:00 [4] => 2018-09-18 10:00:00 )';

$array_string = preg_replace('/\[(\d+)\] => (.*?)(?=\s+[[)])/s', "'$1' => '$2',", $array_string);
eval("\$array1 = $array_string;");
// $array1 contains the array

Demo here: https://3v4l.org/G2B5i

Alternatively (and more safely), you can use preg_match_all with the same pattern and manually build the array yourself.

You could also go through serialized form/deserialization, which is also less risky and handles more situations (the code there uses a slightly different format though, so you'll probably have to adapt it).

Jeto
  • 14,596
  • 2
  • 32
  • 46