Jaroslav Klimčík
Jaroslav Klimčík

Reputation: 4808

Parse or implode simple text in array

I have a simple array:

array(price => "1737 EUR - 3447 EUR")

And I need result just like this:

$price1 = 1737

$price = 3447

I know that is very simple question, but I don't know which function to choose and how to do it?

Upvotes: 0

Views: 85

Answers (3)

kennypu
kennypu

Reputation: 6051

not too pretty, but I would do something like this:

//the initial array
$price = array("price" => "1737 EUR - 3447 EUR");

$prices = explode("-",$price["price"]);
foreach($prices as &$price) $price = (int) $price;
print_r($prices); //Array ( [0] => 1737 [1] => 3447 )

Upvotes: 0

aphex
aphex

Reputation: 9

Or try this if you don't want to use regex;

$priceArray=array(price => "1737 EUR - 3447 EUR");
$spiltPrices=explode(' - ',$priceArray['price']);
echo $price1=$spiltPrices[0];
echo $price=$spiltPrices[1];

Upvotes: 0

Prasanth Bendra
Prasanth Bendra

Reputation: 32750

Try this :

<?php
 $arr   = array("price" => "1737 EUR - 3447 EUR");
 preg_match('/(?P<price1>\d+)\s*EUR\s*-\s*(?P<price>\d+)\s*EUR/',$arr["price"],$matches);
 echo "<pre>";
 print_r($matches);
?>

Upvotes: 4

Related Questions