PHP - 将带点键的 json 数组转换为 cakephp 控制器中的 $this->data

发布于 2024-10-24 18:03:05 字数 2020 浏览 1 评论 0原文

我的粒子控制器中有一个 json 数组,在 json_encode, true 之后看起来像这样:

Array
(
    [0] => Array
        (
            [Particle.day_id] => Sat
            [Particle.week_no] => 1
            [Particle.pattern_id] => 589
            [Particle.work] => 0
        )

    [1] => Array
        (
            [Particle.day_id] => Fri
            [Particle.week_no] => 1
            [Particle.pattern_id] => 589
            [Particle.work] => 0
        )

    [2] => Array
        (
            [Particle.day_id] => Thu
            [Particle.week_no] => 1
            [Particle.pattern_id] => 589
            [Particle.work] => 0
        )

    [3] => Array
        (.....

我正在尝试将此数组转换为正确的格式以进行多记录保存:

Array
(
    [Particle] => Array(
            [0] => Array
                (
                            [day_id] => Sat
                            [week_no] => 1
                            [pattern_id] => 589
                            [work] => 0
                        )
            [1] => Array
                (
                            [day_id] => Fri
                            [week_no] => 1
                            [pattern_id] => 589
                            [work] => 0
                        )
                )


                 ....

其中 Particle 是我的 Cakephp 模型名称。

我能得到的最接近的是使用此代码(php):

 $output = array();

        foreach ($jsonData as $keyA => $valueA) {
        foreach ($valueA as $keyB => $valueB) {
            $output = Set::insert( $output, $keyB, $valueB );
        }
        }

其中 $jsonData 是 json_encoded 数组。 这给了我:

Array
(
    [Particle] => Array
        (
            [day_id] => Sun
            [week_no] => 1
            [pattern_id] => 589
            [work] => 1
        )

)

这只是数组的第一部分 - 如何获取数组的其余部分?

我知道答案可能很简单,但它已经让我困惑太久了!

感谢您的任何帮助。

I have a json array in my Particles controller that looks like this after json_encode, true:

Array
(
    [0] => Array
        (
            [Particle.day_id] => Sat
            [Particle.week_no] => 1
            [Particle.pattern_id] => 589
            [Particle.work] => 0
        )

    [1] => Array
        (
            [Particle.day_id] => Fri
            [Particle.week_no] => 1
            [Particle.pattern_id] => 589
            [Particle.work] => 0
        )

    [2] => Array
        (
            [Particle.day_id] => Thu
            [Particle.week_no] => 1
            [Particle.pattern_id] => 589
            [Particle.work] => 0
        )

    [3] => Array
        (.....

I am trying to convert this array to the correct format for a multiple record save:

Array
(
    [Particle] => Array(
            [0] => Array
                (
                            [day_id] => Sat
                            [week_no] => 1
                            [pattern_id] => 589
                            [work] => 0
                        )
            [1] => Array
                (
                            [day_id] => Fri
                            [week_no] => 1
                            [pattern_id] => 589
                            [work] => 0
                        )
                )


                 ....

Where Particle is my Cakephp model name.

The nearest I can get is using this code (php):

 $output = array();

        foreach ($jsonData as $keyA => $valueA) {
        foreach ($valueA as $keyB => $valueB) {
            $output = Set::insert( $output, $keyB, $valueB );
        }
        }

Where $jsonData is the json_encoded array.
This gives me:

Array
(
    [Particle] => Array
        (
            [day_id] => Sun
            [week_no] => 1
            [pattern_id] => 589
            [work] => 1
        )

)

Which is only the first part of the array - how do I get the rest of the array?

I know the answer will probably be straighforward but it has baffled me for too long now!

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情旳空白 2024-10-31 18:03:05
$result = array();
$parentKey = null;
foreach ($jsonData as $keyA => $valueA) {
     foreach ($valueA as $keyB => $valueB) {
        $keyB = explode('.', $keyB);
        list($parentKey, $childKey) = $keyB;
        $output[$childKey] = $valueB;
     }

    if(!isset($result[$parentKey]))
       $result[$parentKey] = array();

   array_push($result[$parentKey], $output);
}


 print_r($result);

您当前的代码是通过写入数组 $output 来实现的。这就是为什么它向您显示数据的最后一部分。

$result = array();
$parentKey = null;
foreach ($jsonData as $keyA => $valueA) {
     foreach ($valueA as $keyB => $valueB) {
        $keyB = explode('.', $keyB);
        list($parentKey, $childKey) = $keyB;
        $output[$childKey] = $valueB;
     }

    if(!isset($result[$parentKey]))
       $result[$parentKey] = array();

   array_push($result[$parentKey], $output);
}


 print_r($result);

Your current code it over writing the array $output. Thats why it is showing you the last part of data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文