Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中读取数组?_Php_Arrays - Fatal编程技术网

如何在PHP中读取数组?

如何在PHP中读取数组?,php,arrays,Php,Arrays,如何循环遍历整个数组并在php中读取它 Array ( [order] => Array ( [apikey] => 798bdaedd4b46b62297b16decdad7a4f [id] => 29049 [date] => 2017-04-04 13:00:00 [priority] => 2 [customs_value] => 1847700 [

如何循环遍历整个数组并在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。最后,我使用雄辩的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);


    } 
}

您可以使用以下命令迭代数组:

<?php

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

?>



您必须更具体地说明您想要做什么。你所说的通过数组的“循环”到底是什么意思?当然,您可以自己实现一些循环来实现这一点。然后呢?您希望如何存储数据?具体是什么数据?数据格式的定义在哪里?当问题很简单时,不要试图让它变得更难,我的朋友。我试图理解你的问题,并帮助你改进它,以便获得更好的结果。抱歉,如果这不是您的意图。如何访问$order对象中的apiKey元素?您好,我已经更新了我的答案,向您展示了如何访问apiKey
<?php

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

?>