PHP split chain and create an array

2

Good I want to extract a string with PHP in separate strings I do not know what method to use, my document .php send me by means of AJAX a string similar to the following:

?i=0&edad=18_28&tarifa=40_80,81_110&servicio=25&ubicacion=1,4

Now more than all I want to extract the strings that are between & and save them in variables or array that is as follows:

id=0
edad=18_28
tarifa=40_48,81_110
servicio=25
ubicacion=1,4

Taking into account that they will not always be all of them sometimes, it may be age and rate but not location, please help

    
asked by Victor Laya 21.10.2018 в 05:51
source

1 answer

2

For that use parse_str you pass the string and an array to store things.

<?php

    $cosas =[];
    $urlString ="?i=0&edad=18_28&tarifa=40_80,81_110&servicio=25&ubicacion=1,4";
    // sacamos el ?
    $urlString = ltrim($urlString,'?');
    // parseamos al array
    parse_str($urlString, $cosas);
    // imprimimos a ver que hay
    var_dump($urlString, $cosas);

result:

string(60) "i=0&edad=18_28&tarifa=40_80,81_110&servicio=25&ubicacion=1,4"
array(5) {
  ["i"]=>
  string(1) "0"
  ["edad"]=>
  string(5) "18_28"
  ["tarifa"]=>
  string(12) "40_80,81_110"
  ["servicio"]=>
  string(2) "25"
  ["ubicacion"]=>
  string(3) "1,4"
}

see online

    
answered by 21.10.2018 / 05:56
source