dow46218 2019-05-15 16:15
浏览 73

在SQL循环中如何修改为子数组?

I am trying to add data in from SQL loop into a specific section of an array, however it does't seem to keep the array format when I do a json encode.

I create the array:

$array['type'] = 'update';
$array['tags']['data'] = array(array('name' => "Tag", 'tags' => array()));

Then in my sql loop in the while:

$array['tags']['data']['tags'][] = array('a' => "$row[a]", 'b' => "$row[b]");

after this loop, json encode:

json_encode($array);

In the output, the "tags" has the rows added under data, and the one I setup for the array is now under tags:data:0:tags , and it has not populated this.

Also data has gone from [ ] to { }, and I need it to be [{ }]. If I take out the loop, the data section is with [ { } ] as expected.

Hope that makes sense! if not can expand more.

Many thanks

  • 写回答

1条回答 默认 最新

  • dtukyb8095 2019-05-15 16:39
    关注

    You have an extra array between data and tags

    $array['tags']['data'] = array(array('name' => "Tag", 'tags' => array()));
    print_r($array);
    

    Output

    Array
    (
        [tags] => Array
            (
                [data] => Array
                    (
                        [0] => Array
                            (
                                [name] => Tag
                                [tags] => Array
                                    (
                                    )
    
                            )
    
                    )
    
            )
    
    )
    

    Then you try to access (for assignment)

    $array['tags']['data']['tags'][] = array('a' => "$row[a]", 'b' => "$row[b]");
    

    Which based on your $array should actually be

    $array['tags']['data'][0]['tags'][]
    

    Or something like that...

    Without more to actually go on that's the best I can do, this really doesn't rate a answer, but there was no way to illustrate an array with that much nesting within a comment.

    What you'll wind up with is something like this:

    Array
    (
        [tags] => Array
            (
                [data] => Array
                    (
                        [0] => Array
                            (
                                [name] => Tag
                                [tags] => Array
                                    (
                                    )
    
                            ),
                        [tags] => Array
                           (
                               [0] => Array
                               (
                                  a => "$row[a]",
                                  b => "$row[b]"
                               )
                              [1] => Array(...)
                           )
    
                    )
    
            )
    
    )
    

    Which because of the mixed keys ['data' => [0 => ['name'=>'Tag',...], 'tags' => [...]] Will give you a JSON object at that location {data : {0:{name:"Tag",...},tags:[]}}

    P.S this really annoys me "$row[a]" - I don't know why but it does ... lol

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿