0

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

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;
Sign up to request clarification or add additional context in comments.

1 Comment

wow! this is the solution im looking. youre a life saver sir! thank you! :)
1

yes, you can using json_encode($myArray)

Comments

0

You can use php function like this

echo json_encode($array);

and more click here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.