哪个更好地在PHP中的会话中存储数据? 数组或json

I'm building a shopping cart,and have to store the product list that user has chosen before in SESSION. I wonder which kind of data is better in this situation ?

  1. Storing in JSON: by this way, I have to json_encode and json_decode each the set and get.
  2. Storing in array.

My question is :

  • Thinking about performance, which is better ?

Store the session data as an array. The $_SESSION superglobal is an array anyway so you'll be working with it natively. Not sure why you'd want to use JSON at all for something like this.

However, the third option that Leri mentioned is also a good idea. In the context of an e-commerce system, this will allow you to do better things in the future with the data - you can track uncompleted orders and make contextual recommendations etc.