php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71954 array_unique and datetimes
Submitted: 2016-04-04 08:01 UTC Modified: 2016-04-04 08:14 UTC
From: marcelloh at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.0.5 OS: win10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: marcelloh at gmail dot com
New email:
PHP Version: OS:

 

 [2016-04-04 08:01 UTC] marcelloh at gmail dot com
Description:
------------
I have this array filled with all kinds of types.
Today I used a DateTime object in that array for the first time.

In my code there is this line:
$aUniqueValues = array_unique($aValues);

and there is an fatal error now:
Catchable fatal error: Object of class DateTime could not be converted to string in MyClass.php on line 149

I can't find in the documentation
http://php.net/manual/en/function.array-unique.php
how to catch that error.


Test script:
---------------
$aValues = array();

$now = date('Y-m-d H:i:s');
$aValues['test1'] = $now;
$aValues['test2'] = DateTime::createFromFormat('Y-m-d H:i:s', $now);

$aUniqueValues = array_unique($aValues);


Expected result:
----------------
I would expect no fatal error, but a normal unique array.

I think, it is a bit strange that I can make a string out of a DateTime object, and that the code behind array_unique can't do that.



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-04-04 08:14 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-04-04 08:14 UTC] requinix@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

The test2 item is a DateTime object. It cannot be converted to a string automatically, which is what array_unique will try to do by default.
 [2017-03-05 08:26 UTC] kordosoft25 at gmail dot com
That is a bug... you can't change the status of this issue to: Not a bug when it returns fatal error...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 15 20:01:35 2024 UTC