explode() into $key=>$value pair

后端 未结 15 619
广开言路
广开言路 2020-12-01 09:24

I have this:

$strVar = \"key value\";

And I want to get it in this:

array(\'key\'=>\'value\')

I tried

相关标签:
15条回答
  • 2020-12-01 09:49

    If you have more values in a string, you can use array_walk() to create an new array instead of looping with foreach() or for(). I'm using an anonymous function which only works with PHP 5.3+.

    // your string
    $strVar = "key1 value1&key2 value2&key3 value3";
    
    // new variable for output
    $result = array();
    
    // walk trough array, add results to $result
    array_walk(explode('&', $strVar), function (&$value,$key) use (&$result) {
        list($k, $v) = explode(' ', $value);
        $result[$k] = $v;
    });
    
    // see output
    print_r($result);
    

    This gives:

    Array
    (
        [key1] => value1
        [key2] => value2
        [key3] => value3
    )
    
    0 讨论(0)
  • 2020-12-01 09:50
    $pairs = explode(...);
    $array = array();
    foreach ($pair in $pairs)
    {
        $temp = explode(" ", $pair);
        $array[$temp[0]] = $temp[1];
    }
    

    But it seems obvious providing you seem to know arrays and explode. So there might be some constrains that you have not given us. You might update your question to explain.

    0 讨论(0)
  • 2020-12-01 09:50
    list($array["min"], $array["max"]) = explode(" ", "key value");
    
    0 讨论(0)
  • 2020-12-01 09:51

    I am building an application where some informations are stored in a key/value string.

    tic/4/tac/5/toe/6

    Looking for some nice solutions to extract data from those strings I happened here and after a while I got this solution:

    $tokens = explode('/', $token);
    
    if (count($tokens) >= 2) {
        list($name, $val) = $tokens;
        $props[$name] = $val;
    }
    
    if (count($tokens) >= 4) {
        list(, , $name, $val) = $tokens;
        $props[$name] = $val;
    }
    
    if (count($tokens) >= 6) {
        list(, , , , $name, $val) = $tokens;
        $props[$name] = $val;
    }
    
    // ... and so on
    

    freely inspired by smassey's solution

    This snippet will produce the following kinds of arrays:

    $props = [
        'tic' => 4,
    ];
    

    or

    $props = [
        'tic' => 4,
        'tac' => 5,
    ];
    

    or

    $props = [
        'tic' => 4,
        'tac' => 5,
        'toe' => 6,
    ];
    

    my two cents

    0 讨论(0)
  • 2020-12-01 09:52
    $my_string = "key0:value0,key1:value1,key2:value2";
    
    $convert_to_array = explode(',', $my_string);
    
    for($i=0; $i < count($convert_to_array ); $i++){
        $key_value = explode(':', $convert_to_array [$i]);
        $end_array[$key_value [0]] = $key_value [1];
    }
    

    Outputs array

    $end_array(
                [key0] => value0,
                [key1] => value1,
                [key2] => value2
                )
    
    0 讨论(0)
  • 2020-12-01 09:52

    Another single line:

    parse_str(str_replace(' ', '=', $strVar), $array);
    
    0 讨论(0)
提交回复
热议问题