PHP - 将带点键的 json 数组转换为 cakephp 控制器中的 $this->data
我的粒子控制器中有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前的代码是通过写入数组 $output 来实现的。这就是为什么它向您显示数据的最后一部分。
Your current code it over writing the array $output. Thats why it is showing you the last part of data.