0

I'm attempting to add a couple of columns to a multidimensional PHP array inside a loop. Inside the loop I currently have this:

$html[]['strongsNum'] = $strongsCode;
$html[]['wordNum'] = $wordNumber;

However, because I'm not setting the index manually, it creates two separate entries for the two. How can I make it add the two columns to the one entry / row of the array?

3 Answers 3

2

try:

$html[] = array(
  'strongsNum' => $strongsCode,
  'wordNum' => $wordNumber,
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! That definitely makes sense, since a multidimensional array is essentially an array within an array.
1
$html[] = array(
    'strongsNum' => $strongsCode,
    'wordNum' => $wordNumber
);

Comments

-2

If you don't want to use the array(key => value) syntax:

After adding the initial 'strongsNum', you can re-access the last member of your array by using count($myArray)-1 as the index.

$html[]['strongsNum'] = $strongsCode;
$html[count($html) - 1]['wordNum'] = $wordNumber;

1 Comment

I just want people to know that this is an option.

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.