ArrayObject::append
Appends a given value as the last ARRAY element.
<?php
void public ArrayObject::append ( mix $value )
where,
$value = The value to be appended
?>
$value
The value being appended.
ATTENTION
This method cannot be called when the ArrayObject was constructed from an object.
We must use the ArrayObject::offsetSet instead.
EXERCISE
<?php
$arr01a = ['red', 'green'];
$val01a = 'blue';
$arr01b = ['cyan', 'yellow', 'magenta', 'black'];
$arr01 = new ArrayObject($arr01a);
$arr01->append($val01a);
$arr01->append($arr01b);
echo '<pre>';
print_r($arr01);
echo '</pre>';
?>
RESULT
ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[0] => red
[1] => green
[2] => blue
[3] => Array
(
[0] => cyan
[1] => yellow
[2] => magenta
[3] => black
)
)
)
EXERCISE
<?php
$arr01a = ['K' => 2, 'L' => 8];
$arr01b = ['M' => 18];
$arr01c = ['N' => 32, 'O' => 32, 'P' => 18, 'Q' => 8];
$arr01 = new ArrayObject($arr01a);
$arr01->append($arr01b);
$arr01->append($arr01c);
foreach($arr01 as $arr)
{
var_dump($arr);
echo '<br>';
}
?>
RESULT
int(2)
int(8)
array(1) { ["M"]=> int(18) }
array(4) { ["N"]=> int(32) ["O"]=> int(32) ["P"]=> int(18) ["Q"]=> int(8) }
EXERCISE
<?php
$RGB = [ 'R' => 'red',
'G' => 'green',
'B' => 'blue' ];
$CMYK = [ 'C' => 'cyan',
'M' => 'magenta',
'Y' => 'yellow',
'K' => 'black' ];
$Object = new ArrayObject($RGB);
var_dump($RGB);
echo '<br><br>';
var_dump($CMYK);
echo '<br><br>';
$Object->append($CMYK);
var_dump($Object);
?>
RESULT
$RGB
array(3) { ["R"]=> string(3) "red" ["G"]=> string(5) "green" ["B"]=> string(4) "blue" }
$CYYK
array(4) { ["C"]=> string(4) "cyan" ["M"]=> string(7) "magenta" ["Y"]=> string(6) "yellow" ["K"]=> string(5) "black" }
After ArrayObject::append
object(ArrayObject)#1 (1) { ["storage":"ArrayObject":private]=> array(4) { ["R"]=> string(3) "red" ["G"]=> string(5) "green" ["B"]=> string(4) "blue" [0]=> array(4) { ["C"]=> string(4) "cyan" ["M"]=> string(7) "magenta" ["Y"]=> string(6) "yellow" ["K"]=> string(5) "black" } } }