0

I have this options array:

string(111) "colors:black;white;transparent;pink;"    

how can I explode it so I get as a label first separator : and the rest in array as options? I have this so far:

$options= explode(";",$rows[0]);
$htm= '<ul class="product_opts">'.$lang['available_options'].'';
foreach ($options as $value) { 
        $row=mysql_fetch_row($result);
            $htm.='<li style="text-align:left;font-weight:normal;">'.$value.'</li>';
            }
        $htm.= '</ul>';
        }
    echo $htm;    

but it returns the label as option too..

5 Answers 5

2
$attributes="colors:black;white;transparent;pink;";

list($attribute, $values) = array_map(
    function($value) {
        return array_filter(explode(';', $value));
    },
    explode(':', $attributes)
);
var_dump($attribute);
var_dump($values);
Sign up to request clarification or add additional context in comments.

Comments

1
$str = "colors:black;white;transparent;pink;";

list($label, $optionsStr) = explode(":", $str);

$optionsStr = rtrim($optionsStr, ";");
$options = explode(";", $optionsStr);

echo "<pre>";
print_r($label);

print_r($options);

Comments

1
  1. Explode string by :. First token will be your label, second token your options.
  2. Explode second token by ;, and you have your options in an array.

Comments

1

Try

$str = "colors:black;white;transparent;pink;";
$arr = explode(";",$str);
$key = explode(":",$arr[0]);
$arr[0] = $key[1];
unset($arr[sizeof($arr)-1]);

See demo here

Comments

0

use split to do like so

$options= split("[;|:]",$rows[0]);

1 Comment

Note: This function (split) has been DEPRECATED as of PHP 5.3.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.