W
W
w_b_x2016-09-09 13:00:57
PHP
w_b_x, 2016-09-09 13:00:57

Convert string to array in php?

Good afternoon!
In general, there is the following line:
name=%D0%98%D0%BB%D1%8C%D1%8F;phone=89213251626
You need to get an array of the form in php:
$array["name"] = "%D0%98%D0%BB%D1%8C%D1%8F";
$array["phone"] = "89213251626";
I do it in order to eventually decode the name to the output (url decode) :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita, 2016-09-09
@Rema1ns

$arr = explode(';', 'name=%D0%98%D0%BB%D1%8C%D1%8F;phone=89213251626');
foreach($arr as $item) {
    $tmp  = explode('=', $item);
    $array[$tmp[0]] = $tmp[1];
}

This is the easiest way, you can also break it with regular expressions.
Method 2 is even easier;
first replace all ';' on the &
$arr = null;
$returnValue = parse_str('name=%D0%98%D0%BB%D1%8C%D1%8F&phone=89213251626', $arr);

//результат 
array (
  'name' => 'Илья',
  'phone' => '89213251626',
)

N
Nikolai Konyukhov, 2016-09-09
@heahoh

$result = array();
$stringParams = 'name=%D0%98%D0%BB%D1%8C%D1%8F;phone=89213251626';
$paramsArray = explode(';', $stringParams);
foreach ($paramsArray as $item) {
    list($k, $v) = explode('=', $item);
    $result[$k] = $v;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question