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'));
3
  • Possible duplication of stackoverflow.com/questions/1319903/… Commented Feb 20, 2016 at 21:28
  • 1
    What is the desired output? The first code snippet doesn't create the same array as the one in the second. Commented Feb 20, 2016 at 21:37
  • consider marking an answer as answer if it helped you, please. Commented Feb 29, 2016 at 19:00

3 Answers 3

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 :).

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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
    )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.