phparraysforeach

Array to CSS style parameters Conversion


i have an array like

$data = [
'padding-top'=> '15px',
'padding-bottom'=> '15px',
];

How can have generated a string value like

"padding-top:15px; padding-bottom:15px;"

Solution

  • <?php
    
        $data = [
        'padding-top'=> '15px',
        'padding-bottom'=> '15px',
        ];
    
        $string  = '';
        foreach($data  as $key => $value){
            //using php connection add the key and value with string variable
            $string .= $key.':'.$value.';';
        }
    
        echo $string;
    
        ?>