Skip to content
Advertisement

Parse CSV file into columns using PHP

I’m unsure as to how I would add columns to information I have within my CSV file. The three columns that I would like to have are Mobile Number, Carrier Name and Validity Status.

index.php:

<?php

if (($open = fopen("Book1.csv", "r")) !== FALSE)
{

while (($data = fgetcsv($open, 1000, ",")) !== FALSE)
{       
$array[] = $data;
}

fclose($open);
}
echo "<pre>";
//To display array data
var_dump($array);
echo "</pre>";

My information currently displays as shown in the following code:

array(6) { [0]=> array(3) {
                [0]=> string(11) "0744395****"
                [1]=> string(9) " Vodafone"
                [2]=> string(6) " Valid"
            }
            [1]=>    array(3) {
                [0]=> string(11) "0744395****"
                [1]=> string(3) " EE"
                [2]=> string(6) " Valid"
            }
            [2]=> array(3) {
                [0]=> string(11) "0744395****"
                [1]=> string(6) " Three"
                [2]=> string(6) " Valid"
            }
            [3]=> array(3) {
                [0]=> string(11) "0744395****"
                [1]=> string(9) " Vodafone"
                [2]=> string(6) " Valid"
            }
            [4]=> array(3) {
                [0]=> string(11) "0744395****"
                [1]=> string(13) " Tesco Mobile"
                [2]=> string(10) " Not Valid"
            }
            [5]=> array(3) {
                [0]=> string(11) "0744395****"
                [1]=> string(3) " EE"
                [2]=> string(6) " Valid"
            } 
}

I would like for the information to be columned as shown below (online example I found):

[REPTILE] => Array(
        [0] => stdClass Object(
                [animal] => crocodile
                [type] => REPTILE
                [number] => 4
            )
    )

[BIRD] => Array(
        [0] => stdClass Object(
                [animal] => duck
                [type] => BIRD
                [number] => 2
            )
    )

[MAMMAL] => Array (
        [0] => stdClass Object(
                [animal] => koala
                [type] => MAMMAL
                [number] => 4
            )
        [1] => stdClass Object(
                [animal] => lion
                [type] => MAMMAL
                [number] => 5
            )
    )

[FISH] => Array
    (
        [0] => stdClass Object(
                [animal] => áéíóú
                [type] => FISH
                [number] => 3
            )
    )

This is how my information is stored within the CSV file:

0744395****, Vodafone, Valid
0744395****, EE, Valid
0744395****, Three, Valid
0744395****, Vodafone, Valid
0744395****, Tesco Mobile, Not Valid
0744395****, EE, Valid

Advertisement

Answer

$newArray = [];

if (($fh = fopen("Book1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($fh, 1000, ",")) !== FALSE) {
        $o = new stdClass;
        
        $o->MobileNumber    = trim($data[0]);
        $o->Carrier         = trim($data[1]);
        $o->ValidityStatus  = trim($data[2]);
        $newArray[$o->Carrier][] = $o;
    }
}

Should produce an array of Carriers, each with an array of objects with phone number and statuses inside

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement