php对象转数组
Ⅰ key1=value1$key2=value2&key3=value3$key4=value4 转数组php
$a='key1=value1$key2=value2$key3=value3$key4=value4';
$temparr=explode('$',$a);
foreach($temparras$value){
$temp=explode('=',$value);
$arr[$temp[0]]=$temp[1];
}
print_r($arr);
将字符串按"$"分割转数组
Array
(
[0]=>key1=value1
[1]=>key2=value2
[2]=>key3=value3
[3]=>key4=value4
)将上面数组的每个元素按"="分割转数组,$temp[0]就是key,$temp[1]就是value
然后将key和value同时放到arr新的数组里面结果就出来了
Array
(
[key1]=>value1
[key2]=>value2
[key3]=>value3
[key4]=>value4
)
Ⅱ thinkphp5 select对象怎么转数组
/**
* [std_class_object_to_array 将对象转成数组]
* @param [stdclass] $stdclassobject [对象]
* @return [array] [数组]
*/
function std_class_object_to_array($stdclassobject)
{
$_array = is_object($stdclassobject) ? get_object_vars($stdclassobject) : $stdclassobject;
foreach ($_array as $key =>$value) {
$value = (is_array($value) || is_object($value)) ? std_class_object_to_array($value) : $value;
$array[$key] = $value;
}
return $array;
} 你看过后很简单吧以后不会可以向我一样经常到后盾人找找相关教材看看就会了,希望能帮到你,给个采纳吧谢谢
Ⅲ thinkphp5 select对象怎么转数组
[std_class_object_to_array 将对象转成数组]
* @param [stdclass] $stdclassobject [对象]
* @return [array] [数组]
*/
function std_class_object_to_array($stdclassobject)
{
$_array = is_object($stdclassobject) ? get_object_vars($stdclassobject) : $stdclassobject;
foreach ($_array as $key = $value) {
$value = (is_array($value) || is_object($value)) ? std_class_object_to_array($value) : $value;
$array[$key] = $value;
}
return $array;
}如果还有不懂咱俩可以在后盾人找找答案。
Ⅳ php访问对象属性为数组时的情况
一样得,例如:
<?php
classstudent{
private$id;//定义属性,学号
private$name;//定义属性,姓名
private$teacher;//定义属性,所属老师,array
//读取值
publicfunctiongetteacher(){
$this->teacher=array('甲','乙');
return$this->teacher;
}
}
$d=newstudent();
$stu_teacher=$d->getteacher();
echo"<pre>";
print_r($stu_teacher);
echo"</pre>";
?>
Ⅳ php怎么把对象转为数组
这里定义2个php匿名对象和数组相互转换的函数,代码如下:
function array2object($array) {
if (is_array($array)) { $obj = new StdClass(); foreach ($array as $key => $val){ $obj->$key = $val; } } else { $obj = $array; } return $obj;}function object2array($object) { if (is_object($object)) { foreach ($object as $key => $value) { $array[$key] = $value; } } else { $array = $object; } return $array;}
用法示例如下:
$array = array('foo' => 'bar','one' => 'two','three' => 'four');$obj = array2object($array);print $obj->one; // output's "two"$arr = object2array($obj);print $arr['foo']; // output's bar
Ⅵ thinkphp5 select对象怎么转数组
/**
* [std_class_object_to_array 将对象转成数组]
* @param [stdclass] $stdclassobject [对象]
* @return [array] [数组]
*/
function std_class_object_to_array($stdclassobject)
{
$_array = is_object($stdclassobject) ? get_object_vars($stdclassobject) : $stdclassobject;
foreach ($_array as $key =>$value) {
$value = (is_array($value) || is_object($value)) ? std_class_object_to_array($value) : $value;
$array[$key] = $value;
}
return $array;
}你也可以去后盾人看看php基础教学视频看看,也是不错的选择,希望能帮到你,给个采纳吧谢谢╰(*´︶`*)╯
Ⅶ PHP 对象数组的筛选和判断
手打不易,有用请采纳。
$rArray = json_decode(json_encode($r),true);//通过json将对象转换为数组。
foreach($rArray as $k => $v){
if($v['banji']==1){ //如果banji=1则求语文成绩
echo "语文成绩={$v['yuwen']}";
}elseif($v['banji']==2){ //如果banji=2则求数学成绩
echo "语文成绩={$v['shuxue']}";
}
if($v['xingming']=='x'){ //有一个姓名为$x是否在banji1
if($v['banji']==1){
echo "xingmign为x的是班级1";
}else{
echo "xingmign为x的不是班级1";
}
}
}
Ⅷ php中数组对象转普通数组
/**
* 对象数组转为普通数组
* 此函数支持多维数组处理。
*
* @param array
* @return array
*/
function objarray_to_array($obj) {
$ret = array();
foreach ($obj as $key => $value) {
if (gettype($value) == "array" || gettype($value) == "object"){
$ret[$key] = objarray_to_array($value);
}else{
$ret[$key] = $value;
}
}
return $ret;
}
你可以去后盾人平台看看,里面的东西不错
Ⅸ PHP的stdClass类详解及几种数组对象转换方法
一、stdClass数组转对象
$arr=array();
$arr['a']=1;
$arr['b']=2;
$arr['c']=3;
$object=newstdClass;
foreach($arras$key=>$value){
$object->$key=$value;
}
var_mp($object);
结果输出如下:
object(stdClass)#1(3){
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
二、对象转数组
functionobject_to_array($obj)
{
$_arr=is_object($obj)?get_object_vars($obj):$obj;
foreach($_arras$key=>$val)
{
$val=(is_array($val)||is_object($val))?object_to_array($val):$val;
$arr[$key]=$val;
}
return$arr;
}
三、ArrayObject方法数组转对象
$arr=array('key1'=>'test1','key2'=>'test2');
var_mp(newArrayObject($arr));
结果输出如下:
object(ArrayObject)#1(1){
["storage":"ArrayObject":private]=>
array(2){
["key1"]=>
string(5)"test1"
["key2"]=>
string(5)"test2"
}
}
Ⅹ PHP中如何定义对象类型数组
应该是存到对象属性中吧
你把
public function setDepartment($department)
{
$this->department=$department;
}
改成
public function setDepartment($department)
{
$this->department[]=$department;
}
即可
private $department;
要不要改成
private $department=array();
其实无所谓