drpelz
drpelz

Reputation: 811

Add items to associative array

I have an array like this:

$errors = new array();

There are some PHP-variables which contain data:

$errorID = 0;
$errorTxt = 'Success! You have been registered!';
$errorID2 = 1;
$errorTxt2 = 'Failure! Something went wrong!!';

My question is:

Is it possible to add data to the array in this way:

$errors[$errorID] = $errorTxt;
$errors[$errorID2] = $errorTxt2;

Also I'd like to know how do you iterate the array so I can get the value of a certain key? Just wanted to mention that this is an associative array...

Upvotes: 0

Views: 112

Answers (1)

Nir Alfasi
Nir Alfasi

Reputation: 53525

All you had to do is add

var_dump($errors);

and run the code - and you would see that it works fine.

You don't "parse" an array - you parse a string.

You can iterate an associative array using foreach loop:

foreach($errors as $k => $v)
    echo "$k $v"

Upvotes: 1

Related Questions