我有下一个数组:
$array = array
(
'body' => 'array ("color" => "blue", "font-weight" => "bold")',
'div' => 'array ("color" => "red", "background" => "blue")'
);
例如,我想将body
的"color" => "blue"
编辑为"color" => "black"
。
从这些数组中提取数据并打印出来很容易:
foreach ($array as $element_name => $element_style){
echo $array_part_name . '{';
foreach ($array_part_style as $property_name => $property){
echo $property_name . ': ' . $property . '; '
}
echo '}'
}
我想它的结果应该和css一样,但我该如何编辑特定的部分?(如上所述)
Umm,您希望您的子数组是字符串吗?因为如果你想要一个多维数组,你可以这样做:
$array = array
(
'body' => array("color" => "blue", "font-weight" => "bold"),
'div' => array ("color" => "red", "background" => "blue")
);
然后你可以像这样改变身体颜色:
$array['body']['color'] = 'black';
如果你想循环浏览所有的项目和子项目,你可以这样做:
foreach ($array as $selector => $properties) {
echo $selector.' {';
foreach ($properties as $key => $value) {
echo $key.': '.$value.';';
}
echo '}';
}