简体   繁体   中英

Add items in loop to multidimensional PHP array

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?

try:

$html[] = array(
  'strongsNum' => $strongsCode,
  'wordNum' => $wordNumber,
);
$html[] = array(
    'strongsNum' => $strongsCode,
    'wordNum' => $wordNumber
);

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM