Exoon
Exoon

Reputation: 1553

Split into multiple items by splitting at ;

I have some items in a database like

Apple Juice;Orange Juice;Pineapple Juice;Cranberry Juice;Milk;

How can i split them so each one is in a PHP array

Upvotes: 0

Views: 47

Answers (3)

Fluffeh
Fluffeh

Reputation: 33522

It's a really basic explode function that you are looking for:

$yourString='Apple Juice;Orange Juice;Pineapple Juice;Cranberry Juice;Milk;';

$yourArray=explode(';',$yourString);

print_r($yourArray);

Edit:

To remove empty array fields, we can simply use the array filter function like this:

$yourString='Apple Juice;Orange Juice;Pineapple Juice;Cranberry Juice;Milk;';

$yourArray=array_filter(explode(';',$yourString));

print_r($yourArray);

Upvotes: 3

learn
learn

Reputation: 300

You could do something like this:

$items = explode(";", "Apple Juice;Orange Juice;Pineapple Juice;Cranberry Juice;Milk");

<pre>
print_r($items)
</pre>

Upvotes: 0

user1864610
user1864610

Reputation:

$array = explode(";", "Apple Juice;Orange Juice;Pineapple Juice;Cranberry Juice;Milk");

Upvotes: 2

Related Questions