简体   繁体   中英

how to store two variables strings in 2d array in php

I'm working with multi-dimensional arrays in PHP. I want to store two string values in a two dimensional array. I have tried the following code:

$arr[][]=['string1']['string2'];

I have also tried:

$arr[][]="string1","string2";

Due to the comma I have a syntax error. How can I fix this?

$arr = [];
$arr[] = ['string1'];
$arr[] = ['string2'];

// or simply
// $arr = [['string1'], ['string2']];

print_r($arr);

// output:
Array ( [0] => Array ( [0] => string1 ) [1] => Array ( [0] => string2 ) )

If you want 'string1' and 'string2' to be the key and subkey in an array then you use:

$arr = [];
$arr['string1']['string2'] = 'somevalue';

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