0

i have this kind of string.

[
    {
        "uniqueID" : "com.product",
        "name" : "john doe",
        "price" : "15",
        "description" : "some description"
    },
    {
        "uniqueID" : "com.product1",
        "name" : "john doe",
        "price" : "15",
        "description" : "some descriptio"
    }
]

and i want to change this into array of objects. like

Array
(
 [0] => [
    {
        "uniqueID" : "com.product",
        "name" : "john doe",
        "price" : "15",
        "description" : "some descriptio"
    },

    [1] => 
    {
        "uniqueID" : "com.product1",
        "name" : "john doe",
        "price" : "15",
        "description" : "some description"
    },
]
)

how i can do this, i used this code

  $data = $this->input->post('productArray');
  $dataArray = explode('},' , $data);

but it has give me this kind of solution. which is not as my requirement.

Array
(
    [0] => [
    {
        "uniqueID" : "com.product",
        "name" : "john doe",
        "price" : "15",
        "description" : "some description"

    [1] => 
    {
        "uniqueID" : "com.product1",
        "name" : "john doe",
        "price" : "15",
        "description" : "some description"
    }
]
)

it will be honor for me if you help me in this.

1 Answers1

1

json_decode is the thing you are looking for. It is a built-in PHP function. Here is the sample code listed below using your json string as input:

$string = '[{"uniqueID":"com.product","name":"john doe","price":"15","description":"some description"},{"uniqueID":"com.product1","name":"john doe","price":"15","description":"some descriptio"}]';

$array = json_decode($string);

print_r($array);

//Output///

Array([ 0 ]=>stdClassObject([ uniqueID ]=>com.product[ name ]=>johndoe[ price ]=>15[ description ]=>somedescription)[ 1 ]=>stdClassObject([ uniqueID ]=>com.product1[ name ]=>johndoe[
price ]=>15[ description ]=>somedescriptio))

codeLover
  • 2,571
  • 1
  • 11
  • 27
Osama Ibrahim
  • 148
  • 10