你如何遍历 Input::post() 数据?
How do you iterate through Input::post() data?
根据我的理解 Input::post();
没有参数 returns 包含来自特定 POST..
的所有数据的数组
我正在做这个$all_input = Input::post();
但随后我遍历数组 Java-like(你甚至是这样做的吗?)
for ($i=0; $i<count($all_input); $i++)
{
if (strpos($all_input[$i], 'something') == true) // ERROR...
但应用程序因错误 Undefined offset: 0
而崩溃,我认为这意味着找不到索引?
我也试过添加这个无济于事:
if (!isset($all_input))
{
return;
}
如果是这样,您如何访问数据以循环访问它们?我知道它包含数据,因为如果我删除该代码,在浏览器调试期间按下按钮时我可以看到它们。
如果你还不明白我来自 Java 开发人员,我刚刚开始学习 php 所以请多多包涵。
这行不通,因为您处理的是对象(输入)而不是数组。
我建议使用 foreach 循环代替 for 循环。要验证输入对象的 contents/structure,您还可以执行 dd() 以查看整个输入对象。
基本上,
$input = Input::post();
foreach($input as $i) {
echo $i; //this is a property of the Input Object. if you have arrays or other objects store inside of it, you may need to go deeper into the oject either with a foreach loop again or by calling the property directly ($i->property)
};
据此:https://fuelphp.com/docs/classes/input.html#/method_post Input::post();
will return $_POST
这是一个关联数组。
这是源代码,因为 fuelphp 的文档没有完全涵盖它。
/**
* Fetch an item from the POST array
*
* @param string $index The index key
* @param mixed $default The default value
* @return string|array
*/
public static function post($index = null, $default = null)
{
return (func_num_args() === 0) ? $_POST : \Arr::get($_POST, $index, $default);
}
您需要引用您的输入名称,因此如果您有一个名为 'name' 的输入,那么您需要引用 $all_input['name']
。您可以使用 array_keys()
函数获取密钥。另外,在这种情况下使用 foreach
会更好。喜欢:
foreach($all_input as $key => $value) {
echo 'Input key: ' . $key . ' value: ' . $value;
}
如果你离开 $key =>
你将只得到值,你可以离开它,如果你不在 foreach 中使用它。
如果你不想使用 foreach 原因:
$all_input = Input::post();
$keys = array_keys($all_input);
for ($i = 0; $i < count($keys); $i++) {
if (strpos($all_input[$keys[$i]], 'something') == true) {
// Do your stuff here.
}
}
但如果可能,我仍然推荐使用 foreach,它的开销更小,代码更清晰。
根据我的理解 Input::post();
没有参数 returns 包含来自特定 POST..
我正在做这个$all_input = Input::post();
但随后我遍历数组 Java-like(你甚至是这样做的吗?)
for ($i=0; $i<count($all_input); $i++)
{
if (strpos($all_input[$i], 'something') == true) // ERROR...
但应用程序因错误 Undefined offset: 0
而崩溃,我认为这意味着找不到索引?
我也试过添加这个无济于事:
if (!isset($all_input))
{
return;
}
如果是这样,您如何访问数据以循环访问它们?我知道它包含数据,因为如果我删除该代码,在浏览器调试期间按下按钮时我可以看到它们。
如果你还不明白我来自 Java 开发人员,我刚刚开始学习 php 所以请多多包涵。
这行不通,因为您处理的是对象(输入)而不是数组。
我建议使用 foreach 循环代替 for 循环。要验证输入对象的 contents/structure,您还可以执行 dd() 以查看整个输入对象。
基本上,
$input = Input::post();
foreach($input as $i) {
echo $i; //this is a property of the Input Object. if you have arrays or other objects store inside of it, you may need to go deeper into the oject either with a foreach loop again or by calling the property directly ($i->property)
};
据此:https://fuelphp.com/docs/classes/input.html#/method_post Input::post();
will return $_POST
这是一个关联数组。
这是源代码,因为 fuelphp 的文档没有完全涵盖它。
/**
* Fetch an item from the POST array
*
* @param string $index The index key
* @param mixed $default The default value
* @return string|array
*/
public static function post($index = null, $default = null)
{
return (func_num_args() === 0) ? $_POST : \Arr::get($_POST, $index, $default);
}
您需要引用您的输入名称,因此如果您有一个名为 'name' 的输入,那么您需要引用 $all_input['name']
。您可以使用 array_keys()
函数获取密钥。另外,在这种情况下使用 foreach
会更好。喜欢:
foreach($all_input as $key => $value) {
echo 'Input key: ' . $key . ' value: ' . $value;
}
如果你离开 $key =>
你将只得到值,你可以离开它,如果你不在 foreach 中使用它。
如果你不想使用 foreach 原因:
$all_input = Input::post();
$keys = array_keys($all_input);
for ($i = 0; $i < count($keys); $i++) {
if (strpos($all_input[$keys[$i]], 'something') == true) {
// Do your stuff here.
}
}
但如果可能,我仍然推荐使用 foreach,它的开销更小,代码更清晰。