3

Im studying in array, and I was wondering How can I add key to this kind of array?

{  
   "items":[  
      {  
         "count":"1",
         "id":123,
         "description":"Bag",
         "price":11

      },
      {  
         "count":1,
         "id":1234,
         "description":"10% Discount",
         "price":-1.1

      }
   ],
   "total":9.9,
   "discount_total":9.9
}

because I needed convert the array to have a key base on the id inside the array.

{  
   "items":{  
      "123":{  
         "count":"1",
         "cart_id":123,
         "description":"Bag",
         "price":11
      },
      "1234":{  
         "count":1,
         "cart_id":1234,
         "description":"10% Discount",
         "price":-1.1
      }
   },
   "total":9.9,
   "discount_total":9.9
}

and this is my code

header('Content-Type: application/json');
$cart_array = json_decode('{  
   "items":[  
      {  
         "count":"1",
         "cart_id":123,
         "plu":"TP16",
         "description":"Bag"

      },
      {  
         "count":1,
         "cart_id":1234,
         "plu":"DISCT10",
         "description":"10% Discount"

      }
   ],
   "total":9.9,
   "discount_total":9.9
}');

foreach ($cart_array->items as $item)
{
    $construct["cart_id"] = $item->cart_id;
}

I wanted to ask how can I put the id in the array? I cant use $cart_array['id'] = $value, it returns error.

Uncaught Error: Cannot use object of type stdClass as array

I could really use some explanation here

1
  • @piyush Yes, I look through it and tried to understand it and looks like this is what im looking for. and another thing im missing is making the json_decode true. thanks! Commented Jul 29, 2018 at 5:12

4 Answers 4

2

Following code can help you.

<?php
$json = '{  
   "items":[  
      {  
         "count":"1",
         "cart_id":123,
         "plu":"TP16",
         "description":"Small Four Seasons"

      },
      {  
         "count":1,
         "cart_id":1234,
         "plu":"DISCT10",
         "description":"10% Discount"

      }
   ],
   "total":9.9,
   "discount_total":9.9
}';
$data = json_decode($json,TRUE); //json decode 
foreach ($data['items'] as $key => $value) 
{
    $data['items'][$value['cart_id']] = $value;
    unset($data['items'][$key]);
} 
echo "<pre>";print_r($data);die;
?>
Sign up to request clarification or add additional context in comments.

Comments

1

You don't have to loop at all. You can use array_column to make an array associative with one line of code.

$cart_array['items'] = array_column($cart_array['items'], NULL, 'cart_id');

https://3v4l.org/cPD5n

1 Comment

I changed the correct answer since this also answer my another problem in deconstructing my array (removing the key after some validation) thanks
1

You can use this code in your application to add keys to indexed array.

foreach($obj as $key=>$val){
  foreach ($val as $index=>$content){
    $data[$content['id']]=$content;
  }
}

Comments

1

You can get same output json data as per you want :

$array = json_decode($data,true);
if(isset($array['items']) && count($array['items']) > 0){
    foreach($array['items'] as $key => $value){
        $value['cart_id'] = $value['id'];
         unset($value['id']);
        $array['items'][$value['cart_id']] = $value;
        unset($array['items'][$key]);

                   }
}
echo "<pre>";print_r($array);
echo json_encode($array);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.