使用foreach循环

时间:2017-02-08 15:14:57

标签: php for-loop multidimensional-array foreach

我有从android收到的数组,必须插入mysql表,其中包含字段user,product,remark,color,size,count。 我无法获得包含大小和计数的内部循环数据的值。它显示错误

$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
                    },

               ]}
      ]}
 ]';

这是我试过的代码。

$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'];


        }
    }
}

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

请帮助我。谢谢你提前

3 个答案:

答案 0 :(得分: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'];
                }
            }
}

你的json得到2个commat,你必须删除它们

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

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

           ]}

答案 1 :(得分:2)

尝试在MacBooc的回答中替换以下行:

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

用这个:

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

答案 2 :(得分:1)

看起来你有一个多维数组,这会让你在MySQL数据表中插入数据时感到困惑。

要完成此问题,您必须将数组设置为二维数组,以便您可以轻松地将数据插入数据库。

For That你必须使用这个函数使数组成二维: -

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

这里你必须只使用一个foreach循环

就像这样: -

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

这里我创建了一个数组,例如在那个地方你必须将你的数组赋予foreach循环。

现在它将输出作为

enter image description here

现在您可以轻松地将数据插入到您的数据库中。