使用JSON代码获取多维数组的特定值

时间:2019-06-14 22:13:24

标签: php arrays json multidimensional-array

我知道之前在stackoverflow上曾问过这个问题,但我仍然遇到困难。

我想知道如何从多维数组中获取选定的值。 插件的简化代码和原始数组:

$options_label = 

Array (
[0] => Array   (
        [0] => Array (
                [0] => 17
                [1] => [{"id":"1","label":"350 cm x 250 cm"},{"id":"2","label":"400 cm x 250 cm"},{"id":"3","label":"450 cm x 250 cm"}]
                [2] => Array  (
                     ...
                    )
            )

        [1] => Array  (
                [0] => 5
                [1] => [{"id":"3","label":"Rechts"},{"id":"4","label":"Links"}]
                [2] => Array  (
                      ...                          
                    )
            )  
    )

[1] => Array
    (
        [0] => Array  (
                [0] => 11
                [1] => [{"id":"1","label":"300 cm x 250 cm"},{"id":"2","label":"350 cm x 250 cm"}]
                [2] => Array  (
                       ...
                    )                   
            )            
    )
)

我想创建一个使用[1](与json相关的代码)中的值的新数组,因此新数组应如下所示:

$new_array(

    Array (
        [0] => array (
            [0] => array   (
                [0] => array (
                        [id] => 1
                        [label] => 350 cm x 250 cm
                    )
                [1] => array (
                        [id] => 2
                        [label] => 400 cm x 250 cm
                    )
                [2] => array (
                        [id] => 3
                        [label] => 450 cm x 250 cm
                    )
            )
            [1] => array (
                [0] => Array  (
                    [id] => 3
                    [label] => Rechts
                )
                [1] => Array  (
                    [id] => 4
                    [label] => Links
                )
            )

        )

        [1] => array (      
            [0] => array   (
                [0] => array (
                        [id] => 1
                        [label] => 300 cm x 250 cm
                    )
                [1] => array (
                        [id] => 2
                        [label] => 350 cm x 250 cm
                    )       
            )
        )       
)

非常感谢任何指导。

2 个答案:

答案 0 :(得分:0)

我猜测这里似乎有JSON字符串,我们希望将其替换为一个数组,我们可能想要类似以下内容:

$options_label = array(
    "0" => array(
        "0" => array(
            "0" => 17,
            "1" => '[{"id":"1","label":"350 cm x 250 cm"},{"id":"2","label":"400 cm x 250 cm"},{"id":"3","label":"450 cm x 250 cm"}]',
            "2" => array(),
        ),

        "1" => array(
            "0" => 5,
            "1" => '[{"id":"3","label":"Rechts"},{"id":"4","label":"Links"}]',
            "2" => array(),
        ),
    ),

    "1" => array
    (
        "0" => array(
            "0" => 11,
            "1" => '[{"id":"1","label":"300 cm x 250 cm"},{"id":"2","label":"350 cm x 250 cm"}]',
            "2" => array(),
        ),
    ),
);

$new_options_label = array();

foreach ($options_label as $key1 => $value1) {
    foreach ($value1 as $key2 => $value2) {
        foreach ($value2 as $key3 => $value3) {
            if (json_decode($value3, true)) {
                $options_label[$key1][$key2][$key3] = json_decode($value3, true);
            }
        }
    }
}
var_dump($options_label);

输出

array(2) {
  [0]=>
  array(2) {
    [0]=>
    array(3) {
      [0]=>
      int(17)
      [1]=>
      array(3) {
        [0]=>
        array(2) {
          ["id"]=>
          string(1) "1"
          ["label"]=>
          string(15) "350 cm x 250 cm"
        }
        [1]=>
        array(2) {
          ["id"]=>
          string(1) "2"
          ["label"]=>
          string(15) "400 cm x 250 cm"
        }
        [2]=>
        array(2) {
          ["id"]=>
          string(1) "3"
          ["label"]=>
          string(15) "450 cm x 250 cm"
        }
      }
      [2]=>
      array(0) {
      }
    }
    [1]=>
    array(3) {
      [0]=>
      int(5)
      [1]=>
      array(2) {
        [0]=>
        array(2) {
          ["id"]=>
          string(1) "3"
          ["label"]=>
          string(6) "Rechts"
        }
        [1]=>
        array(2) {
          ["id"]=>
          string(1) "4"
          ["label"]=>
          string(5) "Links"
        }
      }
      [2]=>
      array(0) {
      }
    }
  }
  [1]=>
  array(1) {
    [0]=>
    array(3) {
      [0]=>
      int(11)
      [1]=>
      array(2) {
        [0]=>
        array(2) {
          ["id"]=>
          string(1) "1"
          ["label"]=>
          string(15) "300 cm x 250 cm"
        }
        [1]=>
        array(2) {
          ["id"]=>
          string(1) "2"
          ["label"]=>
          string(15) "350 cm x 250 cm"
        }
      }
      [2]=>
      array(0) {
      }
    }
  }
}

答案 1 :(得分:0)

代码有点复杂,但是我还是要发布它。

global $wpdb;

$table = $wpdb->base_prefix . 'woopricesim_simulations';

$existing = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $table . " WHERE order_id = %d", $order->get_order_number() ), ARRAY_A );

$simulation_data = json_decode($existing['simulation_data'], true);
$simulators      = json_decode($existing['simulators'], true);

$fields = array();

foreach($simulation_data as $key => $value) {                
    $fields[] = $value['simulator_fields_data'];                
}

此输出为

fields:Array
(
    [0] => Array
        (
            [aws_price_calc_1] => 2
            [aws_price_calc_2] => 1
            [aws_price_calc_3] => 2
        )

    [1] => Array
        (
            [aws_price_calc_12] => 1
            [aws_price_calc_2] => 3
            [aws_price_calc_14] => 1
            [aws_price_calc_13] => 1
            [aws_price_calc_6] => 
            [aws_price_calc_18] => 1
            [aws_price_calc_17] => 1
            [aws_price_calc_15] => 1
        )

)

我需要将其转换为名称(标签)并选择所选内容。

$labels = array();
$data   = array();              

$new_options    = array();  
$final          = array();  

foreach( $fields as $field_key => $field_value ){

        $new_labels     = array();                                          
        $new_data       = array();  

            foreach( $field_value as $key => $value ){

                $field_id = str_replace("aws_price_calc_", "", $key);

                $field_table = $wpdb->base_prefix . 'woopricesim_fields';
                $field_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $field_table . " WHERE id = %d", $field_id ), ARRAY_A );

                //$new_data[] = json_decode( $field_data['options'], true );

                $new_data[] = $field_data['options'];
                $new_labels[] = $field_data['label'];                                                   

            }   

        $labels[]   = $new_labels;
        $data[]     = $new_data;

}

标签工作正常。

labels:Array
(
    [0] => Array
        (
            [0] => Size
            [1] => Operation Side
            [2] => Operation Type
        )

    [1] => Array
        (
            [0] => Size
            [1] => Operation Side
            [2] => Operation Type
            [3] => Color
            [4] => Fabric Color
            [5] => Strips
            [6] => Sensor
            [7] => Delivery
        )

)

数据现在看起来像这样。

data:Array
(
    [0] => Array
        (
            [0] => {"items_list_id":"17","picklist_items":"[{\"id\":\"1\",\"label\":\"350 cm x 250 cm\",\"value\":\"0\",\"order_details\":\"\",\"tooltip_message\":\"\",\"tooltip_position\":\"none\",\"default_option\":\"1\"},{\"id\":\"2\",\"label\":\"400 cm x 250 cm\",\"value\":\"99\",\"order_details\":\"\",\"tooltip_message\":\"\",\"tooltip_position\":\"none\",\"default_option\":\"0\"}] 
            [2] => {"items_list_id":"7","picklist_items":"[{\"id\":\"4\",\"label\":\"Electric\",\"value\":\"0\",\"order_details\":\"\",\"tooltip_message\":\"\",\"tooltip_position\":\"none\",\"default_option\":\"0\"},{\"id\":\"5\",\"label\":\"Motor\",\"value\":\"95\",\"order_details\":\"\",\"tooltip_message\":\"\",\"tooltip_position\":\"none\",\"default_option\":\"0\"},{\"id\":\"6\",\"label\":\"RTS + sensor\",\"value\":\"210\",\"}]
         )

    [1] => Array
        (
            [0] => {...}
            [1] => {...}
            [2] => {...}
            [3] => {...}
            [4] => {...}
            [5] => {...}
            [6] => {...}
            [7] => {...}
        )
)

从现在开始,我需要创建一个新数组,其中仅包含来自picklist_items的值,也只有来自idlabel的值。 我使用的简单的foreach,可以返回Undefined index: picklistid/label

我的最终目标是用标签名称和所选内容替换$fields。 例如。 [aws_price_calc_1] => 2将成为Size => 400 cm x 250 cm