我只使用基础数组,我有一个简单的数组,我从ajax json调用传递到我的php类
"item": [ "4","3","6","5","7"]
我正在尝试进入这种数组格式,其中id是上面的数组,顺序从0开始并计数++ 1
我在数组中尝试了一个foreach循环,但我知道这不是这样做的方法。
$data = array(
array('Page' => array('id' => 3, 'order' => 0)),
array('Page' => array('id' => 4, 'order' => 1)),
array('Page' => array('id' => 5, 'order' => 2)),
array('Page' => array('id' => 6, 'order' => 3)),
array('Page' => array('id' => 7, 'order' => 4)),
);
我试过了:
$data = array(
foreach($this->request->data as $key => $value) {
array('Page' => array('id' => $key, 'order' => 0)),
};
);
答案 0 :(得分:1)
你不能在这样的数组中运行foreach。试试这个:
$data = array();
foreach($this->request->data['item'] as $key => $value) {
$data[] = array('Page' => array('id' => $value, 'order' => $key));
}
在猜测您的格式时,我认为您可能希望在$value
中使用$key
代替'id' => $key
$data = array();
$raw = $this->request->data;
$array = $raw['item'];
$i = 0;
foreach($array as $val) {
$data[] = array('Page' => array('id' => $val, 'order' => $i));
$i++;
}