簡體   English   中英

數組值來分配PHP中的變量

[英]Array Values to assign the variables in php

數組值以分配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.'"}];

我嘗試了這段代碼,最后一個數組值僅打印array [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"}]

您想從數組創建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函數。

我同意利亞姆的觀點, json_encode()可能是解決您問題的最佳解決方案。 但是,如果您正在尋找DIY解決方案,則非常簡單:

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

您的代碼中存在的問題是,在foreach循環運行之后,您只分配了一次JSON數據。 相反,您需要將每個發票的JSON數據附加到其他發票的JSON數據。

盡管如此, json_encode()可能是更好的解決方案,因為它只有一行,更易於閱讀,並且更容易理解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM