lilmer Posted March 5, 2013 Share Posted March 5, 2013 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. . 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? 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 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!!??? 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. 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 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 ) */ 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!! 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...
lilmer Posted March 6, 2013 Author 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 } } 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
Archived
This topic is now archived and is closed to further replies.