0

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:

enter image description here

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?

4
  • do not use array_push, use directly as $final_array[$pav['product_attribute_key']['name']] => $pav['name'] Commented Nov 29, 2019 at 11:14
  • What version of Laravel are you using? Commented Nov 29, 2019 at 11:14
  • @Rwd - Laravel v5.8 Commented Nov 29, 2019 at 11:22
  • May be - ->plick('product_attribute_values.name', 'product_attribute_key.name')->all() Commented Nov 29, 2019 at 11:35

3 Answers 3

3

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

Comments

3

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);

2 Comments

@SougataBose I'm not sure it can be as it would trying to access the relationship as a column on the initial table (if I'm not mistaken). The QueryBuilder should ultimately return a collection so it will be possible from that.
I meant to ask this. :) . I am not sure too.
0

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);

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.