PHP 对象数组和一般的数组的相互转化

深渊向深渊呼唤
把数组转化成任何你想要的对象类型的数组:
function array2object($array) {
        $aboutVideoArr = array();
        if (is_array($array)) {
            foreach ($array as $value) {
                  $video = new Video(); 
                foreach ($value as $key => $val) {   
                    $video->$key = $val;
                }
                array_push($aboutVideoArr, $video); 
            }
        } else {
            $video = $array;
        } 
        return $aboutVideoArr;
    }
把对象转化成普通的数组:
function object2array($object) {
  if (is_object($object)) {
    foreach ($object as $key => $value) {
      $array[$key] = $value;
    }
  }
  else {
    $array = $object;
  }
  return $array;
} 

 

栏目