How to access an array in $_GET using JInput?

by TryHarder   Last Updated October 02, 2019 21:10 PM - source

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

According to there are 2 methods that I can use.

  • Method1

    $jinput = JFactory::getApplication()->input;
  • Method2

    $jinput = JFactory::getApplication()->input;

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.

Answers 1

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');
David Fritsch
David Fritsch
September 24, 2014 06:21 AM

Related Questions

Using JInput instead of $_POST

Updated June 02, 2015 22:04 PM

Can I find out all of the arrays on my page?

Updated January 19, 2018 18:10 PM

Joomla Array Database

Updated July 30, 2015 14:04 PM

SP Page Builder get value from select option

Updated November 08, 2017 13:10 PM

In custom fields, array_values not working

Updated October 01, 2018 23:10 PM