首页 > 解决方案 > 如何从 json 数组中获取所有定价值

问题描述

如何从 JSON 数组中获取所有定价值?我需要拆分两个段 tld(com、in、in.net 等)和相应的 tld 定价值我试图从数组中获取值,但 com、in 等这些是关键值。

我的 JSON 数组

Array
(
[result] => success
[pricing] => Array
    (
        [com] => Array
            (
                [categories] => Array
                    (
                        [0] => gTLD
                        [1] => Popular
                    )

                [addons] => Array
                    (
                        [dns] => 1
                        [email] => 1
                        [idprotect] => 1
                    )

                [group] => sale
                [register] => Array
                    (
                        [1] => 637.70
                    )

                [transfer] => Array
                    (
                        [1] => 637.70
                    )

                [renew] => Array
                    (
                        [1] => 637.70
                    )

               )

        [in] => Array
            (
                [categories] => Array
                    (
                        [0] => ccTLD
                        [1] => Geography
                    )

                [addons] => Array
                    (
                        [dns] => 1
                        [email] => 1
                        [idprotect] => 1
                    )

                [group] => sale
                [register] => Array
                    (
                        [1] => 1014.67
                    )

                [transfer] => Array
                    (
                        [1] => 1014.67
                    )

                [renew] => Array
                    (
                        [1] => 1014.67
                    )

            )

        [info] => Array
            (
                [categories] => Array
                    (
                        [0] => gTLD
                        [1] => Popular
                    )

                [addons] => Array
                    (
                        [dns] => 1
                        [email] => 1
                        [idprotect] => 1
                    )

                [group] => sale
                [register] => Array
                    (
                        [1] => 200.00
                    )

                [transfer] => Array
                    (
                        [1] => 200.00
                    )

                [renew] => Array
                    (
                        [1] => 200.00
                    )

            )

        [net] => Array
            (
                [categories] => Array
                    (
                        [0] => gTLD
                        [1] => Popular
                    )

                [addons] => Array
                    (
                        [dns] => 1
                        [email] => 1
                        [idprotect] => 1
                    )

                [group] => sale
                [register] => Array
                    (
                        [1] => 829.76
                    )

                [transfer] => Array
                    (
                        [1] => 829.76
                    )

                [renew] => Array
                    (
                        [1] => 829.76
                    )

            )

        [biz] => Array
            (
                [categories] => Array
                    (
                        [0] => gTLD
                        [1] => Popular
                    )

                [addons] => Array
                    (
                        [dns] => 1
                        [email] => 1
                        [idprotect] => 1
                    )

                [group] => sale
                [register] => Array
                    (
                        [1] => 878.33
                    )

                [transfer] => Array
                    (
                        [1] => 878.33
                    )

                [renew] => Array
                    (
                        [1] => 878.33
                    )

            )

        [org] => Array
            (
                [categories] => Array
                    (
                        [0] => gTLD
                        [1] => Popular
                    )

                [addons] => Array
                    (
                        [dns] => 1
                        [email] => 1
                        [idprotect] => 1
                    )

                [group] => sale
                [register] => Array
                    (
                        [1] => 939.45
                    )

                [transfer] => Array
                    (
                        [1] => 939.45
                    )

                [renew] => Array
                    (
                        [1] => 939.45
                    )

            )

        [asia] => Array
            (
                [categories] => Array
                    (
                        [0] => gTLD
                        [1] => ccTLD
                        [2] => Geography
                        [3] => Popular
                    )

                [addons] => Array
                    (
                        [dns] => 1
                        [email] => 1
                        [idprotect] => 1
                    )

                [group] => sale
                [register] => Array
                    (
                        [1] => 1527.88
                    )

                [transfer] => Array
                    (
                        [1] => 1527.88
                    )

                [renew] => Array
                    (
                        [1] => 1527.88
                    )

            )

        [co.uk] => Array
            (
                [categories] => Array
                    (
                        [0] => ccTLD
                        [1] => Geography
                    )

                [addons] => Array
                    (
                        [dns] => 1
                        [email] => 1
                        [idprotect] => 1
                    )

                [group] => sale
                [register] => Array
                    (
                        [1] => 602.53
                    )

                [transfer] => Array
                    (
                        [1] => 602.53
                    )

                [renew] => Array
                    (
                        [1] => 602.53
                    )

            )

        [in.net] => Array
            (
                [categories] => Array
                    (
                        [0] => Other
                    )

                [addons] => Array
                    (
                        [dns] => 1
                        [email] => 1
                        [idprotect] => 1
                    )

                [group] => sale
                [register] => Array
                    (
                        [1] => 100.00
                    )

                [transfer] => Array
                    (
                        [1] => 100.00
                    )

                [renew] => Array
                    (
                        [1] => 100.00
                    )

            )

        [uk] => Array
            (
                [categories] => Array
                    (
                        [0] => ccTLD
                        [1] => Geographic
                        [2] => Popular
                    )

                [addons] => Array
                    (
                        [dns] => 1
                        [email] => 1
                        [idprotect] => 1
                    )

                [group] => sale
                [register] => Array
                    (
                        [1] => 200.00
                    )

                [transfer] => Array
                    (
                        [1] => 300.00
                    )

                [renew] => Array
                    (
                        [1] => 400.00
                    )

            )

    )

 )

如何拆分数组并从上述 JSON 数组中获取传输值?

标签: phparraysjsonlaravel

解决方案


如果我没有误解您的问题,那么这就是您需要通过简单foreach()循环来获取具有相应价格的tld

$expected = [];
foreach($result['pricing'] as $tld=>$array){
   // this is the register value, you can change it for transfer or renew if you wish
   $expected[$tld] = $array['register'][1]; 
}
print_r($expected);

推荐阅读