我遇到了如何在json_encode中迭代数组的问题。
我做了ajax POST,在那里我有一个循环代码,它可以做"数组推送"在jQuery中,类似这样:
$(this).closest('tr.header').nextUntil('.header').each(function(){
i++;
var forms="<form method='POST'>"+$(this).html()+'</form>';
data.push($(forms).serializeArray());
});
所以当我把它传递给我的控制器/其他页面时,我这样做:
$dataList = json_encode($this->input->post("form"));
echo $dataList ;
输出是:
[
[{"name":"category_1", "values":"packages"},
{"name":"PK_1", "values": "1"}
],
[{"name":"category_2", "value":"products"},
{"name":"PK_2", "value": "3"}
]
]
我试过这样做:
foreach ($dataList as $data) {
echo $data . "\n";
}
但只是在foreach上给我错误。
提前致谢。
答案 0 :(得分:1)
对于package pkg is
type output_t is array(0 to 9) of integer; -- Just change size
constant output : output_t; -- Value assign is deferred
end package;
library std;
use std.textio.all;
package body pkg is
-- Entries in output without value in file are assigned to 0
impure function output_init return output_t is
file vec_file: text open read_mode is "mytext";
variable iline: line;
variable data_read: integer;
variable x: integer := 0;
variable res_t : output_t := (others => 0);
begin
while not endfile (vec_file) loop
readline (vec_file, iline);
read(iline,data_read);
res_t(x) := data_read;
x := x + 1;
end loop;
return res_t;
end function;
constant output : output_t := output_init;
end package body;
输出,您需要使用array
这是示例代码。
json_decode()
答案 1 :(得分:1)
只需解码字符串并循环创建的数组。
<?php
$a = '[
[{"name":"category_1", "values":"packages"},
{"name":"PK_1", "values": "1"}
],
[{"name":"category_2", "value":"products"},
{"name":"PK_2", "value": "3"}
]
]';
echo '<pre>';
$res = json_decode($a, true);
$newArr = array();
foreach($res as $data => $val)
{
foreach($val as $k=>$value){
$value=array_values($value);
echo 'Name => ' . $value[0] . ' Value => ' . $value[1] . '<br/>';
}
}
?>