From 1aeeb2b5850116352e3712cb600117d2ed3693cd Mon Sep 17 00:00:00 2001 From: Dyan Galih Date: Mon, 1 Jul 2019 06:36:19 +0700 Subject: [PATCH] Fix serialize data in array --- src/Tools/PopoTools.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Tools/PopoTools.php b/src/Tools/PopoTools.php index f85b176..36276c0 100644 --- a/src/Tools/PopoTools.php +++ b/src/Tools/PopoTools.php @@ -32,23 +32,27 @@ public function serializeJson($object) */ public function serialize($object) { - $objectAsArray = (array) $object; + $objectAsArray = (array)$object; foreach ($objectAsArray as $key => $value) { if (stripos($key, "\0") === 0) { $newKey = $this->fixKeyName($key); $this->replaceKey($objectAsArray, $key, $newKey); - }else{ + } else { $newKey = $key; } - - if($value instanceof Collection){ + + if ($value instanceof Collection) { $value = $this->serialize($objectAsArray[$newKey]); $objectAsArray[$newKey] = $value['items']; - }elseif(is_object($value)) { + } elseif (is_object($value)) { $objectAsArray[$newKey] = $this->serialize($objectAsArray[$newKey]); + } elseif (is_array($value)) { + for ($i = 0; $i < count($value); $i++) { + $objectAsArray[$newKey][$i] = $this->serialize($value[$i]); + } } } - + return $objectAsArray; } @@ -73,7 +77,7 @@ function replaceKey(&$array, $curkey, $newkey) * @param string $oldKey * @return string */ - function fixKeyName(string $oldKey) : string + function fixKeyName(string $oldKey): string { return substr($oldKey, strpos($oldKey, "\0", 2) + 1); }