1

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.

4
  • 1
    since you're worried about performance you should add your current code so you wouldn't get the same one from others. also add variations of the string you might receive Commented Oct 5, 2013 at 12:25
  • I cannot post my complete code, cause that's more then 300 rows :) It's a filter module, which uses too many foreach loops to get the desired results... That's why it's too slow and I'm looking for solutions to replace as much loops as possible with other code.. Commented Oct 5, 2013 at 12:37
  • in that case it sounds like the drag might also be from somewhere else, but you can post the relevant parts anyway (and you should) Commented Oct 5, 2013 at 12:47
  • I know, but every little improvement can help. And this the part where I got stuck, so that's why I'm asking it here on stackoverflow. Commented Oct 10, 2013 at 17:58

5 Answers 5

3

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your comment! Didn't think about that! But at the moment I'm using AJAX and send the string using POST to AJAX page. At the moment we're rebuilding it to static PHP, so I'll keep this in mind!
This looks like a nice solution apart from the fact that it doesn't split the elements into the array subsets. i.e. prijs = array(0, 209)
Unfortunately not. But neither does the accepted answer 3v4l.org/89v3g. Mine gets you much closer to the array you are looking for 3v4l.org/LYC9b
2

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

2 Comments

I was intrigued by array_walk, as I have never used it before and love using closures. Unfortunately I found it to be SLOWER than using a foreach with referenced value. My test case was multiplying 10,000 elements in an array by 2. This occured for both a normal array and a 'map'
testing is the way to go, gave you another option without knowing your code anyway. BTW: did you check with preg_split() also? because explode() is known to be slow...
1
$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);

4 Comments

Even though I was looking for a solution without foreaches, It is an improvement cause your solution just uses 1 foreach, then my earlier solution with 2 foreaches. So thank you :)
if worried about performance you should also note the 2 function calls inside the foreach
why did you reject my answer?
vascowhite: probably because I changed some values, before I implemented it in my own system. 3v4l.org/vhUTR tinyByte: my mistake, clicked the wrong button
1

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);
    ?>

3 Comments

That's not the way to do it because we can have order, orderby, but also branches and such...
@ErikVandeVen: As per your question, i have given you the answer. you should have mentioned it, if you had to consider other options as well.
@ErikVandeVen: Please edit the question and include your exact requirements and post your attempted code. It'll help us answer your question better.
0
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);

4 Comments

Thank you for your answer, but as I wrote in my first post I'm looking for a way to use less foreaches then I'm already using :)
good point, but is there any particular reason why you don't like foreach? Is it to keep the number of lines to a minimum?
may be because loops are slow when handling big data
Exactly CSᵠ, that's the reason.

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.