Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

try:

$html[] = array(
  'strongsNum' => $strongsCode,
  'wordNum' => $wordNumber,
);
share|improve this answer
    
Thanks a lot! That definitely makes sense, since a multidimensional array is essentially an array within an array. –  Adam Mar 19 '12 at 19:52
$html[] = array(
    'strongsNum' => $strongsCode,
    'wordNum' => $wordNumber
);
share|improve this answer

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;
share|improve this answer
2  
I'd use array(key => value). This doesn't read very good. –  AndrewR Mar 19 '12 at 19:59
    
I just want people to know that this is an option. –  Jake May 4 '12 at 14:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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