PHP diziye id = 1 & type = 2 gibi bir dize dönüştürmek için hızlı yolu?

4 Cevap php

Ben içine değiştirmek gerekir:

$arr['id']=1;

$arr['type']=2;

4 Cevap

Kullanın: parse_str().

void parse_str(string $str [, array &$arr])  

Bu, geçerli kapsam içinde bir URL ve setleri değişkenler yoluyla geçirilen sorgu dizesi sanki str ayrıştırır.

Örnek:

<?php
    $str = "first=value&arr[]=foo+bar&arr[]=baz";
    parse_str($str);
    echo $first;  // value
    echo $arr[0]; // foo bar
    echo $arr[1]; // baz

    parse_str($str, $output);
    echo $output['first'];  // value
    echo $output['arr'][0]; // foo bar
    echo $output['arr'][1]; // baz

?>

Use parse_str() with the second argument, bu gibi:

$str = 'id=1&type=2';

parse_str($str, $arr);

$arr ardından içerecektir:

Array
(
    [id] => 1
    [type] => 2
)