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.
我有一个接收电话号码和金额的脚本。如下所示。然后你json编码数组中的数据。
$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.
这适用于一个电话号码和一个金额。我想扩大规模,如果有人上传说csv文件有两列电话号码和数量,他们可以填充电话号码和数量,他们都转换为数组和json编码。
Would anyone kindly assist to do this in PHP? Thank you.
有人会帮助用PHP做这个吗?谢谢。
3 个解决方案
#1
1
Let's say that we have a file recipients.csv
with the following contents:
假设我们有一个带有以下内容的文件recipients.csv:
+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
:
要获得具有所需详细信息的多维数组,请使用以下方法。使用的函数:array_map,str_getcsv,array_combine和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
)
)
#2
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.
请根据您的需要为这些数组操作(如循环等)添加一些逻辑。
#3
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.
请注意,上面的for循环仅供参考。关于如何在PHP中读取CSV文件的例子很多(比如这个)。您可以迭代地读取该文件,直到EOF,并按照此处的建议继续将变量存储在数组中。
#1
1
Let's say that we have a file recipients.csv
with the following contents:
假设我们有一个带有以下内容的文件recipients.csv:
+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
:
要获得具有所需详细信息的多维数组,请使用以下方法。使用的函数:array_map,str_getcsv,array_combine和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
)
)
#2
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.
请根据您的需要为这些数组操作(如循环等)添加一些逻辑。
#3
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.
请注意,上面的for循环仅供参考。关于如何在PHP中读取CSV文件的例子很多(比如这个)。您可以迭代地读取该文件,直到EOF,并按照此处的建议继续将变量存储在数组中。