Alex Vasile Alex Vasile - 1 year ago 50
PHP Question

modify an array inside another array

I have the next array:

$array = array
'body' => 'array ("color" => "blue", "font-weight" => "bold")',
'div' => 'array ("color" => "red", "background" => "blue")'

And i want, for example to edit the
"color" => "blue"
"color" => "black"

As extracting the data from those arrays and printing it out was easy:

foreach ($array as $element_name => $element_style){
echo $array_part_name . '{';
foreach ($array_part_style as $property_name => $property){
echo $property_name . ': ' . $property . '; '
echo '}'

And I guess it should result just like css, but how do i edit that specific part? (mentioned above)

Answer Source

Umm, do you want your sub arrays to be strings? Because if you want a multidimensional array you would do this:

$array = array
    'body' => array("color" => "blue", "font-weight" => "bold"),
    'div' => array ("color" => "red", "background" => "blue")

And then you can change the body color like this:

$array['body']['color'] = 'black';

And if you want to loop through all the items and subitems you will do it like this:

foreach ($array as $selector => $properties) {

    echo $selector.' {';

    foreach ($properties as $key => $value) {
        echo $key.': '.$value.';';

    echo '}';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download