使用循环php在多维数组上添加项目

我有一个 json,我需要获取特定值并使用 foreach 循环插入到数组中。然后我将它转换回 json 以检查我是否得到相同的数组/json 格式或输出。但我无法让它发挥作用。有人能帮助我吗。谢谢!

这是源格式:

http://img1.mukewang.com/643a60890001763105220583.jpg

但这是我在 foreach 循环中生成的内容:

http://img1.mukewang.com/643a60970001d7b303360431.jpg

这是我的代码。


$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);


元芳怎么了
浏览 95回答 3
3回答

qq_遁去的一_1

我认为这就是您要寻找的......这也没有您的代码那么复杂。<?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);&nbsp;$options = $test_product->product->options;// Check whatever you like in this for eachforeach ($options as $option) {&nbsp; &nbsp; // Example&nbsp; &nbsp; switch ($option->name) {&nbsp; &nbsp; &nbsp; &nbsp; case 'Color':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'this is the color array';&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}$options_json = json_encode($options);var_dump($options_json);?>

月关宝盒

如果我没有误解你的要求,那么你只需要这样做就可以得到你想要的,<?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);&nbsp;$attributes2 = $test_product['product']['options'];$expected = ['options'=>$attributes2];echo json_encode($expected);?>演示: https: //3v4l.org/5r6WI

UYOU

您正在覆盖循环中的 name 键,您需要执行以下操作:foreach ($attributes2 as $attribute) {&nbsp; &nbsp; $data = [&nbsp; &nbsp; &nbsp; &nbsp; "name" => $attribute["name"],&nbsp; &nbsp; &nbsp; &nbsp; "values" => []&nbsp; &nbsp; ];&nbsp; &nbsp; foreach ($attribute['values'] as $option) {&nbsp; &nbsp; &nbsp; &nbsp; $data['values'][] = $option;&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; $options_arr[] = $data;}
打开App,查看更多内容
随时随地看视频慕课网APP