1

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
8
  • 2
    Great, but how are you going to decide on which are reptiles or birds or fish or .... And when did mobile phones get clasified that way anyway??? PHP like all other programming languages stops short of down right Magic OR am I the one getting the wrong end of the stick here Commented Sep 7, 2021 at 13:35
  • Oh I get it, that was just a bad example. If you want us to understand your requirement, spend a little time showing the "WHAT I WANT" array using data from the "WHAT I STARTED WITH" array and maybe it will all make sense Commented Sep 7, 2021 at 13:42
  • The animal, type and number is the part I'm hoping to have a similar output for the Mobile Number, Carrier Name and Validity Status. I just used that online code as an example of how I would like my code to be structured. Commented Sep 7, 2021 at 13:43
  • 1
    My bad. Maybe wasn't written or explained as well as I hoped so. Commented Sep 7, 2021 at 13:44
  • Do you want to group records per mobile phone operator? As noted already by @Riggs, please edit the question to show the "WHAT I WANT" array using data from the "WHAT I STARTED WITH" array. Commented Sep 7, 2021 at 13:45

1 Answer 1

1
$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

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

7 Comments

This is basically what your "WHAT I WANT" example suggests, so what did you have in mind
The way in the online example has [animal] => koala [type] => MAMMAL [number] => 4 I need [Mobile Number] => 07738****** [Carrier] => EE [Validity Status] => Valid. I need this to display in this structure for each data output
Yea, that you will get from this code, but all the EE numbers will be under an array called EE like your FISH etc
array(1) { ["0744395****"]=> array(6) { [0]=> object(stdClass)#1 (3) { ["carrier"]=> string(11) "0744395****" ["phone"]=> string(9) " Vodafone" ["status"]=> string(6) " Valid" } [1]=> object(stdClass)#2 (3) { ["carrier"]=> string(11) "0744395****" ["phone"]=> string(3) " EE" ["status"]=> string(6) " Valid" }
Oh for heavans sake, Then move the parts around ALL I did was get the order of the fields on the csv wrong
|

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.