Marcin Magdziarz Marcin Magdziarz - 3 years ago 115
PHP Question

Read from large array

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

Array
(
[0] => Array
(
[imie;nazwisko;telefon] => 3;email@gmail.com;123456789
)

[1] => Array
(
[imie;nazwisko;telefon] => 6;email@gmail.com;123456789
)

[2] => Array
(
[imie;nazwisko;telefon] => 7;email@gmail.com;123456789
)

[3] => Array
(
[imie;nazwisko;telefon] => 16;email@gmail.com;123456789
)

[4] => Array
(
[imie;nazwisko;telefon] => 17;email@gmail.com;123456789
)

[5] => Array
(
[imie;nazwisko;telefon] => 19;email@gmail.com;123456789
)

[6] => Array
(
[imie;nazwisko;telefon] => 32;email@gmail.com;123456789
)

[7] => Array
(
[imie;nazwisko;telefon] => 39;email@gmail.com;123456789
)

[8] => Array
(
[imie;nazwisko;telefon] => 50;email@gmail.com;123456789
)

[9] => Array
(
[imie;nazwisko;telefon] => 52;email@gmail.com;123456789
)

[10] => Array
(
[imie;nazwisko;telefon] => 54;email@gmail.com;123456789
)

[11] => Array
(
[imie;nazwisko;telefon] => 720;email@gmail.com;123456789
)

[12] => Array
(
[imie;nazwisko;telefon] => 54;email@gmail.com;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?

Answer Source

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];
         }
   }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download