如何在PHP中读取数组?

时间:2017-04-24 07:44:51

标签: php arrays

如何遍历整个数组并在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将此数组存储在数据库中。

以下是我在app / Http / Controllers / 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);


    } 
}

2 个答案:

答案 0 :(得分:1)

您可以使用以下内容迭代数组;

<?php

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

?>

答案 1 :(得分:1)

<?php

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

?>