2017-04-24 93 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