PHP Associative Array to JSON

PHP Associative Array to JSON

Related searches

I need to have data output in a very specific format to be used with something.

$product = Product::with('product_attribute_values', 'product_attribute_values.product_attribute_key')->find(2);

$product_decoded = json_decode($product, true);

I need to extract product attribute values into a specific format and it currently looks like:

I wish for it be like:

{
    "Material":"Plastic", 
    "Printing Method":"Pad", 
    "Ink Colour":"Black", 
    "Barrel Colour":"Silver", 
    "Grip Colour":"Black"
}

I have attempted this:

$final_array = array();

foreach($product_decoded['product_attribute_values'] as $pav) {
     $array = [
          $pav['product_attribute_key']['name'] => $pav['name']
     ];

     array_push($final_array, $array);
}

return json_encode($final_array);

This results in data looking like:

[
    {"Material":"Plastic"},
    {"Printing Method":"Pad"},
    {"Ink Colour":"Black"},
    {"Barrel Colour":"Silver"},
    {"Grip Colour":"Black"}
]

How would this be achieved?


You can do it like this:

foreach($product_decoded['product_attribute_values'] as $pav) {
     $array[$pav['product_attribute_key']['name']] = $pav['name'];
}

return json_encode($array);

Associative array to Json, You can also loop through the values with a foreach() loop: JSON_Encode - PHP variables to JSON. json_enode () is a native PHP function that allows you to convert PHP data into the JSON format. json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] ) : string. The function takes in a PHP object ($value) and returns a JSON string (or False if the operation fails).


For something like this you could use collections:

return collect($product_decoded['product_attribute_values'])
    ->pluck('name', 'product_attribute_key.name')
    ->toJson();

Alternatively, you could use the array helpers:

$finalArray = Arr::pluck($product_decoded['product_attribute_values'],'name', 'product_attribute_key.name' );

return json_encode($finalArray);

json_encode - Manual, Associative array to Json [closed] � php json array-push. It's difficult to tell what is being asked� By Krunal Last updated Dec 11, 2019 PHP json_encode () is an inbuilt function that converts PHP array to json. The json_encode () function returns the string containing a JSON equivalent of the value passed to it as we demonstrate here with the numerically indexed array. We can also convert any JSON received from a server into JavaScript objects.


You need to have it in an object instead of an array.

$item = array("Material"=>"Plastic", "Printing Method"=>"Pad", "Ink Colour"=>"Black", "Barrel Colour"=>"Silver", "Grip Colour"=>"Black");

echo json_encode($item);

will result in this JSON:

[
    {"Material":"Plastic"},
    {"Printing Method":"Pad"},
    {"Ink Colour":"Black"},
    {"Barrel Colour":"Silver"},
    {"Grip Colour":"Black"}
]

Which is correct JSON array syntax.

In order to get

{
    "Material":"Plastic", 
    "Printing Method":"Pad", 
    "Ink Colour":"Black", 
    "Barrel Colour":"Silver", 
    "Grip Colour":"Black"
}

You would need soemthing like

$item->Material = "Plastic";
$item->PrintingMethod = "Pad";
$item->InkColour = "Black";
$item->Barrel Colour = "Silver";
$item->GripColour = "Black;

json_encode($item);

Pass PHP Arrays to JSON and JavaScript Using json_encode, Return Values �. Returns a JSON encoded string on success or FALSE on failure . echo "Associative array always output as object: ", json_encode($d,� Pass PHP Arrays to JSON and JS with json_encode The PHP json_encode function translates the data passed to it to a JSON string which can then be output to a JavaScript variable. We demonstrate on this page with single level arrays. Other pages demonstrate using json_encode with multi-dimensional arrays and scalar values.


How To Convert PHP Array To JSON Example, How to pass arrays from PHP to JSON and JavaScript using json_encode. Demonstrates with single-level numerically indexed and associative arrays. The json_decode () function is used to decode a JSON object into a PHP object or an associative array. The json_decode () function returns an object by default. The json_decode () function has a second parameter, and when set to true, JSON objects are decoded into associative arrays.


PHP json_encode() is an inbuilt function that converts PHP array to json. The json_encode() function returns the string containing a JSON equivalent of the value passed to it as we demonstrate here with the numerically indexed array. We can also convert any JSON received from a server into JavaScript objects. PHP JSON. json_decode() json_encode() PHP Libxml. PHP Associative Arrays. Associative arrays are arrays that use named keys that you assign to them.


A note of caution: If you are wondering why json_encode() encodes your PHP array as a JSON object instead of a JSON array, you might want to double check your array keys because json_encode() assumes that you array is an object if your keys are not sequential.