I have arrays that looks like this:
Array ( [multi_YTnsPrfuB832.jpg] => gray [multi_hUORaTux1bI.jpg] => episode [multi_Ijtxz4U0iaq_.jpg] => fidgetq [multi_m0QWCyfVjDKh.jpg] => fidget2 )
the data inside the bracket is a URL and the value is the name. I want to encode this to be a json data to be something like this:
{ "offers":
{
"url":multi_YTnsPrfuB832.jpg,
"name":"gray"
},
{
"url":multi_hUORaTux1bI.jpg,
"name":"episode"
},
{
"url":multi_Ijtxz4U0iaq_.jpg,
"name":"fidgetq"
},
{
"url":multi_m0QWCyfVjDKh.jpg,
"name":"fidget2"
}
}
I'm fairly new to json so if someone has an idea how to implement this using php. Thanks!
3 Answers 3
To reformat your array all you need to do is iterate it and push to a new array in the format you are looking for. The json_encode function will turn an array into a JSON formatted string.
$array = /*your array*/;
$offers = [];
foreach ($array as $key => $value) {
$offers[] = ['url' => $key, 'name' => $value,];
}
$json = json_encode(['offers' => $offers,]);
echo $json;
answered Jun 1, 2017 at 3:04
D Lowther
1,6191 gold badge10 silver badges16 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
RaffyM
wow! this is the solution im looking. youre a life saver sir! thank you! :)
yes, you can using json_encode($myArray)
Comments
answered Jun 1, 2017 at 3:01
Khetesh kumawat
7118 silver badges15 bronze badges
Comments
lang-php