0

To my understanding Input::post(); with no parameters returns an array containing all the data from a specific POST..

I am doing this $all_input = Input::post();

But then I am iterating through the array Java-like (is that how you even do it?)

for ($i=0; $i<count($all_input); $i++)
    { 
        if (strpos($all_input[$i], 'something') == true) // ERROR...

but the application crashes with error Undefined offset: 0 which I believe means that the index was not found?

I have also tried adding this to no avail:

    if (!isset($all_input))
    {
        return;
    }

If so how can you access the data to iterate through them? I know it contains data cause I can see them when I press the button during the debugging on the browser if I remove that code.

If you didn't already figure it out I am coming from Java developer and I have just started learning php so bear with me please.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Theocharis K.
  • 1,281
  • 1
  • 16
  • 43
  • 1
    Your `$all_input` is probably an associative array, and you trying to access 0 index of it in if statement, which probably doesn't exist. `var_dump($all_input)` - to check what data you have there – ArtOsi Sep 27 '17 at 14:32

2 Answers2

1

According to this: https://fuelphp.com/docs/classes/input.html#/method_post Input::post(); will return $_POST which is an assoc array. Here is the source code, because the fuelphp's documentation doesn't cover it exactly.

/**
 * 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);
}

You need to refer with your input names, so if you have an input which you call 'name', in that case you need to refer $all_input['name']. You can get your keys by using array_keys() function. Also it's better if you use foreach in this situation. Like:

foreach($all_input as $key => $value) {
    echo 'Input key: ' . $key . ' value: ' . $value;
}

If you left the $key => you will get only the value, you can left it, if you doesn't use it inside the foreach.

If you don't want to use foreach somewhy:

$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.
    }
}

But I still recommend foreach if possible, it's less overhead and cleaner code.

golddragon007
  • 1,247
  • 14
  • 27
  • That's the first time I hear about assoc arrays. Interesting. If I know the parameters to pass into let's say `Input::post('key')` but the $_POST returns many variables will they all be stored into `array['key'][0] = $value` etc? – Theocharis K. Sep 28 '17 at 08:47
  • Why in the world are you quoting the CodeIgniter docs if OP tagged the question with FuelPHP? The syntax is exactly the same as FuelPHP, yes - and so is the principles behind it - but that's just going to be confusing to a beginner like OP. – patricksweeney Sep 29 '17 at 00:56
  • @patricksweeney yeah... that's true, so I included the fuelphp's and source code, because it doesn't say exactly the documentation this... – golddragon007 Sep 29 '17 at 08:59
  • @TheocharisK. yes, but you need to name the input fields in a specific way, here's an example in pure php https://stackoverflow.com/questions/2433727/submitting-a-multidimensional-array-via-post-with-php in your case you can refer for this like `Input::post('diameters')` than you get an array which is a two dimensional array, first a list and inside it there's an assoc array. Also assoc array is some kind of (but not exactly the same) like `HashMap` from Java or a `Dictionary` in c#. – golddragon007 Sep 29 '17 at 09:10
  • @TheocharisK. if you name 'normally' so let say, you have two field with these names: field1 and field2 than you can refer like: `Input::post('field1')` and `Input::post('field2')` and in this case you will get back only the field1 or field2 value, nothing else. If you refer like this: `Input::post()` you will get back an assoc array: `['field1' => 'some value', 'field2' => 'some value 2']` But let me know if I misunderstood your question. – golddragon007 Sep 29 '17 at 09:22
0

This won't work because you are dealing with an Object (Input) not an array.

I recommend using a foreach loop vice a for loop. To verify the contents/structure of the Input Object you can also do a dd() to see the Input Object in its entirety.

Basically,

$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)

};
Luke
  • 943
  • 6
  • 12