您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在php中利用嵌套拼接數(shù)組,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
利用嵌套 拼接數(shù)組
<?php // 自 PHP 5.4 起 $array = [ "status" => "0", "message" => "ok", "arr"=> [] ]; class Person { public $name; public $age; //定義一個構(gòu)造方法初始化賦值 public function __construct($name,$age) { $this->name=$name; $this->age=$age; } } for($i=0;$i<10;$i++) { $p=new Person("ren",$i); $array["arr"][]=$p; } //var_dump($array); echo json_encode($array); ?>
php利用嵌套數(shù)組 解析混合json 包含對象數(shù)組
<?php function json_to_array($web) { $arr=array(); foreach($web as $k=>$v) { if(is_object($v)) $arr[$k]=json_to_array($v); //判斷類型是不是object else $arr[$k]=$v; } return $arr; } $s='{"webname":"homehf","url":"www.homehf.com","qq":"744348666"}'; //將字符轉(zhuǎn)成JSON $web=json_decode($s); $arr=array(); foreach($web as $k=>$v) $arr[$k]=$v; echo "<pre>"; print_r($arr); echo "</pre>"; $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}'; $web=json_decode($s); $arr=json_to_array($web); echo "<pre>"; print_r($arr); echo "</pre>"; /************************************************************************ ************************************************************************/ $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}'; $web=json_decode($s); echo '網(wǎng)站名稱:'.$web->webname.'<br />網(wǎng)址:'.$web->url.'<br />聯(lián)系方式:QQ-'.$web->contact->qq.' MAIL:'.$web->contact->mail; echo '<br /><br />'; /************************************************************************ ************************************************************************/ $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}'; $web=json_decode($s); echo json_encode($web); $mys='{"status":"0","message":"ok","arr":[{"name":"ren","age":0},{"name":"ren","age":1},{"name":"ren","age":2}, {"name":"ren","age":3},{"name":"ren","age":4},{"name":"ren","age":5},{"name":"ren","age":6},{"name":"ren","age":7}, {"name":"ren","age":8},{"name":"ren","age":9}]}'; $myweb=json_decode($mys); echo $myweb->status; for($i=0;$i<10;$i++) { echo $myweb->arr[$i]->age; echo '<br /><br />'; } ?>
關(guān)于怎么在php中利用嵌套拼接數(shù)組就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。