Mira
Mira

Reputation: 87

Creat List From CSV file in PHP

I am new in PHP. I am working for make bulk message script for twillio. Its require number list like below

$subscribers = [
   json_encode(['binding_type' => "sms", 'address' => "+910000000000"]),
   json_encode(['binding_type' => "sms", 'address' => "+910000000000"])
];

I have data in csv file like below image

enter image description here

I am able to read the data from it like below

$csv = array_map('str_getcsv', file('data.csv'));

When I do

print_r($csv);

its showing data like

Array ( [0] => Array ( [0] => 910000000000) [1] => Array ( [0] => 910000000000) ) 

I am not getting idea how I can create list like below for use with twillio

$subscribers = [
   json_encode(['binding_type' => "sms", 'address' => "+910000000000"]),
   json_encode(['binding_type' => "sms", 'address' => "+910000000000"])
];

Let me know if anyone here can help me for do it. Thanks!

Upvotes: 0

Views: 56

Answers (1)

Vel
Vel

Reputation: 9342

Try this code

<?php
    $csv = array_map('str_getcsv', file('data.csv'));
    

    $subscribers = array();

    foreach ($csv as $key => $value) {
        $item = array();
        $item['binding_type'] = "sms";
        $item['address'] =  "+".$value[0];

        array_push($subscribers, json_encode($item));
    }
?>

Upvotes: 1

Related Questions