lilmer Posted March 5, 2013 Share Posted March 5, 2013 (edited) 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. . Edited March 5, 2013 by lilmer Quote Link to comment https://forums.phpfreaks.com/topic/275267-exploding-random-array-values-and-do-conditioning/ Share on other sites More sharing options...
trq Posted March 5, 2013 Share Posted March 5, 2013 What? Quote Link to comment https://forums.phpfreaks.com/topic/275267-exploding-random-array-values-and-do-conditioning/#findComment-1416704 Share on other sites More sharing options...
lilmer Posted March 5, 2013 Author Share Posted March 5, 2013 (edited) 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 March 5, 2013 by lilmer Quote Link to comment https://forums.phpfreaks.com/topic/275267-exploding-random-array-values-and-do-conditioning/#findComment-1416705 Share on other sites More sharing options...
haku Posted March 5, 2013 Share Posted March 5, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275267-exploding-random-array-values-and-do-conditioning/#findComment-1416706 Share on other sites More sharing options...
lilmer Posted March 5, 2013 Author Share Posted March 5, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/275267-exploding-random-array-values-and-do-conditioning/#findComment-1416707 Share on other sites More sharing options...
Barand Posted March 5, 2013 Share Posted March 5, 2013 $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 ) */ Quote Link to comment https://forums.phpfreaks.com/topic/275267-exploding-random-array-values-and-do-conditioning/#findComment-1416798 Share on other sites More sharing options...
lilmer Posted March 6, 2013 Author Share Posted March 6, 2013 Thank you Barand! You Rock!! Quote Link to comment https://forums.phpfreaks.com/topic/275267-exploding-random-array-values-and-do-conditioning/#findComment-1416850 Share on other sites More sharing options...
Solution lilmer Posted March 6, 2013 Author Solution Share Posted March 6, 2013 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/275267-exploding-random-array-values-and-do-conditioning/#findComment-1416857 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.