Exemple #1
0
 /**
  * @param array $data
  * @param string $separator
  * @return array|\stdClass
  */
 public static function toMulti(array $data, $separator = '.')
 {
     $result = [];
     $toSubAssoc = function ($data, $separator) {
         $object = new \stdClass();
         foreach ($data as $key => $val) {
             $key = explode($separator, $key);
             if (count($key) === 1) {
                 $object->{$key}[0] = $val;
             } elseif (count($key) === 2) {
                 //$object->$key[0] = new \stdClass();
                 @($object->{$key}[0]->{$key}[1] = $val);
             } elseif (count($key) === 3) {
                 @($object->{$key}[0]->{$key}[1]->{$key}[2] = $val);
             } else {
                 $end_key = end($key);
                 unset($key[count($key) - 1]);
                 $buff = implode($separator, $key);
                 @($object->{$buff}->{$end_key} = $val);
             }
         }
         return $object;
     };
     if (ArrayHelper::depth($data) === 0) {
         $result = $toSubAssoc($data, $separator);
     } else {
         foreach ($data as $value) {
             $result[] = $toSubAssoc($value, $separator);
         }
     }
     return $result;
 }