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();
其實無所謂