Erik van de Ven
Erik van de Ven

Reputation: 4975

Turn string into multidimensional array using regex

I would like to turn the following string into an array:

prijs=0,209&orderby=price&order=undefined&posts_per_page=undefined

INTO

array(
  [prijs] => Array
        (
            [0] => 0
            [1] => 209
        )
  [orderby] => Array
        (
            [0] => price
        )
  [order] => Array
        (
            [0] => undefined
        )  
  [posts_per_page] => Array
        (
            [0] => undefined
        )  
)

Something like that. Is that possible?
Now I'm using some foreach loops, but that's not so fast and ideal like a RegEx.
It's a script which needs to load as fast as possible, so every bit of improvement in the code may help.

The amount of arrays can be variable. But it will always be 2 levels deep, like above. And just one main array.

Upvotes: 1

Views: 248

Answers (5)

CSᵠ
CSᵠ

Reputation: 10169

Looks like you're in need of array_walk as it should be faster than a foreach even if it does pretty much the same thing it does it at a lower level.

as vascowhite suggested you can use $_GET if you have it or parse_str() to get an initial array, after that:

array_walk($array, function(&$n) { 
  $n = explode(',', $n); 
}); 

Live code: http://3v4l.org/YfuKs

Results in exactly what you want (always having arrays instead of CSV strings):

Array
(
    [prijs] => Array
        (
            [0] => 0
            [1] => 209
        )

    [orderby] => Array
        (
            [0] => price
        )

    [order] => Array
        (
            [0] => undefined
        )

    [posts_per_page] => Array
        (
            [0] => undefined
        )

)

PS: instead of explode() you can use preg_split('/,/', $n) since you mentioned RegEx and see which one is faster for you

Upvotes: 2

vascowhite
vascowhite

Reputation: 18440

You don't need REGEX for this. The string you have shown looks like a URL query string, in which case $_GET will already hold the values you need:-

var_dump($_GET);

Should give you:-

array (size=4)
  'prijs' => string '0,209' (length=5)
  'orderby' => string 'price' (length=5)
  'order' => string 'undefined' (length=9)
  'posts_per_page' => string 'undefined' (length=9)

Otherwise you can use parse_string().

$values = array();
parse_str('prijs=0,209&orderby=price&order=undefined&posts_per_page=undefined', $values);
var_dump($values);

Output:

array (size=4)
  'prijs' => string '0,209' (length=5)
  'orderby' => string 'price' (length=5)
  'order' => string 'undefined' (length=9)
  'posts_per_page' => string 'undefined' (length=9)

Upvotes: 3

Programster
Programster

Reputation: 12784

function urlSplitter($input)
{
    $step1Array = explode('&', $input);
    $result = array();

    foreach($step1Array as $element)
    {
        $parts = explode("=", $element);
        $result[$parts[0]] = explode(",", $parts[1]);
    }

    return $result;
}

$result = urlSplitter("prijs=0,209&orderby=price&order=undefined&posts_per_page=undefined");
var_dump($result);

Upvotes: 0

Developerium
Developerium

Reputation: 7265

$str = 'prijs=0,209&orderby=price&order=undefined&posts_per_page=undefined';

$array = preg_split('/[&]/' , $str);

foreach ($array as $a)
{
    $a = preg_split('/[=]/' , $a);
    $a[1] = preg_split('/[,]/' , $a[1]);
}

var_dump($array);

Upvotes: 1

aaron
aaron

Reputation: 687

Well, you can use something like this:

    <?php
    parse_str("prijs=0,209&orderby=price&order=undefined&posts_per_page=undefined",$myArray);
    $myArray['prijs'] = explode(",", $myArray['prijs']);
    $myArray['orderby'] = explode(",", $myArray['orderby']);
    $myArray['order'] = explode(",", $myArray['order']);
    $myArray['posts_per_page'] = explode(",", $myArray['posts_per_page']);
    print_r($myArray);
    ?>

Upvotes: 1

Related Questions