0

I am trying to get a simple two dimensional array set up such as:

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

So that:

$transactionType [0][0] would return 'B'

$transactionType [1][0] would return 'Boat purchase'

$transactionType [0][1] would return 'S'

$transactionType [1][1] would return 'Start up' etc

The following works but appears a little messy to me. Is there is neater way of doing it?

$transactionType = array (array('B', 'Boat purchase'), array('S', 'Start up'), array('F', 'Fee'), array('M', 'Maintenance'), array('D', 'Deposit from client'), array('R','Rent'), array('O', 'Other'));
RGriffiths
  • 5,722
  • 18
  • 72
  • 120

3 Answers3

1

What's wrong with

$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');

You pretty much had it right the first time :).

Matt
  • 2,851
  • 1
  • 13
  • 27
1

Wouldn't a key => value approach be more suitable?

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

$transactionIds = array_keys($transactions);
$transactionValues = array_values($transactions);
jakub_jo
  • 1,494
  • 17
  • 22
0

When you have:

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

Then:

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

Output is:

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
    )

)
Positivity
  • 5,406
  • 6
  • 41
  • 61