7

I've been researching different ways to get an array using JInput.

According to http://docs.joomla.org/Retrieving_request_data_using_JInput there are 2 methods that I can use.

  • Method1

    $jinput = JFactory::getApplication()->input;
    $jinput->get('nameOfArray',array(),'ARRAY');
    
  • Method2

    $jinput = JFactory::getApplication()->input;
    $jinput->getArray('nameOfArray',array());
    

Note: nameOfArray is the name of the array I want to access in the $_GET superglobal array.

Method1 works, but Method2 gives the following error:

Catchable fatal error: Argument 1 passed to JInput::getArray() must be of the type array, string given, called in test.php

My question is: What’s the difference between these two methods?

  • The array used in both examples is the same
  • I'm using Joomla 2.5.x
  • The array is a multidimensional array.
5

The first method looks for elements that start with the name that you have specified:

<input type="hidden" name="nameOfArray[test][1]" value="test" />
<input type="hidden" name="nameOfArray[]" value="test" />
<input type="hidden" name="nameOfArray[test][]" value="test" />
<input type="hidden" name="nameOfArray[tester][yes]" value="test" />

All of the above would be returned as part of an array in method one.


Method two does not attempt to load an array by name, instead it accepts an array and tries to find input elements to match the keys. Take the following input:

<input type="hidden" name="object1" value="test1" />
<input type="hidden" name="name2" value="test2" />
<input type="hidden" name="element3" value="test3" />

If you then did the following:

$elements = array('object1'=>'', 'name2'=>'', 'element3'=>'');
$jinput = JFactory::getApplication()->input;
$jinput->getArray($elements); // note that the second parameter is "datasource" not default value

You would then get an array as such in $element:

array('object1'=>'test1', 'name2'=>'test2', 'element3'=>'test3');
  • 1
    Yeap, and the documentation is wrong. I've made a change to it. – Dmitry Rekun Sep 24 '14 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.