⑴ 如何在php定義一個類的數組
<?
class Animal{
function bake(){
echo "我在叫……";
}
}
$array=array();
for($i=0;$i<3;$i++){
array_push($array,new Animal());
}
echo $array[0]->bake();
?>
⑵ 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 中怎樣把類中的屬性轉化為一個數組
<?php
class test{
var $a1=1;
var $a2=2;
var $a3=3;
function aaa(){
return false;
}
}
$a = new test();
$array = array();
foreach ($a as $b){
$array[] = $b;
}
print_r($array);
?>
⑷ php創建一個類,在類中聲明一個數組存放另一個對象,為什麼無法調用數組中對象的方法
//新建一個類User用來存放這,三個數據
//結果放到一個User數組中,你看這可以嗎?
//還是說要放到一個list中?
public class ObjectTest {
public static void main(String[] args) {
String[] id = new String[8];
String[] type = new String[8];
String[] username = new String[8];
User[] users = getUsers(type, id, username);
}
private static User[] getUsers(String[] type, String[] id, String[] username) {
User[] users = new User[type.length];
for (int i = 0; i < type.length; i++) {
users[i] = new User(id[i], type[i], username[i]);
}
return users;
}
}
class User {
public User(String id, String tpye, String username) {
this.tpye = tpye;
this.id = id;
this.username = username;
}
private String tpye;
private String id;
private String username;
public String getTpye() {
return this.tpye;
}
public String getId() {
return this.id;
}
public String getUsername() {
return this.username;
}
public void setTpye(String tpye) {
this.tpye = tpye;
}
public void setId(String id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
}
⑸ php類中數組怎麼使用
你把 private $a; 定義成 public $a,就可以直接用$obj->a 了。
$obj->a['key'] 就是你要訪問的值。
⑹ php 元素為類的數組 的排序問題
可以排序,但是你得自己寫一個比較類變數大小的函數,使用函數usort進行排序,以下內容粘貼自PHP手冊:
bool usort ( array &$array, callback $cmp_function )
本函數將用用戶自定義的比較函數對一個數組中的值進行排序。如果要排序的數組需要用一種不尋常的標准進行排序,那麼應該使用此函數。
比較函數必須在第一個參數被認為小於,等於或大於第二個參數時分別返回一個小於,等於或大於零的整數。
注意: 如果兩個成員比較結果相同,則它們在排序後的數組中的順序未經定義。到 PHP 4.0.6 之前,用戶自定義函數將保留這些單元的原有順序。但是由於在 4.1.0 中引進了新的排序演算法,結果將不是這樣了,因為對此沒有一個有效的解決方案。
注意: 本函數為 array 中的單元賦予新的鍵名。這將刪除原有的鍵名而不僅是重新排序。
如果成功則返回 TRUE,失敗則返回 FALSE。
例 311. usort() 例子
<?php
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
foreach ($a as $key => $value) {
echo "$key: $value\n";
}
?>
上例將輸出:
0: 1
1: 2
2: 3
3: 5
4: 6
注意: 很明顯在這個小例子中用 sort() 函數更合適。
例 312. 使用多維數組的 usort() 例子
<?php
function cmp($a, $b)
{
return strcmp($a["fruit"], $b["fruit"]);
}
$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";
usort($fruits, "cmp");
while (list($key, $value) = each($fruits)) {
echo "\$fruits[$key]: " . $value["fruit"] . "\n";
}
?>
當排序多維數組時,$a 和 $b 包含到數組第一個索引的引用。
上例將輸出:
$fruits[0]: apples
$fruits[1]: grapes
$fruits[2]: lemons
例 313. 使用對象的成員函數的 usort() 例子
<?php
class TestObj {
var $name;
function TestObj($name)
{
$this->name = $name;
}
/* This is the static comparing function: */
function cmp_obj($a, $b)
{
$al = strtolower($a->name);
$bl = strtolower($b->name);
if ($al == $bl) {
return 0;
}
return ($al > $bl) ? +1 : -1;
}
}
$a[] = new TestObj("c");
$a[] = new TestObj("b");
$a[] = new TestObj("d");
usort($a, array("TestObj", "cmp_obj"));
foreach ($a as $item) {
echo $item->name . "\n";
}
?>
上例將輸出:
b
c
d
⑺ php 類里能包含數組嗎
在class定義外的變數的作用域達不到class定義的裡面,
$this->x=$_arr['pagetitle_index']; ------------------- $_arr['pagetitle_index']這個值是個NULL
必須在實例化時用$v把參數傳進class里:
<?php
$_arr=array();
$_arr['pagetitle_index']='心想事成';
class Something {
var $x;
function Something($v) {
$this->x=$v;
}
function getX() {
return $this->x;
}
}
$obj=new Something($_arr['pagetitle_index']); // 實例化時傳參
$see=$obj->getX();
echo $see."<br>";
?>
這個不是數組的問題。
⑻ php數組寫法
rule_name是一個數組啊他應該有個原始類型的類型的,把rule_section數組當成一個數組數據加進去就行了。我把你的JSON改了一下。你看看
{
"code":100,
"message":"計費模式列表",
"rule_name":[
{
"id":"10",
"box_id":null,
"rule_name":"規則",
"gid":"10",
"rule_section":[
{
"id":"9",
"rule_id":"10",
"start_time":"00:00:00",
"end_time":"10:00:00",
"money":"6000",
"update_time":"1512528175"
},
{
"id":"10",
"rule_id":"10",
"start_time":"10:00:00",
"end_time":"20:00:00",
"money":"6000",
"update_time":"1512528175"
},
{
"id":"11",
"rule_id":"10",
"start_time":"20:00:00",
"end_time":"00:00:00",
"money":"6000",
"update_time":"1512528175"
},
{
"id":"7",
"rule_id":"9",
"start_time":"00:00:00",
"end_time":"12:00:00",
"money":"6000",
"update_time":"1512527986"
},
{
"id":"8",
"rule_id":"9",
"start_time":"12:00:01",
"end_time":"23:59:59",
"money":"6000",
"update_time":"1512527986"
},
{
"id":"5",
"rule_id":"8",
"start_time":"00:00:00",
"end_time":"12:00:00",
"money":"6000",
"update_time":"1511852577"
},
{
"id":"6",
"rule_id":"8",
"start_time":"12:00:01",
"end_time":"23:59:59",
"money":"6000",
"update_time":"1511852577"
}
]
},
{
"id":"9",
"box_id":null,
"rule_name":"規則",
"gid":"10",
"rule_section":[
{
"id":"9",
"rule_id":"10",
"start_time":"00:00:00",
"end_time":"10:00:00",
"money":"6000",
"update_time":"1512528175"
},
{
"id":"10",
"rule_id":"10",
"start_time":"10:00:00",
"end_time":"20:00:00",
"money":"6000",
"update_time":"1512528175"
},
{
"id":"11",
"rule_id":"10",
"start_time":"20:00:00",
"end_time":"00:00:00",
"money":"6000",
"update_time":"1512528175"
},
{
"id":"7",
"rule_id":"9",
"start_time":"00:00:00",
"end_time":"12:00:00",
"money":"6000",
"update_time":"1512527986"
},
{
"id":"8",
"rule_id":"9",
"start_time":"12:00:01",
"end_time":"23:59:59",
"money":"6000",
"update_time":"1512527986"
},
{
"id":"5",
"rule_id":"8",
"start_time":"00:00:00",
"end_time":"12:00:00",
"money":"6000",
"update_time":"1511852577"
},
{
"id":"6",
"rule_id":"8",
"start_time":"12:00:01",
"end_time":"23:59:59",
"money":"6000",
"update_time":"1511852577"
}
]
},
{
"id":"8",
"box_id":"1",
"rule_name":"規則名稱",
"gid":"10",
"rule_section":[
{
"id":"9",
"rule_id":"10",
"start_time":"00:00:00",
"end_time":"10:00:00",
"money":"6000",
"update_time":"1512528175"
},
{
"id":"10",
"rule_id":"10",
"start_time":"10:00:00",
"end_time":"20:00:00",
"money":"6000",
"update_time":"1512528175"
},
{
"id":"11",
"rule_id":"10",
"start_time":"20:00:00",
"end_time":"00:00:00",
"money":"6000",
"update_time":"1512528175"
},
{
"id":"7",
"rule_id":"9",
"start_time":"00:00:00",
"end_time":"12:00:00",
"money":"6000",
"update_time":"1512527986"
},
{
"id":"8",
"rule_id":"9",
"start_time":"12:00:01",
"end_time":"23:59:59",
"money":"6000",
"update_time":"1512527986"
},
{
"id":"5",
"rule_id":"8",
"start_time":"00:00:00",
"end_time":"12:00:00",
"money":"6000",
"update_time":"1511852577"
},
{
"id":"6",
"rule_id":"8",
"start_time":"12:00:01",
"end_time":"23:59:59",
"money":"6000",
"update_time":"1511852577"
}
]
}
]
}
如果是PHP
classrule_nameBase{
....
//加上一條
rule_section=array();
}
//然後把rule_section的單元當做元素壓進去就行了。
⑼ php stdclass 怎麼轉換為數組並遍歷出來
使用mysql_fetch_assoc函數,具體用法可以查看一下手冊,還有幾個相關的函數,我一般是用這個。
$list = array();//聲明一個數組用來存放數據
//$row 為取出來的一行的數據,把他放到$list裡面
//$rs就是你從資料庫查詢出來的資源
while($row = mysql_fetch_assoc($rs)) {
$list[] = $row;
}
⑽ php類中,如何定義一個數組變數。
類中的數組變數定義跟正常的定義一樣
例如:
<?php
class my_class {
public $array = array('a','b');
function my_function(){
return $this->$array;
}
}
$new_class = new my_class;
print_r($new_class->my_function);
?>