我在用户提交表单后从Post获取数组。数据以数组的形式返回给我,但是里面有2个以上的数组和一些属于父数组的元素。
如何提取父数组中的数组(基于它们的名称)并将其余元素添加到新数组中。
array (size=x)
'parent_array_element1' => string 'value1'
'parent_array_element2' => string 'value2'
'parent_array_element3' => string 'value3'
'parent_array_element4' => string 'value4'
'parent_array_element5' => string 'value5'
'parent_array_element6' => string 'value6'
'parent_array_element7' => string 'value7'
'parent_array_element8' => string 'value8'
'parent_array_element9' => string 'value9'
'parent_array_element10' => string 'value10'
'internalArray1' =>
array
'internalArray1_element1' => string 'value1'
'internalArray1_element2' => string 'value2'
'internalArray1_element3' => string 'value3'
'internalArray1_element4' => string 'value4'
'internalArray1_element5' => string 'value5'
'internalArray1_element6' => string 'value6'
'internalArray1_element7' => string 'value7'
'internalArray2' =>
array
'internalArray2_element1' => string 'value1'
'internalArray2_element2' => string 'value2'
'internalArray2_element3' => string 'value3'
'internalArray2_element4' => string 'value4'
'internalArray2_element5' => string 'value5'
到目前为止,我要做的是通过在控制器中执行以下操作来获取整个数据:
//Gets the parent array (Including nested arrays)
$data = $this->params()->fromPost();
//Gets the internaArray1
$data = $this->params()->fromPost('internalArray1');
//Gets the internaArray2
$data = $this->params()->fromPost('internalArray2');
但是父数组中有剩余数据,我该如何获取?
答案 0 :(得分:-1)
使用unset
从父级删除嵌套数组
//Gets the parent array (Including nested arrays)
$data = $this->params()->fromPost();
//Gets the internalArray1
$internalArray1 = $data['internalArray1'];
//Remove internalArray1 from parent
unset($data['internalArray1']);
//Gets the internalArray2
$internalArray2 = $data['internalArray2'];
//Remove internalArray2 from parent
unset($data['internalArray2']);