我不明白为什么,但是当我将序列化对象插入数组时,empty()
函数返回true
而不是false
:
$arr = array();
$arr[] = serialize($obj);
empty($arr) => returns true
count($arr) => returns 1
当我转储$arr
时,我得到一个带有序列化对象的数组,而不是一个空数组。为什么?
以下是我的测试:
array(1) {
[2] => string(2090) "O:32:"Application_Model_RelServiceCart":5:{s:14:"*_id_service";s:1:"2";s:11:"*_id_cart";i:72;s:12:"*_quantity";s:1:"6";s:11:"*_taxable";b:1;s:10:"*_mapper";O:31:"Application_Model_DefaultMapper":2:{s:11:"*_dbTable";O:40:"Application_Model_DbTable_RelServiceCart":18:{s:8:"*_name";s:16:"rel_service_cart";s:11:"*_primary";a:2:{i:1;s:7:"id_cart";i:2;s:10:"id_service";}s:8:"*_cols";a:3:{i:0;s:7:"id_cart";i:1;s:10:"id_service";i:2;s:8:"quantity";}s:14:"*_definition";N;s:24:"*_definitionConfigName";N;s:6:"*_db";O:25:"Zend_Db_Adapter_Pdo_Mysql":11:{s:11:"*_pdoType";s:5:"mysql";s:20:"*_numericDataTypes";a:16:{i:0;i:0;i:1;i:1;i:2;i:2;s:3:"INT";i:0;s:7:"INTEGER";i:0;s:9:"MEDIUMINT";i:0;s:8:"SMALLINT";i:0;s:7:"TINYINT";i:0;s:6:"BIGINT";i:1;s:6:"SERIAL";i:1;s:3:"DEC";i:2;s:7:"DECIMAL";i:2;s:6:"DOUBLE";i:2;s:16:"DOUBLE PRECISION";i:2;s:5:"FIXED";i:2;s:5:"FLOAT";i:2;}s:20:"*_defaultStmtClass";s:21:"Zend_Db_Statement_Pdo";s:10:"*_config";a:8:{s:4:"host";s:9:"localhost";s:8:"username";s:4:"root";s:8:"password";s:0:"";s:6:"dbname";s:6:"zftest";s:7:"charset";s:4:"utf8";s:10:"persistent";b:0;s:7:"options";a:3:{s:11:"caseFolding";i:0;s:20:"autoQuoteIdentifiers";b:1;s:9:"fetchMode";i:2;}s:14:"driver_options";a:1:{i:1002;i:0;}}s:13:"*_fetchMode";i:2;s:12:"*_profiler";O:16:"Zend_Db_Profiler":4:{s:17:"*_queryProfiles";a:0:{}s:11:"*_enabled";b:0;s:21:"*_filterElapsedSecs";N;s:15:"*_filterTypes";N;}s:24:"*_defaultProfilerClass";s:16:"Zend_Db_Profiler";s:15:"*_caseFolding";i:0;s:24:"*_autoQuoteIdentifiers";b:1;s:22:"*_allowSerialization";b:1;s:30:"*_autoReconnectOnUnserialize";b:0;}s:10:"*_schema";N;s:12:"*_identity";i:1;s:12:"*_sequence";b:1;s:12:"*_metadata";a:0:{}s:17:"*_metadataCache";N;s:24:"*_metadataCacheInClass";b:1;s:12:"*_rowClass";s:17:"Zend_Db_Table_Row";s:15:"*_rowsetClass";s:20:"Zend_Db_Table_Rowset";s:16:"*_referenceMap";a:0:{}s:19:"*_dependentTables";a:0:{}s:17:"*_defaultSource";s:11:"defaultNone";s:17:"*_defaultValues";a:0:{}}s:13:"*_callTrace";s:32:"Application_Model_RelServiceCart";}}"
}
空($ arr):
bool(true)
count($ arr):
int(1)
is_array($ arr):
bool(true)
编辑: PHP版本:5.3.0 ,添加了完整的序列化对象
这是demo,但输出正常。请注意,我用简单的引号替换了第一个双引号,以免出现T_STRING错误。
编辑:转储bin2hex(序列化($ obj)):
我以这种方式添加对象:
if (is_a($service, 'Application_Model_RelServiceCart')) {
$this->_sitems[$service->service] = bin2hex(serialize($service));
}
转储:
array(1) {
[2] => string(4180) "4f3a33323a224170706c69636174696f6e5f4d6f64656c5f52656c5365727669636543617274223a353a7b733a31343a22002a005f69645f73657276696365223b733a313a2232223b733a31313a22002a005f69645f63617274223b693a37343b733a31323a22002a005f7175616e74697479223b733a313a2236223b733a31313a22002a005f74617861626c65223b623a313b733a31303a22002a005f6d6170706572223b4f3a33313a224170706c69636174696f6e5f4d6f64656c5f44656661756c744d6170706572223a323a7b733a31313a22002a005f64625461626c65223b4f3a34303a224170706c69636174696f6e5f4d6f64656c5f44625461626c655f52656c5365727669636543617274223a31383a7b733a383a22002a005f6e616d65223b733a31363a2272656c5f736572766963655f63617274223b733a31313a22002a005f7072696d617279223b613a323a7b693a313b733a373a2269645f63617274223b693a323b733a31303a2269645f73657276696365223b7d733a383a22002a005f636f6c73223b613a333a7b693a303b733a373a2269645f63617274223b693a313b733a31303a2269645f73657276696365223b693a323b733a383a227175616e74697479223b7d733a31343a22002a005f646566696e6974696f6e223b4e3b733a32343a22002a005f646566696e6974696f6e436f6e6669674e616d65223b4e3b733a363a22002a005f6462223b4f3a32353a225a656e645f44625f416461707465725f50646f5f4d7973716c223a31313a7b733a31313a22002a005f70646f54797065223b733a353a226d7973716c223b733a32303a22002a005f6e756d65726963446174615479706573223b613a31363a7b693a303b693a303b693a313b693a313b693a323b693a323b733a333a22494e54223b693a303b733a373a22494e5445474552223b693a303b733a393a224d454449554d494e54223b693a303b733a383a22534d414c4c494e54223b693a303b733a373a2254494e59494e54223b693a303b733a363a22424947494e54223b693a313b733a363a2253455249414c223b693a313b733a333a22444543223b693a323b733a373a22444543494d414c223b693a323b733a363a22444f55424c45223b693a323b733a31363a22444f55424c4520505245434953494f4e223b693a323b733a353a224649584544223b693a323b733a353a22464c4f4154223b693a323b7d733a32303a22002a005f64656661756c7453746d74436c617373223b733a32313a225a656e645f44625f53746174656d656e745f50646f223b733a31303a22002a005f636f6e666967223b613a383a7b733a343a22686f7374223b733a393a226c6f63616c686f7374223b733a383a22757365726e616d65223b733a343a22726f6f74223b733a383a2270617373776f7264223b733a303a22223b733a363a2264626e616d65223b733a363a227a6674657374223b733a373a2263686172736574223b733a343a2275746638223b733a31303a2270657273697374656e74223b623a303b733a373a226f7074696f6e73223b613a333a7b733a31313a2263617365466f6c64696e67223b693a303b733a32303a226175746f51756f74654964656e74696669657273223b623a313b733a393a2266657463684d6f6465223b693a323b7d733a31343a226472697665725f6f7074696f6e73223b613a313a7b693a313030323b693a303b7d7d733a31333a22002a005f66657463684d6f6465223b693a323b733a31323a22002a005f70726f66696c6572223b4f3a31363a225a656e645f44625f50726f66696c6572223a343a7b733a31373a22002a005f717565727950726f66696c6573223b613a303a7b7d733a31313a22002a005f656e61626c6564223b623a303b733a32313a22002a005f66696c746572456c617073656453656373223b4e3b733a31353a22002a005f66696c7465725479706573223b4e3b7d733a32343a22002a005f64656661756c7450726f66696c6572436c617373223b733a31363a225a656e645f44625f50726f66696c6572223b733a31353a22002a005f63617365466f6c64696e67223b693a303b733a32343a22002a005f6175746f51756f74654964656e74696669657273223b623a313b733a32323a22002a005f616c6c6f7753657269616c697a6174696f6e223b623a313b733a33303a22002a005f6175746f5265636f6e6e6563744f6e556e73657269616c697a65223b623a303b7d733a31303a22002a005f736368656d61223b4e3b733a31323a22002a005f6964656e74697479223b693a313b733a31323a22002a005f73657175656e6365223b623a313b733a31323a22002a005f6d65746164617461223b613a303a7b7d733a31373a22002a005f6d657461646174614361636865223b4e3b733a32343a22002a005f6d657461646174614361636865496e436c617373223b623a313b733a31323a22002a005f726f77436c617373223b733a31373a225a656e645f44625f5461626c655f526f77223b733a31353a22002a005f726f77736574436c617373223b733a32303a225a656e645f44625f5461626c655f526f77736574223b733a31363a22002a005f7265666572656e63654d6170223b613a303a7b7d733a31393a22002a005f646570656e64656e745461626c6573223b613a303a7b7d733a31373a22002a005f64656661756c74536f75726365223b733a31313a2264656661756c744e6f6e65223b733a31373a22002a005f64656661756c7456616c756573223b613a303a7b7d7d733a31333a22002a005f63616c6c5472616365223b733a33323a224170706c69636174696f6e5f4d6f64656c5f52656c5365727669636543617274223b7d7d"
}
Empty : bool(true)
Count : int(1)
is_array : bool(true)
答案 0 :(得分:1)
无法复制(Demo):
var_dump(PHP_VERSION);
$arr = array();
$arr[] = serialize($obj);
var_dump(empty($arr), count($arr));
输出:
string(5) "5.2.5"
bool(false)
int(1)
如您所见,数组不为空。实际上它包含一个字符串(当你发布时)。如果任何数组包含多于0个值(如果数组不是array()
),则PHP会将其视为非空数。
请参阅empty
。
如果相同的代码对您不同,请将您的输出添加到您的问题中,包括PHP版本。
答案 1 :(得分:1)
无法重现
$arr = array();
$arr[] = serialize($obj);
$a = empty($arr);
$b = count($arr);
var_dump($arr);
var_dump($a);
var_dump($b);
给出
array
0 => string 'N;' (length=2)
boolean false
int 1
你确定在将$ arr传递给empty()之前没有操作它吗?!