如何在PHP的二维数组中存储两个变量字符串

时间:2019-03-08 17:45:04

标签: php arrays multidimensional-array

我正在使用PHP中的多维数组。我想在二维数组中存储两个字符串值。我尝试了以下代码:

Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Continuum\Anaconda3\lib\site-packages\sqlalchemy\pool.py", line 1122, in _do_get
    return self._pool.get(wait, self._timeout)
  File "C:\Users\xx\AppData\Local\Continuum\Anaconda3\lib\site-packages\sqlalchemy\util\queue.py", line 145, in get
    raise Empty
sqlalchemy.util.queue.Empty

During handling of the above exception, another exception occurred:

sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

我也尝试过:

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

由于逗号,我语法错误。 我该如何解决?

1 个答案:

答案 0 :(得分:3)

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

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

print_r($arr);

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

如果您希望'string1''string2'作为数组中的键和子键,则可以使用:

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