2

I have an array received from android which has to be inserted to mysql table with fields user,product,remark,color,size,count. Im not able to get the values of inner loop data containing size and count.It shows error

$dat='[{"user":10,
        "product":58,
        "remarks":"ok",
        "details":[{
                 "color":"Red",
                 "data":[{
                        "size":12,
                        "count":1
                  },
                  {
                        "size":14,
                        "count":2
                  }]
         }]
    },
    {"user":10,
     "product":60,
     "remarks":"okk",
     "details":[{
              "color":"Black",
              "data":[{
                     "size":10,
                     "count":5
                     },

              ]},{
             "color":"Red",
             "data":[{
                   "size":10,
                   "count":3
                    },

               ]}
      ]}
 ]';

This is the code i tried.

$s = json_decode($dat, true);
print_r($s);


foreach($s as $item)
{
    foreach($item as $details) {
        foreach($details as $data) {

                echo "user".$item['user'];
                echo "product".$item['product'];
                 echo "remarks".$item['remarks'];
                 echo "color".$details['color'];
                echo "size".$data['size'];
                echo "count".$data['count'];


        }
    }
}

This the output of print_r($s)

Array(
        [0] => Array (
                 [user] => 10
                 [product] => 58 
                 [remarks] => ok
                 [details] => Array (
                             [0] => Array(
                                      [color] => Red
                                      [data] => Array ( 
                                                [0] => Array (
                                                    [size] => 12
                                                    [count] => 1
                                                        ) 
                                                [1] => Array (
                                                    [size] => 14 
                                                    [count] => 2 
                                                        ) 
                                                )
                                    ) 
                             ) 
                 ) 
        [1] => Array (
                [user] => 10 
                [product] => 60 
                [remarks] => okk 
                [details] => Array ( 
                                [0] => Array (
                                         [color] => Black 
                                         [data] => Array( 
                                                    [0] => Array ( 
                                                        [size] => 10 
                                                        [count] => 5 
                                                            ) 
                                                                                    ) 
                                         ) 
                                [1] => Array( 
                                        [color] => Red 
                                        [data] => Array ( 
                                                    [0] => Array( 
                                                        [size] => 10 
                                                        [count] => 3 
                                                            ) 
                                                                                    ) 
                                        ) 
                                ) 
                ) 
        ) ;

Pls help me.Thanks in advance

2
  • What is the output of the print_r($s)? Commented Feb 8, 2017 at 15:18
  • 1
    Your JSON data have some issues fix them first, use json.parser.online.fr Commented Feb 8, 2017 at 15:25

3 Answers 3

2
foreach($s as $item)
{
            echo "user".$item['user'];
            echo "product".$item['product'];
            echo "remarks".$item['remarks'];
            foreach($item['details'] as $detail)
            {
                echo "color".$detail['color'];
                foreach($detail['data'] as $data)
                {
                    echo "size".$data['size'];
                    echo "count".$data['count'];
                }
            }
}

And your json get 2 commat , you have to delete them

"details":[{
          "color":"Black",
          "data":[{
                 "size":10,
                 "count":5
                 } <---

          ]},{
         "color":"Red",
         "data":[{
               "size":10,
               "count":3
                } <---

           ]}
Sign up to request clarification or add additional context in comments.

Comments

2

try replacing the following line in MacBooc's answer:

foreach($item['detail'] as $detail)

with this:

foreach($item['details'] as $detail)

2 Comments

@MacBooc Yes..true :)
@MacBooc Thanks :)
1

Look like that you have a multidimensional array ,which make you confuse in inserting the data in the MySQL table.

To over come from this issue you have to make the array to two dimensional array so that you can easily insert the data into DB.

For That You have to use this function to make the array to two dimensional :-

 function array_flatten($array) {
    if (!is_array($array)) {
        return FALSE;
    }
    $result = array();
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $result =$result+$this->array_flatten($value);
        }
        else {
            $result[$key] = $value;
        }
    }

    return $result;
}

Here you have to use only one foreach loop for this

Just like this:-

 $data=['size'=>22,'count'=>3];
    $details=['size'=>23,'color'=>'red','data'=>$data];
    $s []= ['user'=>'10','product'=>58,'details'=>$details];
    $s []= ['user'=>'10','product'=>58,'details'=>$details];
    $data='';
    foreach($s as $item)
    {
     $data[]=$this->array_flatten($item);
    }
    dd($s,$data);

Here I create an array for example at that place you have to give your array to foreach loop.

Now It Give the output as

enter image description here

Now you can easily insert the data in to you DB.

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.