02- object 添加到array

02- object 添加到array

https://rextester.com/BYDSA83238

<?php  
$foo = [];
$t1=(object)[
        'key1' => 'fooValue',
        'key2' => 'fooValue2',
        'key3' => 'fooValue3'
];
array_push($foo,$t1 );
array_push($foo,$t1 );

print_r($foo);
print_r($foo[0]);
print_r($foo[0]->key1);

  foreach ($foo[0] as $name => $value) {
      echo "$name: $value\n";
  }
?>

輸出:

Array
(
[0] => stdClass Object
(
[key1] => fooValue
[key2] => fooValue2
[key3] => fooValue3
)

[1] => stdClass Object
(
[key1] => fooValue
[key2] => fooValue2
[key3] => fooValue3
)

)
stdClass Object
(
[key1] => fooValue
[key2] => fooValue2
[key3] => fooValue3
)
fooValuekey1: fooValue
key2: fooValue2
key3: fooValue3

PHP
01 StringCH02 Array技術文章CodeIgniter 技術文章youtube api 技術文章新浪微博开放平台