在php中设置二维数组

时间:2016-02-20 21:27:07

标签: php arrays

我正在尝试设置一个简单的二维数组,例如:

got the Int 3

那样:

$ transactionType [0] [0]将返回' B'

$ transactionType [1] [0]将返回'购买船'

$ transactionType [0] [1]将返回' S'

$ transactionType [1] [1]将返回'启动'等

以下作品对我来说有点乱。是否有更简洁的方式呢?

$transactionType [0][] = array ('B', 'S', 'F', 'M', 'D', 'R', 'O');
$transactionType [1][] = array ('Boat purchase', 'Start up', 'Fee', 'Maintenance', 'Deposit from client', 'Rent', 'Other');

3 个答案:

答案 0 :(得分:1)

有什么问题
$transactionType = array();
$transactionType [0][] = array ('B', 'S', 'F', 'M', 'D', 'R', 'O');
$transactionType [1][] = array ('Boat purchase', 'Start up', 'Fee', 'Maintenance', 'Deposit from client', 'Rent', 'Other');

你几乎第一次做对了:)。

答案 1 :(得分:1)

键不会=>价值方法更合适吗?

$transactions = [
   'B' => 'Boat purchase',
   'S' => 'Start up'
];

$transactionIds = array_keys($transactions);
$transactionValues = array_values($transactions);

答案 2 :(得分:0)

当你有:

$transactionType0 = array ('B', 'S', 'F', 'M', 'D', 'R', 'O');
$transactionType1 = array ('Boat purchase', 'Start up', 'Fee', 'Maintenance', 'Deposit from client', 'Rent', 'Other');

然后:

$transactionType = array();
foreach($transactionType0 as $key => $value) {
    $transactionType[$key] = array($transactionType0[$key], $transactionType1[$key]);
}

输出是:

Array
(
[0] => Array
    (
        [0] => B
        [1] => Boat purchase
    )

[1] => Array
    (
        [0] => S
        [1] => Start up
    )

[2] => Array
    (
        [0] => F
        [1] => Fee
    )

[3] => Array
    (
        [0] => M
        [1] => Maintenance
    )

[4] => Array
    (
        [0] => D
        [1] => Deposit from client
    )

[5] => Array
    (
        [0] => R
        [1] => Rent
    )

[6] => Array
    (
        [0] => O
        [1] => Other
    )

)