Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to serialize array as url queryString in php?

Tags:

php

I need to convert an array such as this:

$arr = array(1, 2, 3) ; 

to this format:

arr[0]=1&arr[1]=2&arr[2]=3

is there any built in function in php or i must create this my self?

like image 928
Navid_pdp11 Avatar asked Apr 29 '26 16:04

Navid_pdp11


1 Answers

expected output should require key name in input data array, see below and after that use http_build_query() function to created query string

<?php 
$arr = array("arr" => array(1, 2, 3)) ; 
echo http_build_query($arr);
?>

encode output default

arr%5B0%5D=1&arr%5B1%5D=2&arr%5B2%5D=3

and if you need decode output then

<?php 
$arr = array("arr" => array(1, 2, 3)) ; 
echo urldecode(http_build_query($arr));
?>

arr[0]=1&arr[1]=2&arr[2]=3

like image 155
Girish Avatar answered May 01 '26 05:05

Girish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!