function obj2arr(&$obj) { $obj = (array) $obj; foreach ($obj as &$v) { if (is_array($v)) { obj2arr($v); } } return $obj; }
static function getInstallations($condition, $pageSize = 40, $pageno = 1) { $skip = ($pageno - 1) * $pageSize; $args = array('className' => '_Installation', 'limit' => $pageSize, 'skip' => $skip, 'order' => '-createAt'); if (isset($condition) && count($condition) > 0) { $args['query'] = $condition; } $result = ParseClient::getInstance()->query($args); $list = obj2arr($result->results); return $list; }
public function test_obj2arr() { $a = ['key1' => 'val1', 'key2' => 'val2']; $o = $this->_get_test_obj(); $this->assertEquals($a, obj2arr($o)); $this->assertEquals($a, $o); $this->assertNotEquals($a + ['key3' => 'val3'], obj2arr($o)); $o = $this->_get_test_obj(); $o->key3 = 'val3'; $this->assertEquals($a + ['key3' => 'val3'], obj2arr($o)); $this->assertEquals($a + ['key3' => 'val3'], $o); $o = $this->_get_test_obj(); $o->key4 = ['v4', 'v44', 'v444']; $this->assertEquals($a + ['key4' => ['v4', 'v44', 'v444']], obj2arr($o)); }
public function obj2arr($obj) { if (is_object($obj)) { $obj = (array) $obj; } if (is_array($obj)) { $new = array(); foreach ($obj as $key => $val) { $new[$key] = obj2arr($val); } } else { $new = $obj; } return $new; }
function getListByPointer($className, $merchant, $pointType, $pointId) { $listStr = 'List_' . $className . '_' . $pointType . '_' . $pointId; $refreshStr = $listStr . '_Refresh'; $list = S($listStr); var_dump($pointId); var_dump($list); var_dump(S($refreshStr)); if (!$list || S($refreshStr)) { $args = array('className' => $className, 'query' => array(strtolower($pointType) => parsePointer($pointType, $pointId), 'merchant' => $merchant, 'delFlg' => false)); $result = ParseClient::getInstance()->query($args); $list = obj2arr($result->results); S($listStr, $list, C('CACHE_TIMEOUT')); S($refreshStr, false); } else { dump('cache mode'); } return $list; }
/** * Convert object to array. * * @param object|array $obj Object to convert to array. * * @return array */ function obj2arr($obj) { if (is_array($obj)) { foreach ($obj as $i => $o) { $obj[$i] = obj2arr($o); } return $obj; } elseif (is_object($obj)) { return json_decode(json_encode($obj), true); } return $obj; }
/** * @param $array * @param $tblName * @return bool * Saves an array $array to a table $tblName * The $array could contain enteries that may not belong to $tblName so we find tableFields first so that only related fields are saved to the db */ public function save($array, $tblName) { // $tblFields = array_flip( $db->getTableFieldsList($tblName) ); $classname = tbl2cls($tblName); $arrayToSave = obj2arr($classname::getColumns()); $dataFound = false; foreach ($arrayToSave as $field => $value) { if (array_key_exists($field, $array)) { $arrayToSave[$field] = $array[$field]; $dataFound = true; } else { unset($arrayToSave[$field]); } } if ($dataFound) { if (isset($arrayToSave['id']) && !empty($arrayToSave['id'])) { // pr("updating..."); // i.e. the record already exists, update it return $this->update($arrayToSave, $tblName); } else { // pr("creating..."); // i.e. the record does not already exist, create it return $this->create($arrayToSave, $tblName); } } return false; }
public function login($args) { $params = array('username' => $args['username'], 'password' => $args['password'], 'method' => 'GET'); $return = $this->request($params, 'Login'); if ($this->checkResponse($return, '200')) { $user = json_decode($return['response']); $merchant = obj2arr($user->merchant); session('user', $user); session('merchant', $merchant); } else { return false; } return true; }
/** * Simple key=val replace processing with sub arrays too */ function _process_replaces($string, array &$replace, $name = '') { if (!strlen($string) || false === strpos($string, '{')) { return $string; } // Need to optimize complex replace arrays and templates not containing sub replaces $regex_sub_pairs = '~\\{([a-z0-9_-]+)\\.([a-z0-9_-]+)\\}~ims'; $has_sub_pairs = preg_match($regex_sub_pairs, $string); // Prepare pairs array of simple string replaces $pairs = []; $cleanup_keys = []; foreach ((array) $replace as $item => $value) { if (is_object($value) && !method_exists($value, 'render')) { $replace[$item] = obj2arr($value); $value = $replace[$item]; } // Allow to replace simple 1-dimensional array items (some speed loss, but might be useful) if (is_array($value)) { if (!$has_sub_pairs) { continue; } // 2+ levels deep detected, but not supported if (is_array(current($value))) { continue; } foreach ((array) $value as $_sub_key => $_sub_val) { $pairs['{' . $item . '.' . $_sub_key . '}'] = $_sub_val; } $cleanup_keys[$item] = ''; // Simple key=val replace } else { $pairs['{' . $item . '}'] = $value; } } if ($has_sub_pairs) { $avail_arrays = $this->tpl->_avail_arrays; // ('get' => '_GET') foreach ((array) $avail_arrays as $short => $v) { $v = eval('return $' . $v . ';'); // !! Do not blindly change to $$v, need to figure out before why it does not work foreach ((array) $v as $key => $val) { if (is_array($val)) { continue; } $pairs['{' . $short . '.' . $key . '}'] = $val; } $cleanup_keys[$short] = ''; } } if ($pairs) { $string = str_replace(array_keys($pairs), $pairs, $string); } // Modules properties direct access, example: {main.USER_ID} # if (strpos($string, '{') !== false) { # $string = preg_replace_callback($regex_sub_pairs, function($m) use ($replace, $name) { # return _class_safe($m[1])->$m[2]; # }, $string); # } // Cleanup, using regex pairs if ($cleanup_keys) { $regex_pairs = []; foreach ($cleanup_keys as $k => $v) { $regex_pairs['~\\{' . preg_quote($k, '~') . '\\.[a-z0-9_-]+\\}~i'] = ''; } $string = preg_replace(array_keys($regex_pairs), '', $string); } return $string; }
/** * This function generate dividing table contents per pages */ function divide_pages($sql = '', $url_path = '', $render_type = '', $records_on_page = 0, $num_records = 0, $tpls_path = '', $add_get_vars = 1, $extra = []) { if (is_array($sql)) { $sql_is_array = true; } elseif (is_callable($sql)) { $sql_is_callable = true; } elseif (is_object($sql)) { if ($sql instanceof yf_db_query_builder_driver) { $sql_is_query_builder = true; } else { $sql_is_object = true; } } if ($sql_is_query_builder) { $sql = $sql->sql(); } elseif ($sql_is_object) { $sql = obj2arr($sql); } elseif ($sql_is_callable) { $sql = (array) $sql(func_get_args()); } // Override default method for input array $method = is_array($sql) ? 'go_with_array' : 'go'; return _class('divide_pages', 'classes/common/')->{$method}($sql, $url_path, $render_type, $records_on_page, $num_records, $tpls_path, $add_get_vars, $extra); }
/** * @Title: comboxgetaction * @Description: todo(根据节点父ID查询出控制器中的未加入节点的方法) * @param unknown_type $t * @return multitype:multitype:unknown * @author liminggang * @date 2013-6-4 下午2:49:23 * @throws */ public function comboxgetaction($t = "") { $model = M("node"); if ($t == "") { $id = $_POST['moduleid']; } else { $id = $t; } $map = array(); $map['pid'] = $id; $map['level'] = 4; $map['status'] = 1; $list = $model->where($map)->getField("name,title"); //获取已存在的信息 $already_action = array_keys($list); //标准模式数组 $sclmodel = D('SystemConfigList'); // $normArray = $sclmodel->getNormArray(); //过滤数组,过滤不需要显示的方法名称 $notarr = $sclmodel->getCheckOutMethod(); $map = array(); $id = "240"; $map["id"] = $id; $info = $model->where($map)->find(); print_r($model->getLastSql()); $class = $info['name'] . "Action"; //开启反射 $my_object = new ReflectionClass($class); $class_methods = $my_object->getMethods(ReflectionMethod::IS_PUBLIC); $class_methods = obj2arr($class_methods); foreach ($class_methods as $key => $val) { // if ($val['class'] != $class) { unset($class_methods[$key]); } if (substr($val['name'], 0, 1) == "_" || substr($val['name'], 0, 6) == "lookup" || substr($val['name'], 0, 6) == "combox") { //unset($class_methods[$key]); } } print_r($class_methods); exit; // $arr=array(); // $m=A($info['name']); // $m2=A("Common"); // foreach($class_methods as $k=>$v ){ // //过滤数组及某些带前缀的通用方法 // if( substr($v,0,7) == "_before" || substr($v,0,6) == "_after" || substr($v,0,6) == "lookup" || substr($v,0,6) == "combox" ||in_array($v,$notarr)) { // continue; // } // //判断控制器中,是否有此方法 // if( !method_exists($m,$v) && !method_exists($m2,$v)){ // continue; // } // $a=array(); // $a[]=$v; // if(in_array($v,$already_action)){ // $a[]=$list[$v]; // } // // else{ // // $a[]=$normArray[$v]; // // } // $arr[]=$a; // } // if( $t=="" ){ // echo json_encode($arr); // }else{ // return $arr; // } }
/** * 把一个对象转换成数组 * * @param type $array * @return type */ function obj2arr($array) { if (is_object($array)) { $array = (array) $array; } if (is_array($array)) { foreach ($array as $key => $value) { $array[$key] = obj2arr($value); } } return $array; }
/** */ function _render_get_data(&$params) { $default_per_page = MAIN_TYPE_USER ? conf('user_per_page') : conf('admin_per_page'); if ($params['rotate_table']) { $default_per_page = 10; } $pager = ['path' => $params['pager_path'], 'type' => $params['pager_type'], 'records_on_page' => $params['pager_records_on_page'] ?: $default_per_page, 'num_records' => $params['pager_num_records'] ?: 0, 'stpl_path' => $params['pager_stpl_path'] ?: '', 'add_get_vars' => $params['pager_add_get_vars'] ?: 1, 'sql_callback' => $params['pager_sql_callback'] ?: null]; $sql = $this->_sql; $ids = []; if (is_array($sql)) { $sql_is_array = true; } elseif (is_callable($sql)) { $sql_is_callable = true; } elseif (is_object($sql)) { if ($sql instanceof yf_db_query_builder_driver) { $sql_is_query_builder = true; } else { $sql_is_object = true; } } if ($sql_is_query_builder) { $sql = $sql->sql(); } if ($sql_is_array || $sql_is_object || $sql_is_callable) { if ($sql_is_object) { $data = obj2arr($sql); } elseif ($sql_is_callable) { $data = (array) $sql($params); } else { $data = $sql; } unset($sql); if ($params['filter']) { $this->_filter_array($data, $params['filter'], $params['filter_params']); } $pager['out'] = common()->divide_pages(null, null, null, $pager['records_on_page'], count($data)); $pages = $pager['out'][1]; if (count($data) > $pager['records_on_page']) { $slice_start = (empty($_GET['page']) ? 0 : intval($_GET['page']) - 1) * $pager['records_on_page']; $slice_end = $pager['records_on_page']; $data = array_slice($data, $slice_start, $slice_end, $preserve_keys = true); } $total = count($data); $ids = array_keys($data); } elseif (strlen($sql)) { if (is_object($params['db'])) { $db = $params['db']; $pager['extra']['db'] = $db; } else { $db = db(); } // TODO: add direct support for query builder and filter if ($params['filter']) { list($filter_sql, $order_sql) = $this->_filter_sql_prepare($params['filter'], $params['filter_params'], $sql); // These 2 arrays needed to be able to use filter parts somehow inside methods $this->_filter_data = $params['filter']; $this->_filter_params = $params['filter_params']; } if (!$filter_sql && isset($params['filter_params']['__default_filter'])) { $filter_sql = $params['filter_params']['__default_filter']; } if (!$order_sql && isset($params['filter_params']['__default_order'])) { $order_sql = $params['filter_params']['__default_order']; } if ($filter_sql || $order_sql) { $sql_upper = strtoupper($sql); if (strpos($sql, '/*FILTER*/') !== false) { $sql = str_replace('/*FILTER*/', ' ' . $filter_sql . ' ', $sql); } elseif (strpos($sql_upper, 'WHERE') === false) { if (strpos($sql_upper, 'GROUP BY') === false) { $sql .= ' WHERE 1 ' . $filter_sql; } else { $sql = str_replace('GROUP BY', ' WHERE 1 ' . $filter_sql . ' GROUP BY', $sql); } } else { $sql .= ' ' . $filter_sql; } if ($order_sql) { if (strpos($order_sql, 'ORDER BY') === false) { $order_sql = 'ORDER BY ' . $order_sql; } if (strpos($sql, '/*ORDER*/') !== false) { $sql = str_replace('/*ORDER*/', ' ' . $order_sql . ' ', $sql); } else { $sql .= ' ' . $order_sql; } } } $pager['out'] = common()->divide_pages($sql, $pager['path'], $pager['type'], $pager['records_on_page'], $pager['num_records'], $pager['stpl_path'], $pager['add_get_vars'], $pager['extra']); $add_sql = $pager['out'][0]; $pages = $pager['out'][1]; $total = $pager['out'][2]; $items = []; $q = $db->query($sql . $add_sql); while ($a = $db->fetch_assoc($q)) { if (isset($a['id'])) { $data[$a['id']] = $a; $ids[$a['id']] = $a['id']; } else { $data[] = $a; } } } // Save result details to be used by outer code $this->_total = $total; $this->_pages = $pages; $this->_ids = $ids; $this->_pager = $pager; return ['data' => $data, 'pages' => $pages, 'total' => $total, 'ids' => $ids]; }