ArrayObject::offsetSet


Sets the value at the specified index to newval.



<?php

void 
public ArrayObject::offsetSet mix $index mix $newval )


where,

$index The index to be set

$newval 
The new value for the index

?>

$index


The index being set.



$newval


The new value for the $index.



  1 EXERCISE   

<?php

class Color {
    public 
$propr 'CMYK';
}

$arrcolor = new ArrayObject(new Color());

$arrcolor->offsetSet(1'cyan');
$arrcolor->offsetSet(2'magenta');
$arrcolor->offsetSet(3'yellow');
$arrcolor->offsetSet(4'black');

$arrcolor->offsetSet('RGB', ['red''green''blue']);

echo 
'<pre>';
var_dump($arrcolor);
echo 
'</pre>';

?>

 RESULT   

object(ArrayObject)#1 (1) {
["storage":"ArrayObject":private]=>
object(Color)#2 (6) {
["propr"]=>
string(4) "CMYK
[1]=>
string(4) "cyan"
[2]=>
string(7) "magenta"
[3]=>
string(6) "yellow"
[4]=>
string(5) "black"
["RGB"]=>
array(3) {
[0]=>
string(3) "red"
[1]=>
string(5) "green"
[2]=>
string(4) "blue"
}
}
}


  2 EXERCISE   

<?php

$arrcolor 
= [ [ 'CMYB' => 'cyan''magenta''yellow''black'], 'RGB'];

$arrcolor = new ArrayObject($arrcolor);

$arrcolor->offsetSet(1, ['RGB' => ['red''green''blue']]);

echo 
'<pre>';
var_dump($arrcolor);
echo 
'</pre>';

?>

 RESULT   

object(ArrayObject)#1 (1) {
["storage":"ArrayObject":private]=>
array(2) {
[0]=>
array(4) {
["CMYB"]=>
string(4) "cyan"
[0]=>
string(7) "magenta"
[1]=>
string(6) "yellow"
[2]=>
string(5) "black"
}
[1]=>
array(1) {
["RGB"]=>
array(3) {
[0]=>
string(3) "red"
[1]=>
string(5) "green"
[2]=>
string(4) "blue"
}
}
}
}