1

I do have a script that receives a a phone number and an amount. This is shown as below. Then you json encode the data from the array.

$phoneNumber = "+234424321";

$amount = "120.00";

$recipients = array(
    array("phoneNumber"=>"$phoneNumber", "amount"=>"$amount")
); 
$recipientStringFormat = json_encode($recipients);

This works for one phone number and one amount. I would want to scale this up such that if anyone uploads say a csv file with two columns phone number and amount they can populate the phone number and amount and they are all converted to array and json encoded as well.

Would anyone kindly assist to do this in PHP? Thank you.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • note, that PHP arrays don't allow duplicate keys. Therefore, can you show how should look the expected array? – RomanPerekhrest Jan 27 '17 at 19:15
  • `$recipients = array( array("phoneNumber"=>"+599595", "amount"=>"10.00") array("phoneNumber"=>"+51553678", "amount"=>"10.00") );` So they expect the details to be passed this way. –  Jan 27 '17 at 19:18
  • So how can I scale it up such that if someone uploads a file say a csv file it populates it well and this is passed as required. @RomanPerekhrest kindly advice –  Jan 27 '17 at 19:21

3 Answers3

1

Let's say that we have a file recipients.csv with the following contents:

+234424321,120.00
+334424321,130.00
+444424321,1440.00
+554424321,155.50

To get a multidimensional array with the needed details use the following approach.
Used functions: array_map, str_getcsv, array_combine and file:

$data = array_map(function($line){
    return array_combine(['phoneNumber', 'amount'], str_getcsv($line));
}, file("new_products.csv"));

print_r($data);

The output(for the above contents):

Array
(
    [0] => Array
        (
            [phoneNumber] => +234424321
            [amount] => 120.00
        )

    [1] => Array
        (
            [phoneNumber] => +334424321
            [amount] => 130.00
        )

    [2] => Array
        (
            [phoneNumber] => +444424321
            [amount] => 1440.00
        )

    [3] => Array
        (
            [phoneNumber] => +554424321
            [amount] => 155.50
        )

)

http://php.net/manual/en/function.str-getcsv.php

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

As you are using array of arrays, it can be like this:

$recipients = array(
    array("phoneNumber"=>"$phoneNumber1", "amount"=>"$amount1"),
    array("phoneNumber"=>"$phoneNumber2", "amount"=>"$amount2")
);

or this way

$recipients = array();
$recipients[] = array("phoneNumber"=>"$phoneNumber1", "amount"=>"$amount1");
$recipients[] = array("phoneNumber"=>"$phoneNumber2", "amount"=>"$amount2");

Please add some logic to those array operations (like loops etc.) regarding to your needs.

michail_w
  • 4,318
  • 4
  • 26
  • 43
  • Hi, how can I add a loop? Essentially I would want of someone uploads a file with of say 1000 rows with two columns one phone number and amount it loops then populates this way. ----> `$recipients = array( array("phoneNumber"=>"+599595", "amount"=>"10.00") array("phoneNumber"=>"+51553678", "amount"=>"10.00") );` This is the required format. Kindly advice –  Jan 27 '17 at 19:25
  • Please read how to parse CSV files. You have to read those files line by line, using any loop like `for` or `while`. – michail_w Jan 27 '17 at 19:26
0

Try this:

$recipients = array();
$phone_number = "+234424321";
$amount = "120.00"; 

for($i=0; $i<2; $i++){
    $recipients[$i]["phone_number"] = "+" . $phone_number + $i;
    $recipients[$i]["amount"]       = $amount + $i;
}

print_r($recipients);


The output produced will be:

Array ( 
   [0] => Array ( [phone_number] => +234424321 [amount] => 120 ) 
   [1] => Array ( [phone_number] => +234424322 [amount] => 121 ) 
)


Note that the for loop above is just for reference. There's plenty of examples (such as this) on how to read a CSV file in PHP. You could read that file iteratively, until the EOF, and keep storing the variables in the array as suggested here.

Community
  • 1
  • 1
Dhruv Saxena
  • 1,336
  • 2
  • 12
  • 29