我有一个数组:
array(1) {
[14]=>
array(1) {
[976]=>
array(1) {
[79833]=>
array(1) {
["id"]=>
string(2) "99"
}
}
}
}
这是一个html表单:
<form method="post">
<input type="text" name="mark[14][976][79833][id]" >
<input type="submit">
</form>
我需要帮助才能从此输入表单中导出所有数据;
例如:
$input="mark";
$first="14";
$second="976";
$Third="79833";
$Fourth="id";
$last_posted_typed_data="99"; (for example)
我该怎么做? THX!
答案 0 :(得分:1)
http://php.net/manual/ru/function.array-values.php
function array_values_recursive($array) {
$flat = array();
foreach($array as $value) {
if (is_array($value)) {
$flat = array_merge($flat, array_values_recursive($value));
}
else {
$flat[] = $value;
}
}
return $flat;
}
答案 1 :(得分:0)
我不确定具体细节,但您可以使用递归函数来处理输入数据,当您不了解嵌套实体的数量时,它会有所帮助。所有键和值都将保存到$ results数组。
<?php
function processData($data, &$results) {
if (is_array($data)) {
foreach ($data as $key => $value) {
$results[] = $key;
processData($value, $results);
}
} else {
$results['formValue'] = $data;
}
}
$results = [];
processData($_POST, $results);
答案 2 :(得分:0)
$arr = [14 => [
976 => [79833 => ['id' => 99]]
]];
$keys = [];
$data = "";
$it = new RecursiveArrayIterator($arr); // iterator
foreach (new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST) as $k => $v) {
$keys[] = $k;
if ($k == 'id') $data = $v;
}
list($first, $second, $third, $fourth) = $keys;
var_dump($first, $second, $third, $fourth, $data);
输出:
int(14)
int(976)
int(79833)
string(2) "id"
int(99)
答案 3 :(得分:-1)
不确定导出的确切含义,但是现在你编写它的方式我认为获得它的方法是使用多级嵌套循环。
foreach ($_POST as $input => $firsts) {
foreach ($firsts as $first => $seconds) {
foreach ($seconds as $second => $thirds) {
foreach ($thirds as $third => $fourths) {
foreach ($fourths as $fourth => $last_posted_typed_data) {
// do stuff with $input, $first, $second, $third,
// $fourth, $last_posted_typed_data
}
}
}
}
}
有点麻烦,但不知道这是什么以及你用它做了什么,我不确定如何建议更好的东西。