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

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

使用循環(huán)php在多維數(shù)組上添加項(xiàng)目

使用循環(huán)php在多維數(shù)組上添加項(xiàng)目

PHP
元芳怎么了 2023-04-15 14:28:26
我有一個(gè) json,我需要獲取特定值并使用 foreach 循環(huán)插入到數(shù)組中。然后我將它轉(zhuǎn)換回 json 以檢查我是否得到相同的數(shù)組/json 格式或輸出。但我無(wú)法讓它發(fā)揮作用。有人能幫助我嗎。謝謝!這是源格式:但這是我在 foreach 循環(huán)中生成的內(nèi)容:這是我的代碼。$test_json= '{ "product": { "title": "Burton Custom Freestyle 151", "body_html": "<strong>Good snowboard!</strong>", "vendor": "Burton", "product_type": "Snowboard", "variants": [ { "option1": "Blue", "option2": "155" }, { "option1": "Black", "option2": "159" } ], "options": [ { "name": "Color", "values": [ "Blue", "Black" ] }, { "name": "Size", "values": [ "155", "159" ] } ] } }';$test_product = json_decode($test_json, true); $attributes2 = $test_product['product']['options'];$options_arr = array();foreach ($attributes2 as $attribute) {$options_arr['name'] = $attribute['name'];    foreach ($attribute['values'] as $option) {        $options_arr['values'][] = $option;    }         }$options_json = json_encode($options_arr);var_dump($options_json);
查看完整描述

3 回答

?
qq_遁去的一_1

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

我認(rèn)為這就是您要尋找的......這也沒(méi)有您的代碼那么復(fù)雜。


<?php

$test_json= '{ "product": { "title": "Burton Custom Freestyle 151", "body_html": "<strong>Good snowboard!</strong>", "vendor": "Burton", "product_type": "Snowboard", "variants": [ { "option1": "Blue", "option2": "155" }, { "option1": "Black", "option2": "159" } ], "options": [ { "name": "Color", "values": [ "Blue", "Black" ] }, { "name": "Size", "values": [ "155", "159" ] } ] } }';

$test_product = json_decode($test_json); 


$options = $test_product->product->options;


// Check whatever you like in this for each

foreach ($options as $option) {


    // Example

    switch ($option->name) {

        case 'Color':

            echo 'this is the color array';

        break;

    }

}


$options_json = json_encode($options);

var_dump($options_json);


?>


查看完整回答
反對(duì) 回復(fù) 2023-04-15
?
月關(guān)寶盒

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

如果我沒(méi)有誤解你的要求,那么你只需要這樣做就可以得到你想要的,


<?php

$test_json= '{ "product": { "title": "Burton Custom Freestyle 151", "body_html": "<strong>Good snowboard!</strong>", "vendor": "Burton", "product_type": "Snowboard", "variants": [ { "option1": "Blue", "option2": "155" }, { "option1": "Black", "option2": "159" } ], "options": [ { "name": "Color", "values": [ "Blue", "Black" ] }, { "name": "Size", "values": [ "155", "159" ] } ] } }';

$test_product = json_decode($test_json, true); 

$attributes2 = $test_product['product']['options'];

$expected = ['options'=>$attributes2];

echo json_encode($expected);

?>

演示: https: //3v4l.org/5r6WI


查看完整回答
反對(duì) 回復(fù) 2023-04-15
?
UYOU

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

您正在覆蓋循環(huán)中的 name 鍵,您需要執(zhí)行以下操作:


foreach ($attributes2 as $attribute) {


    $data = [

        "name" => $attribute["name"],

        "values" => []

    ];



    foreach ($attribute['values'] as $option) {

        $data['values'][] = $option;

    }         


    $options_arr[] = $data;

}


查看完整回答
反對(duì) 回復(fù) 2023-04-15
  • 3 回答
  • 0 關(guān)注
  • 149 瀏覽

添加回答

舉報(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)