第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

根據(jù)默認(rèn)索引動(dòng)態(tài)向數(shù)組添加空索引

根據(jù)默認(rèn)索引動(dòng)態(tài)向數(shù)組添加空索引

PHP
小怪獸愛(ài)吃肉 2023-10-22 21:35:43
我有下面的數(shù)組,其中包括一組默認(rèn)索引,它們必須出現(xiàn)在我的最終數(shù)組中:'entities' => [        'deliveredAt',        'issuedAt',        'totals' => [            'due',            'gross',            'net',            'tax' => [                'amount',                'net',                'rate',            ],        ]],上面的數(shù)組保存在一個(gè)名為 的變量中$entities?,F(xiàn)在,我有一個(gè)第 3 方 API,它將返回上述實(shí)體,但僅當(dāng)實(shí)體包含值時(shí)才將它們包含在響應(yīng)中。例如,a$response可以如下所示:array:2 [▼  "issuedAt" => "2020-08-20"  "totals" => array:1 [▼    "tax" => []  ]]正如您所看到的,如果將返回的數(shù)組與我期望的索引進(jìn)行比較,會(huì)發(fā)現(xiàn)缺少一些內(nèi)容:deliveredAttotals.due, totals.gross, totals.net, totals.tax.amount, totals.tax.net, totals.tax.rate我正在嘗試創(chuàng)建一個(gè)可以迭代數(shù)組的方法$response,并檢查它是否包含我期望的索引。如果沒(méi)有,我只想將索引設(shè)置為null。以下是我到目前為止所擁有的:foreach ($entities as $key => $entity) {         if (!is_array($entity)) {             if (!isset($response[$entity])) {                    $response[$entity] = null;             }         }}但是,這只會(huì)添加一個(gè)不是數(shù)組的索引。在此示例中,它只會(huì)添加:deliveredAt => null。我該怎么做,以便上述方法可以迭代多個(gè)至少 2 個(gè)嵌套數(shù)組并添加索引名稱和null值?
查看完整描述

2 回答

?
手掌心

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊

您可以使用鍵和NULL(或任何您需要的)作為值來(lái)定義初始數(shù)組:


$entities = [

    'deliveredAt' => null,

    'issuedAt' => null,

    'totals' => [

        'due' => null,

        'gross' => null,

        'net' => null,

        'tax' => [

            'amount' => null,

            'net' => null,

            'rate' => null,

        ],

    ]

];


// here's your real data

$realData = [

  "issuedAt" => "2020-08-20",

  "totals" => [

    "tax" => [

      'net' => 42,    

    ]

  ]

];

// now use array_replace_recursive to replace keys in `$entities` with values of `$realData`

print_r(array_replace_recursive($entities, $realData));

小提琴


另請(qǐng)注意,$realData不存在的鍵$entities將被添加到結(jié)果中。


查看完整回答
反對(duì) 回復(fù) 2023-10-22
?
呼喚遠(yuǎn)方

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊

您可以使用array_replace_recursive來(lái)執(zhí)行此操作。您只需稍微更改關(guān)聯(lián)數(shù)組實(shí)體,因此每個(gè)屬性都需要初始化(例如 NULL 或 '')。

$result?=?array_replace_recursive($entities,?$array);

在這里您可以測(cè)試它http://sandbox.onlinephpfunctions.com/code/4688ed3240050479edeef7c9e4da16f98dbe01de

這是孔代碼:

$array = [

? "issuedAt" => "2020-08-20",

? "totals" => [

? ? "tax" => [

? ? ? ? 'amount' => 100

? ? ]

? ]

];


$entities = [

? ? 'deliveredAt' => NULL,

? ? 'issuedAt' => NULL,

? ? 'totals' => [

? ? ? ? 'due' => NULL,

? ? ? ? 'gross' => NULL,

? ? ? ? 'net' => NULL,

? ? ? ? 'tax' => [

? ? ? ? ? ? 'amount' => NULL,

? ? ? ? ? ? 'net' => NULL,

? ? ? ? ? ? 'rate' => NULL

? ? ? ? ],

? ? ]

];


$result = array_replace_recursive($entities, $array);

var_dump($result);


查看完整回答
反對(duì) 回復(fù) 2023-10-22
  • 2 回答
  • 0 關(guān)注
  • 167 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)