1

I have data from a csv file that I organized into a multidimensional array using the following method:

$handle = fopen("bankdata.csv", "r");

while(($data = fgetcsv($handle, 0 ,",")) !==FALSE) {

    $transactions[] = $data;

 }  

The array now looks something like this:

Array
(
[0] => Array
    (
        [0] => 2000
        [1] => paycheck
        [2] => credit
    )

[1] => Array
    (
        [0] => 75
        [1] => grocery
        [2] => debit
    )

[2] => Array
    (
        [0] => 45
        [1] => gas
        [2] => debit
    )

[3] => Array
    (
        [0] => 900
        [1] => investments
        [2] => credit
    )

[4] => Array
    (
        [0] => 1500
        [1] => bonus
        [2] => credit
    )  

Now I would like to name the keys in each nested array. I thought I would create a new multidimensional array with an equal amount of nested arrays, their values being the intended name of the keys I would like to add to the original array, then do 'array_combine':

$names = array('amount','source','type');

$run = 1;

while($run < 6){
  $run = $run +1;  
  $names2[] = $names;   
}

$combine = array_combine($names2, $transactions);

2 Answers 2

1

You could use array combine:

$keynames=array('amount','source','type');
foreach ($transactions as $i=>$row) {
   $transactions[$i]=>array_combine($keynames, $row);
}

The right way to solve the problem is not to read the the data into an array then transform it - transform it as you read it.

while(($data = fgetcsv($handle, 0 ,",")) !==FALSE) {
    $transactions[]=array_combine($keynames, $data);
}

BTW PHP doesn't do multi-dimensional arrays - they're nested. Despite what it says in the manual, they only emulate multi-dimensional arrays.

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

3 Comments

Your solution is likely much better than mine, more efficient too.
Perfect Thank you. BTW what's the difference between an "emulation" and the real thing?
Emulation? For most intents and purposes it will behave like a multi-dimensional array but sparse arrays will use less space - are not constrained to any pre-defined dimensions and unset values behave differently from null values.
1

Try the following:

$arr = array(
    array(2000, 'paycheck', 'credit'),
    array(75, 'grocery', 'debit'),
    array(45, 'gas', 'debit'),
    array(900, 'investments', 'credit'),
    array(1500, 'bonus', 'credit')
);

$keys = array('amount','source','type');

// $a will make a reference to the array within $arr
// and override the array
foreach($arr as &$a)
{
    // Override the array keys
    $a = array_combine($keys, $a);
}

/* Output:
Array
(
    [0] => Array
        (
            [amount] => 2000
            [source] => paycheck
            [type] => credit
        )

    [1] => Array
        (
            [amount] => 75
            [source] => grocery
            [type] => debit
        )

    [2] => Array
        (
            [amount] => 45
            [source] => gas
            [type] => debit
        )

    [3] => Array
        (
            [amount] => 900
            [source] => investments
            [type] => credit
        )

    [4] => Array
        (
            [amount] => 1500
            [source] => bonus
            [type] => credit
        )

)
*/

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.