skip to Main Content

I want to search inside a JSON file with the following format:

{
  "success": true,
  "msg": "",
  "obj": [
    {
      "id": 1,
      "up": 546636462,
      "down": 4172830061,
      "total": 53687091200,
      "name": "حامد ",
      "enable": true,
      "expiryTime": 1667049201686,
      "listen": "",
      "dork": 23050,
      "net": "girltik",
      "settings": "{n "clients": [n {n "id": "f8c36812-11e0-47c4-9880-2c8ff9310c49",n "alterId": 0n }n ],n "disableInsecureEncryption": falsen}",
      "streamSettings": "{n "network": "ws",n "security": "none",n "wsSettings": {n "path": "/",n "headers": {}n }n}",
      "tag": "inbound-23050",
      "sniffing": "{n "enabled": true,n "destOverride": [n "http",n "tls"n ]n}"
    },
    {
      "id": 2,
      "up": 25559864,
      "down": 630133850,
      "total": 5368709120,
      "remark": "احمد ",
      "enable": true,
      "expiryTime": 1667051159682,
      "listen": "",
      "dork": 36606,
      "net": "girltik",
      "settings": "{n "clients": [n {n "id": "902b0800-6bbd-4874-f7f8-980deb8d37e8",n "alterId": 0n }n ],n "disableInsecureEncryption": falsen}",
      "streamSettings": "{n "network": "ws",n "security": "none",n "wsSettings": {n "path": "/",n "headers": {}n }n}",
      "tag": "inbound-36606",
      "sniffing": "{n "enabled": true,n "destOverride": [n "http",n "tls"n ]n}"
    }
  ]
}

I want to filter it by word f8c36812-11e0-47c4-9880-2c8ff9310c49 in settings property, so that the result array contains only the matching objects:

{
  "success": true,
  "msg": "",
  "obj": [
    {
      "id": 1,
      "up": 546636462,
      "down": 4172830061,
      "total": 53687091200,
      "remark": "حامد پاشائی",
      "enable": true,
      "expiryTime": 1667049201686,
      "listen": "",
      "port": 23050,
      "protocol": "vmess",
      "settings": "{n "clients": [n {n "id": "f8c36812-11e0-47c4-9880-2c8ff9310c49",n "alterId": 0n }n ],n "disableInsecureEncryption": falsen}",
      "streamSettings": "{n "network": "ws",n "security": "none",n "wsSettings": {n "path": "/",n "headers": {}n }n}",
      "tag": "inbound-23050",
      "sniffing": "{n "enabled": true,n "destOverride": [n "http",n "tls"n ]n}"
    }
  ]
}

How can I filter an array from a JSON file using PHP?

2

Answers


  1. You can use the function json_decode() (https://www.php.net/manual/fr/function.json-decode.php). You will have an array that you can explore.

    With this array, you can use the function strpos() (https://www.php.net/manual/fr/function.strpos.php) on the column "setings"
    This function will gives you false if the word given is not present. Otherwise it will gives you his position. With this information, you will be able to know is the word is existing.

    EDIT :

    You can also use the function strpos() directly with the entire json.

    Login or Signup to reply.
  2. there was a syntax error with your json content structure
    this is correctly version json and needed script

    $str = '{
      "success": true,
      "msg": "",
      "obj": [
        {
          "id": 1,
          "up": 546636462,
          "down": 4172830061,
          "total": 53687091200,
          "name": "حامد ",
          "enable": true,
          "expiryTime": 1667049201686,
          "listen": "",
          "dork": 23050,
          "net": "girltik",
          "settings": {"clients": [{ "id": "f8c36812-11e0-47c4-9880-2c8ff9310c49","alterId": 0 } ], "disableInsecureEncryption": false},
          "streamSettings": { "network": "ws", "security": "none", "wsSettings": { "path": "/", "headers": {} }},
          "tag": "inbound-23050",
          "sniffing": { "enabled": true, "destOverride": [ "http", "tls" ]}
        },
        {
          "id": 2,
          "up": 25559864,
          "down": 630133850,
          "total": 5368709120,
          "remark": "احمد ",
          "enable": true,
          "expiryTime": 1667051159682,
          "listen": "",
          "dork": 36606,
          "net": "girltik",
          "settings": { "clients": [ { "id": "902b0800-6bbd-4874-f7f8-980deb8d37e8","alterId": 0 } ], "disableInsecureEncryption": false},
          "streamSettings": { "network": "ws", "security": "none", "wsSettings": { "path": "/", "headers": {} }},
          "tag": "inbound-36606",
          "sniffing": { "enabled": true, "destOverride": [ "http", "tls" ]}
        }
      ]
    }';
    
    
    $key = "f8c36812-11e0-47c4-9880-2c8ff9310c49";
    
    $json = json_decode($str,true);
    
    foreach ($json['obj'] as $item) {
       
        foreach($item['settings']['clients'] as $setting){
                if($setting['id']==$key) {
                        print_r($item);
                    }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search