Jump to content

Exploding random array values and do conditioning?


lilmer
Go to solution Solved by 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

Edited by lilmer
Link to comment
Share on other sites

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!!???

Edited by lilmer
Link to comment
Share on other sites

You showed us some data. No $variable in it, but lets assume that the data you are showing us was a dump of $variable.

 

That means $variable is an array. explode() works on strings. So something doesn't match up here.

Link to comment
Share on other sites

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

Link to comment
Share on other sites


$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
)
*/
Link to comment
Share on other sites

  • Solution

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
             }
         }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.