Martin
Martin

Reputation: 1279

Read from large array

I want to ask how can I read from my associative array:

Array
(
    [0] => Array
        (
            [imie;nazwisko;telefon] => 3;[email protected];123456789
        )

    [1] => Array
        (
            [imie;nazwisko;telefon] => 6;[email protected];123456789
        )

    [2] => Array
        (
            [imie;nazwisko;telefon] => 7;[email protected];123456789
        )

    [3] => Array
        (
            [imie;nazwisko;telefon] => 16;[email protected];123456789
        )

    [4] => Array
        (
            [imie;nazwisko;telefon] => 17;[email protected];123456789
        )

    [5] => Array
        (
            [imie;nazwisko;telefon] => 19;[email protected];123456789
        )

    [6] => Array
        (
            [imie;nazwisko;telefon] => 32;[email protected];123456789
        )

    [7] => Array
        (
            [imie;nazwisko;telefon] => 39;[email protected];123456789
        )

    [8] => Array
        (
            [imie;nazwisko;telefon] => 50;[email protected];123456789
        )

    [9] => Array
        (
            [imie;nazwisko;telefon] => 52;[email protected];123456789
        )

    [10] => Array
        (
            [imie;nazwisko;telefon] => 54;[email protected];123456789
        )

    [11] => Array
        (
            [imie;nazwisko;telefon] => 720;[email protected];123456789
        )

    [12] => Array
        (
            [imie;nazwisko;telefon] => 54;[email protected];123456789
        )

)

I create this array with this code:

$csv = array_map("str_getcsv", file($path,FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);
foreach ($csv as $i=>$row) {
$csv[$i] = array_combine($keys, $row);
}
echo '<pre>';
print_r($csv);
echo '</pre>';

How can I loop through all items and then get from each item: imie,nazwisko and telefon?

Upvotes: 2

Views: 94

Answers (2)

Mumpo
Mumpo

Reputation: 548

I would change the code of yours for this one:

$csv = array_map("str_getcsv", file($path,FILE_SKIP_EMPTY_LINES));
$keys = explode(";", array_shift($csv));
foreach ($csv as $i=>$row) {
    $csv[$i] = array();
    foreach (explode(";", $row) as $j => $element) {
         $csv[$i][$keys[$j]] = $element;
    }
}

This way you obtain the correct associative array.

Upvotes: 0

Daan
Daan

Reputation: 12236

Use a few loops and the explode function:

foreach($array as $arrIdx => $subArr){
   foreach($subArr as $keys => $values) {
         $key = explode(";", $keys);
         $value = explode(";", $values);

         for($i=0; $i <= 2; $i++){
            echo $key[$i]." : ".$value[$i];
         }
   }
}

Upvotes: 2

Related Questions