array_diff_assoc 在比对两个带key的数组时, 如果右边的数组没有对应的key, 左边对应的key是数组, 那没事, 不然就会转换左边的内容为字符串带来警告
function array_diff_assoc2($array, $array2)
{
$d = [];
foreach ($array as $key => $item) {
if (array_key_exists($key, $array2)) {
if (is_array($item)) {
if (is_array($array2[$key])) {
if ($item != $array2[$key]) {
$d[$key] = $item;
}
} else if (
(strpos($array2[$key], '[') === false
&& strpos($array2[$key], '{') === false)
|| $item != json_decode($array2[$key], true)
) {
$d[$key] = $item;
}
} else if (is_array($array2[$key])) {
if ($item != json_encode($array2[$key], 256)) {
$d[$key] = $item;
}
} else if ($item . '' != $array2[$key] . '') {
$d[$key] = $item;
}
} else {
$d[$key] = $item;
}
}
return $d;
}
- json_encode [php特殊环境下无法使用该方法的过度办法]
function json_encodes($arr)
{
$parts = array();
$is_list = false;
//Find out if the given array is a numerical array
if (is_array($arr)) {
$keys = array_keys($arr);
$max_length = count($arr) - 1;
if (
!is_null($keys)
&& array_key_exists('0', $keys)
&& ($keys [0] === 0)
&& ($keys [$max_length] === $max_length)) { //See if the first key is 0 and last key is length - 1
$is_list = true;
for ($i = 0; $i < count($keys); $i++) { //See if each key correspondes to its position
if ($i != $keys [$i]) { //A key fails at position check.
$is_list = false; //It is an associative array.
break;
}
}
}
foreach ($arr as $key => $value) {
if (is_array($value)) { //Custom handling for arrays
if ($is_list)
$parts [] = json_encodes($value); /* :RECURSION: */
else
$parts [] = '"' . $key . '":' . json_encodes($value); /* :RECURSION: */
} else {
$str = '';
if (!$is_list)
$str = '"' . $key . '":';
//Custom handling for multiple data types
if (is_numeric($value) && $value === $value * 1) {
$str .= $value;
} elseif ($value === false)
$str .= 'false'; //The booleans
elseif ($value === true)
$str .= 'true';
elseif ($value === null)
$str .= 'null';
else
$str .= '"' . addslashes($value) . '"'; //All other things
// :TODO: Is there any more datatype we should be in the lookout for? (Object?)
if (is_string($str)) {
$str = str_replace(PHP_EOL, '\r\n', $str);
}
$parts [] = $str;
}
}
}
$json = implode(',', $parts);
if ($is_list)
return '[' . $json . ']'; //Return numerical JSON
return '{' . $json . '}'; //Return associative JSON
}