Array用于在php中分配变量的值

时间:2015-12-16 20:06:50

标签: php

Array用于在php中分配变量的值:

Array
    (
        [0] => stdClass Object
            (
                [Id] => 116249
                [Amount] => 51.62
                [Currency] => INR
                [ExchangeRate] => 1
                [InvoiceDate] => 2015-12-16T00:00:00Z
                }
       [1] => stdClass Object
            (
                [Id] => 116250
                [Amount] => 55.20
                [Currency] => KWD
                [ExchangeRate] => 1
                [InvoiceDate] => 2015-12-16T00:00:00Z
                }

       [2] => stdClass Object
            (
                [Id] => 116251
                [Amount] => 59.42
                [Currency] => USD
                [ExchangeRate] => 1
                [InvoiceDate] => 2015-12-16T00:00:00Z
                }  

    }

foreach ($invoice as $key => $value) 
{
        $Id=$value->Id;
        $Amount=$value->Amount;
        $Currency=$value->Currency;
        $ExchangeRate=$value->ExchangeRate;
        $invoiceDate = str_replace('Z', '', str_replace('T', ' ',$value->InvoiceDate)); 



 }            


  $jsonData ='[{"Id": "'.$Id.'",
                "Amount": "'.$Amount.'",
                "Currency": "'.$Currency.'",
                "ExchangeRate": "'.$ExchangeRate.'",
                "invoiceDate": "'.$invoiceDate.'"}];

我试过这段代码最后一个数组值只打印数组[2]值,我需要像

这样的输出
[{"Id": "116249",
                    "Amount": "51.62",
                    "Currency": "INR",
                    "ExchangeRate": "1",
                    "invoiceDate": "2015-12-16T00:00:00Z"},

                    {"Id": "116250",
                    "Amount": "55.20",
                    "Currency": "KWD",
                    "ExchangeRate": "1",
                    "invoiceDate": "2015-12-16T00:00:00Z"},

                    {"Id": "116251",
                    "Amount": "59.42",
                    "Currency": "USD",
                    "ExchangeRate": "1",
                    "invoiceDate": "2015-12-16T00:00:00Z"}]

2 个答案:

答案 0 :(得分:0)

您想要从数组创建json字符串。您需要做的就是运行以下

jsonData = json_encode($invoice);

如果您只想使用循环创建数组来生成特定值,那么您可以执行以下操作:

$tmp = array();
foreach ($invoice as $key => $value) 
{
   $tmp["id"] = $value->Id;
   $tmp["amount"] = $value->Amount;
   $tmp["Currency"] = $value->Currency;
   $tmp["ExchangeRate"] = $value->ExchangeRate;
   $tmp["invoiceDate"] = str_replace('Z', '', str_replace('T','',$value->InvoiceDate));
 }  

jsonData = json_encode($tmp);

*编辑*

如果您使用mysql日期作为invoiceData变量。那么你最好使用PHP date函数。

答案 1 :(得分:0)

我同意Liam的观点,unordered_map可能是您问题的最佳解决方案。但是,如果您正在寻找DIY解决方案,那很简单:

json_encode()

您在代码中遇到的问题是,在foreach循环运行后,您只分配了一次JSON数据。相反,您需要将每张发票的JSON数据附加到其他发票的JSON数据中。

尽管如此,$jsonData = '['; foreach ($invoice as $key => $value) { $Id=$value->Id; $Amount=$value->Amount; $Currency=$value->Currency; $ExchangeRate=$value->ExchangeRate; $invoiceDate = str_replace('Z', '', str_replace('T', ' ',$value->InvoiceDate)); // Append the next invoice's JSON data to all of the JSON data $jsonData .='{"Id": "'.$Id.'", "Amount": "'.$Amount.'", "Currency": "'.$Currency.'", "ExchangeRate": "'.$ExchangeRate.'", "invoiceDate": "'.$invoiceDate.'"},'; } // Clean up and close off the JSON data: $jsonData = rtrim($jsonData, ','); // remove the trailing comma $jsonData .= ']'; // Add the closing bracket. 可能是更好的解决方案,因为它是一行,更易于阅读,并且可以说更容易理解。