Jump to content

Exploding random array values and do conditioning?


lilmer

Recommended Posts

How do I put condition upon exploding a variable that outputting different values.

 

I've got a $variable from a loop, each loop the variable has a different content(style attribute)  . 

sample content of the variable. . 

array(6) {
  [0]=>
  string(17) "font-family:Arial"
  [1]=>
  string(14) "font-size:11px"
  [2]=>
  string(13) "color:#000000"
  [3]=>
  string(16) "font-weight:bold"
  [4]=>
  string(25) "text-decoration:underline"
  [5]=>
  string(0) ""
}
array(4) {
  [0]=>
  string(17) "font-family:Arial"
  [1]=>
  string(14) "font-size:11px"
  [2]=>
  string(13) "color:#000000"
  [3]=>
  string(0) ""
}
array(3) {
  [0]=>
  string(15) "padding:1px 2px"
  [1]=>
  string(12) "display:none"
  [2]=>
  string(0) ""
}

array(1) {
  [0]=>
  string(0) ""
}

array(3) {
  [0]=>
  string(15) "padding:1px 2px"
  [1]=>
  string(12) "display:none"
  [2]=>
  string(0) ""
}

so if i'm going to explode it

 $style = explode(';', $variable);
 so to print out put;
  
 echo $style[0];
 right?

But how will I going to be more specifi. Like . .

$style->font-family, or $style->font-weight

 

Then make a condition etc. .  :D

Okay. so I'm just gonna elaborate it. I know my question is really confusing. . 

 

upon exploding the variable.

 

$style = explode(';',$variable);

 

e.g. the $style has a value of 'font-family:arial' , what i want is that the 'font-famly' will become something like (don't know what its called)

$data['font-family'] = 'Arial';

 

is this possible. . ? Absolutely not!!???

OMG. (laughing at myself :) ) . . I don't what to do. . haha  

 

yeah right it's a data. . if I will explode it again . it will become an array so its not going to what I want to happen. Thanks, I think  I'm going to find another solution! again! wews


$variable = array (
"font-family:Arial",
"font-size:11px",
"color:#000000",
"font-weight:bold",
"text-decoration:underline",
""
);

$newvar = array();

foreach ($variable as $style) {
list($att, $value) = explode(':', $style);
if ($att) $newvar[$att] = $value;
}


echo '<pre>',print_r($newvar, true),'</pre>';

/* RESULTS **************
Array
(
[font-family] => Arial
[font-size] => 11px
[color] => #000000
[font-weight] => bold
[text-decoration] => underline
)
*/

Awesomeness!

  $style = explode(';', $content->style);
      foreach($style as $att => $con){          
        @list($att, $value) = explode(':', $con);
         if($att) $newvar[$att] = $value;               
      }
      
       foreach($newvar as $con => $val){
             echo $con.$val;
             
             if($con == 'font-family'){
                 //do what i want
             }
             
            if($con == 'font-size'){
                 //do what i want
             }
         }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.