0

How can I add an index (1,2,3) to an array like this:

$errors['success'] = false; //0
$errors['#errOne'] = "Enter a valid username"; //1
$errors['#errTwo'] = "Enter a valid email";//2
$errors['#errThree'] = "Enter a valid password";//3

2 Answers 2

2

Just use the integer index instead of the string index.

$errors[0] = false;

If your order doesn't matter, it's easier yet to not specify an index, and PHP will push it onto the array.

$errors[] = false;
$errors[] = "Enter a valid username";

Looking at your structure though, I would suggest not keeping such a mix of things in your array. You should have an array for your list of errors, and a separate value for whether or not something was successful. (Is the definition of successful no errors? If so, you can check for that.) Maybe something like this instead?

$status['success'] = false;
$status['errors'] = array();
$status['errors'][] = 'Enter a valid username';
// etc.
Sign up to request clarification or add additional context in comments.

Comments

1

If you dont care about elements order:

$errors = array_values($errors);

If you need to specify a some order:

$errors = array(
   $errors['success']
   $errors['#errOne']
   $errors['#errTwo']
   $errors['#errThree']
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.