2017-04-24 31 views
-4

如何循環遍歷整個這個數組並在PHP中讀取它?如何在PHP中讀取數組?

Array 
(
[order] => Array 
    (
     [apikey] => 798bdaedd4b46b62297b16decdad7a4f 
     [id] => 29049 
     [date] => 2017-04-04 13:00:00 
     [priority] => 2 
     [customs_value] => 1847700 
     [customs_currency] => USD 
     [creator] => Array 
      (
       [id] => 1 
       [name] => CloudPrinter.com 
       [version] => 2.1 
       [date] => 2017-04-04 13:00:00 
       [reference] => 161223200130000 
      ) 

     [client] => Array 
      (
       [id] => 7 
       [name] => GT 
       [date] => 2017-04-04 13:00:00 
       [reference] => 29049 
      ) 

     [addresses] => Array 
      (
       [0] => Array 
        (
         [company] => Lulu Press 
         [type] => delivery 
         [name] => Traverse 
         [street1] => 627 Davis Dr. 
         [street2] => Suite 300 
         [city] => Morrisville 
         [zip] => 27560 
         [country] => US 
         [email] => 
         [phone] => +1 919-260-2140 
         [state] => NC 
        ) 

      ) 

     [shipping] => Array 
      (
       [method] => jam_ae_fedex_intl_priority 
      ) 

     [items] => Array 
      (
       [0] => Array 
        (
         [id] => 161223200130001 
         [count] => 1 
         [title] => 0850X1100FCSTDPB060UWMXX - 18977878 
         [product] => 0850X1100FCSTDPB060UW 
         [desc] => GLassTree - 0850X1100FCSTDPB060UW 
         [files] => Array 
          (
           [0] => Array 
            (
             [type] => cover 
             [format] => pdf 
             [url] => https://s3-eu-west-1.amazonaws.com/798bdaedd4b46b62297b16decdad7a4f.cloudprinter.com/161223200130000/161223200130001/161223200130001_cover.pdf 
             [md5sum] => c779fec8d16565d5049b3877dc846511 
             [size] => 344682 
            ) 

           [1] => Array 
            (
             [type] => book 
             [format] => pdf 
             [url] => https://s3-eu-west-1.amazonaws.com/798bdaedd4b46b62297b16decdad7a4f.cloudprinter.com/161223200130000/161223200130001/161223200130001_book.pdf 
             [md5sum] => d6f6b15816d4e5cfce62e17df895fe2b 
             [size] => 137858352 
            ) 

          ) 

         [pages] => 176 
         [options] => Array 
          (
           [0] => Array 
            (
             [option] => M 
             [desc] => GlassTree Finish Matte 
             [count] => 1 
            ) 

          ) 

        ) 
      ) 
    ) 
) 

該數組在HTTP POST請求中作爲JSON接收。我正在使用Laravel 5.4。最後,我使用Eloquent ORM將此數組存儲在數據庫中。

這裏是我使用的應用程序/ HTTP /控制器Front.php位指示代碼/ Front.php

<?php 

namespace App\Http\Controllers; 

use App\Order; 
use App\Creator; 
use Auth; 
use App\Http\Controllers\Controller; 
//use Illuminate\Support\Facades\Request; 
use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Redirect; 

class Front extends Controller { 

public function createOrder(Request $request) { 
    $data = $request->json()->all(); 

    // Code to read array goes here 

    print_r($data); 


    } 
} 
+0

你將有更具體的瞭解你想要做什麼。通過數組「循環」你的意思是什麼_exactly_?當然你可以自己實現一些這樣做的循環。那什麼? _你想存儲數據嗎? _究竟是什麼數據? _Where_是數據格式的定義? – arkascha

+0

當問題很簡單時,不要試圖讓我的朋友更難。 –

+0

我試着理解你的問題,並幫助你加強它,讓你獲得更好的結果。對不起,如果這不是你的意圖。 – arkascha

回答

1
<?php 

$apiKey = $data['order']['apiKey']; 
$addresses= $data['order']['addresses']; 
foreach($addresses as $a){ 
    $company= $a['company']; 
    $type= $a['type']; 
} 

?> 
1

您可以使用以下陣列上迭代;

<?php 

foreach($data as $order){ 
    // do something with the order here 
    $apiKey = $order['apiKey']; 
} 

?> 
+0

如何訪問$ order對象中的apiKey元素? –

+0

嗨,我已經更新了我的答案,告訴你如何訪問apikey – Garbit

+0

如果它是正確的,請不要忘記接受我的回答 – Garbit