2

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?

1
  • You had some real email addresses and telephone numbers here. I've submitted an order to remove them from the revision history, but they've been scraped on other sites. You probably want to contact these people and make sure they know that their information has been leaked, so they can take appropriate steps. Commented Sep 30, 2017 at 15:30

2 Answers 2

2

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];
         }
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

That's great! Is there any possibility to check the number of columns instead of using static value? for($i=0; $i <= 2; $i++){
@forexknight use $i < count($key)
@forexknight Yes sure, change loop to for($i=0; $i <= count($value)-1; $i++){ }
0

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.

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.