0

I am trying to define a 2d array in php. I have some concept code so you can see the situation:

    class Testing { 
        protected $requiredFieldsByReferenceType = array(
           ['Book']['volume'] => true,
           ['Book']['source'] => true,
           ['Book Section']['volume'] => true,
           ['Book Section']['source'] => true,
           ['Chart or Table']['volume'] => true,
           ['Chart or Table']['source'] => true
        );
        print_r($requiredFieldsByReferenceType);
     }//End Testing

The error that is thrown:

Parse error: syntax error, unexpected '[', expecting ')'

flag
I kept reading the title as "deceleration". I think the misplaced E (it's "declAration") threw me off. – Frank Farmer Jun 5 '09 at 2:42
@Frank , I edited the question's answer to be correct :) – alex Jun 5 '09 at 2:55

11 Answers

5

The other answers are good.
The syntax using array() is:

$requiredFieldsByReferenceType = array('Book'=>array('volume' => true,
                                                     'source' => true),
                                       'Book Section'=>array('volume' => true,
                                                             'source' => true)
                                       );
link|flag
4

You have to use array() inside the array value declarations too:

protected $myArray = array(
    "Book" => array(
        "item1" => true,
        "item2" => true
    ),
    "Chest" => array(
        "item1" => true,
        "item2" => false
    )
);
link|flag
How would I access it then? The normal way? $myArray['Book']['item1'] = false? – Diego Jun 6 '09 at 16:11
@Diego: Yes. After you’ve build that array, you can access it via the $variable[…] syntax. – Gumbo Jun 6 '09 at 16:27
1

Try this way:

$requiredFieldsByReferenceType = array();
$requiredFieldsByReferenceType['Book']['volume'] = true;
$requiredFieldsByReferenceType['Book']['source'] = true;
$requiredFieldsByReferenceType['Book Section']['volume'] = true;
$requiredFieldsByReferenceType['Book Section']['source'] = true;
$requiredFieldsByReferenceType['Chart or Table']['volume'] = true;
$requiredFieldsByReferenceType['Chart or Table']['source'] = true;

var_dump($requiredFieldsByReferenceType);
link|flag
1
$requiredFieldsByReferenceType ['Book']['volume'] = true;
$requiredFieldsByReferenceType ['Book']['source'] = true;
$requiredFieldsByReferenceType ['Book Section']['volume'] = true;
link|flag
1

Only the answers that assign the array in ONE statement are going to work in your context (defining a class property) unless you put them all in the constructor. By the same token, I don't think that print_r is going to work without being in a method...

link|flag
0

Another way to do it is by nesting array() functions:

 $requiredFieldsByReferenceType = array(
           'Book' => array('volume' => true,
                                       'source' => true),
           'Book Section' => array('volume' => true,
                                                     'source' => true),
         ...
        );
link|flag
0

PHP doesn't do multi-dimensional arrays. You must construct it as arrays of arrays.

protected $myArray = array(
  'Book' => array(
    'item1' => true,
    'item2' => true,
  ),
  'Chest' => array(
  ),
    'item1' => true,
    'item2' => false,
);
link|flag
0
Class TestClass {
    protected $myArray = array(
        "Book" => array('item1' => true, 'item2' => false),
        "chest" => array('item1' => true, 'item2' => false),
    );
}
link|flag
0

The syntax to define an array is array(key0 => value0, key1 => value1, key2 => value2, ...). Since a two-dimensional array in PHP is just multiple arrays as values in an array, it would look like this:

$myArray =
    array(
        'Book' =>
            array(
                'item1' => true,
                'item2' => true
            ),
        'Chest' =>
            array(
                'item1' => true,
                'item2' => false
            )
    );
link|flag
0

Do this instead:

	$myArray = array(
			'book' => array(
				'item1' => true,
				'item2' => true
			),
			'chest' => array(
				'item1' => true,
				'item2' => true,
			)
		);

By the way you shouldn't initialize your attribues like this. Rather use getters/setters.

class TestClass {
    protected $_myArray;

    public function __construct()
    {
    	$this->setMyArray();
    }

    public function setMyArray()
    {
    	$this->_myArray = array(
    		'book' => array(
    			'item1' => true,
    			'item2' => true
    		),
    		'chest' => array(
    			'item1' => true,
    			'item2' => true,
    		)
    	);
    }
}


$foo = new TestClass();
print_r($foo);
link|flag
0

It looks like you're mixing styles of appending to an array here.

try

$arr = array(
    'key' => array ('key2' => 'value 1', 'key3' => 'value2'),
    'key12' => array('key4' => 'value4')
);

or

$arr = array();

$arr['key1'] = array();
$arr['key2'] = array();

$arr['key1']['key3'] = 'value1';

(Please note my examples don't produce the same data structure, I was just demonstrating the different methods)

link|flag

Your Answer

get an OpenID
or
never shown

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