开发者

how to convert string to array in php?

I have this string :

0=&task=rose&duration=1.25&user=15&1=&task=daisy&duration=0.75&user=25&2=&task=orchid&duration=1.15&user=7

I want this array structure :

array(
    array( "task" => "rose"开发者_如何学编程, 
           "duration" => 1.25,
           "user" => 15 
         ),
    array( "task" => "daisy", 
           "duration" => 0.75,
           "user" => 25,
         ),
    array( "task" => "orchid", 
           "duration" => 1.15,
           "user" => 7 
         )
      )


Now parse_url won't work for your case, as it will overwrite the duplicate entries. You have to manually extract and group here.

$str = 'decoded_name=0=&task=rose&duration=1.25&user=15&1=&task=daisy&duration=0.75&user=25&2=&task=orchid&duration=1.15&user=7';
preg_match_all('#(\w+)=([^&=]*)(?:&|$)#', $str, $matches, PREG_SET_ORDER); 

$result = array();
$i = 0;    
foreach ($matches as $m) {
    list(, $key, $value) = $m;

    if (!strlen($value)) { 
       $i = (int)$key;
    }
    else {
       $result[$i][$key] = $value;
    }
}

The trick is watching out for the numeric keys (sans value), which separate your groups. The loop will generate following nested array:

[0] => Array
    (
        [task] => rose
        [duration] => 1.25
        [user] => 15
    )

[1] => Array
    (
        [task] => daisy
        [duration] => 0.75
        [user] => 25
    )

[2] => Array
    (
        [task] => orchid
        [duration] => 1.15
        [user] => 7
    )


you can't achieve what you want to using the headers send as $_GET['user'] would be 7 (the previous ones would be overwritten. You would need a query string like so to go down that route...

decoded_name=0=&task[]=rose&duration[]=1.25&user[]=15&1=&task[]=daisy&duration[]=0.75&user[]=25&2=&task[]=orchid&duration[]=1.15&user[]=7

if you had that as your query string it would a simple case of using array_push otherwise you will have to parse the string manually - again using array_push to generate the sub arrays and then push those into the parent.


I'm happy to help!

<?php

$input = 'data[0][task]=rose&data[0][duration]=1.25&data[0][user]=15&' .
         'data[1][task]=daisy&data[1][duration]=0.75&data[1][user]=25&' .
         'data[2][task]=orchid&data[2][duration]=1.15&data[2][user]=7';

$output = array();

parse_str($input, $output);

print_r($output);

hopeseekr@7250MHz ~ $ php stack_5350749.php
Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [task] => rose
                    [duration] => 1.25
                    [user] => 15
                )

            [1] => Array
                (
                    [task] => daisy
                    [duration] => 0.75
                    [user] => 25
                )

            [2] => Array
                (
                    [task] => orchid
                    [duration] => 1.15
                    [user] => 7
                )

        )

)

Since reparsing your bad input string would be substantially harder than answering your core question, if you want help with that as well, consider opening a new question for it.


Here is a solution without regular expressions. I 'd feel more comfortable with it.

$str = 'decoded_name=0=&task=rose&duration=1.25&user=15&1=&task=daisy&duration=0.75&user=25&2=&task=orchid&duration=1.15&user=7';

// Get rid of "decoded_name", which we don't need
$str = str_replace('decoded_name=', '', $str);

$arr = explode('&', $str);
array_push($arr, '0='); // Add sentinel value to simplify the loop
$results = array();
$parsing = array();

foreach ($arr as $item) {
    // If a new task begins
    if (strspn($item, '0123456789') == strlen($item) - 1) {
        // Add the current item if there is one
        if (!empty($parsing)) {
            $results[] = $parsing;
        }

        // Prepare to parse another item
        $parsing = array();
        continue;
    }

    // Add data to the current item    
    list($key, $value) = explode('=', $item);
    $parsing[$key] = $value;
}

See it in action here.


$str = "decoded_name=0=&task=rose&duration=1.25&user=15&1=&task=daisy&duration=0.75&user=25&2=&task=orchid&duration=1.15&user=7";

$str = preg_replace("/&\d=/", "", $str);
$str = preg_replace("/&/", "=", $str);
$tmp = preg_split("/=+/", $str);

$length = count($tmp);

for ($i = 2, $j = 0; $i < $length; $i+=6, $j++) {
    $arr[$j] = array($tmp[$i] => $tmp[$i + 1], $tmp[$i + 2] => $tmp[$i + 3], $tmp[$i + 4] => $tmp[$i + 5]);
}

print_r($arr);


Try to edit your input / textarea in your form from name="task" into name="data[task]"

then you will see your data in var_dump($_GET)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜