1

I'm trying to add multiple arrays to an array using foreach.

$data = array(
    array('name'=>'John','age'=>'19'), //loop 1
    array('name'=>'Bob','age'=>'32'),  //loop 2
    array('name'=>'Kate','age'=>'22'), //loop 3
                                       //more loops
); 

How do I add more array('name'=>'Kate','age'=>'22'); like arrays to $data array using foreach?

2
  • It's not clear what you're asking -- you aren't using foreach at all in this code. Also, your code won't run -- you have semicolons inside array(), which will break the parser. Commented Nov 16, 2013 at 8:41
  • how do you create your loop arrays? Commented Nov 16, 2013 at 8:45

2 Answers 2

1

Try like

$data[] = array('name'=>'Kate','age'=>'22');
Sign up to request clarification or add additional context in comments.

Comments

1

You may use array_push..

array_push($data,array('name'=>'Kate','age'=>'22'));

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.