【问题标题】:Echo part of two dimentional json array二维json数组的回显部分
【发布时间】:2023-03-25 20:15:01
【问题描述】:

我有一个嵌套的 json 数组

$sdata['my_server']['server_ips'];

谁的结果是这样的

{ "ip_address": [ { "access": "private", "address": "x.x.x.x", "family": "IPv4" }, { "access": "public", "address": "xxxx:xxxx:xxxx:xxx:xxxx:xxxx:xxxx:xxxx", "family": "IPv6" }, { "access": "public", "address": "xxx.xxx.xxx.xxx", "family": "IPv4", "part_of_plan": "yes" } ] }

我只想在访问公共部分显示ipv4 ip地址的值是指这个

"address": "xxx.xxx.xxx.xxx"

有什么想法吗?谢谢

【问题讨论】:

  • 您的具体问题是什么,您尝试过什么?

标签: php arrays json jsondecoder


【解决方案1】:

您可以将array_filterjson_decode 一起使用

 $r = array_filter(json_decode($json, true)['ip_address'], function($v){
  return ($v['family'] === 'IPv4' && $v['access'] === 'public');
 });

工作示例:- https://3v4l.org/k0M0V

print_r($r) 会给你一个数组,你可以使用array_shift 而不是使用$r['address']。如果有多个值,则使用 array_column by address 并使用 implode

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-28
    • 2022-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 2018-12-25
    • 2015-08-17
    • 2016-12-27
    相关资源
    最近更新 更多