0

I've the following PHP code with nested structures

<?php


$theJson = <<<EOF
{
  "date": 1492804820000,
  "node1": [{
    "id": 57163,
    "node1_description": "The node1 description",
    "node2": [{
      "id": 57163,
      "node2_description": "The node2 description",
      "dipartments": [{
        "id": 57163,
        "dip_description": "The dipartment description",
        "colorCods": [{
                    "id": 1,
                    "description": "Red",
                    "numbers": {
                        "num1": 1,
            "num2": 23,
            "num3": 11
                    }
                }, {
          "id": 2,
                    "description": "Yellow",
                    "numbers": {
                        "num1": 3,
            "num2": 45,
            "num3": 33
                    }
                }, {
          "id": 3,
                    "description": "Green",
                    "numbers": {
                        "num1": 31,
            "num2": 453,
            "num3": 323
                    }
                }, {
          "id": 3,
                    "description": "White",
                    "numbers": {
                        "num1": 3,
            "num2": 4,
            "num3": 3
                    }
                }]
      },
      {
        "id": 57345,
        "dip_description": "The dipartment description 2",
        "colorCods": [{
          "id": 1,
          "description": "Red",
          "numbers": {
            "num1": 4,
            "num2": 243,
            "num3": 151
          }
        }, {
          "id": 2,
          "description": "Yellow",
          "numbers": {
            "num1": 33,
            "num2": 415,
            "num3": 3
          }
        }, {
          "id": 3,
          "description": "Green",
          "numbers": {
            "num1": 331,
            "num2": 43,
            "num3": 33
          }
        }, {
          "id": 3,
          "description": "White",
          "numbers": {
            "num1": 32,
            "num2": 42,
            "num3": 33
          }
        }]
      }
    ]
    }]
  }]
}
EOF;


  $theArray = json_decode($theJson, true);

  foreach ($theArray['node1'] as $node1) {
          echo "Node 1 description :".$node1['node1_description'];
          echo "\n";
          echo "\n";
                foreach ($node1['node2'] as $node2) {
                        echo "Node 2 description :".$node2['node2_description'];
                        echo "\n";
                        echo "\n";
                                foreach ($node2['dipartments'] as $dip) {
                                                echo "Dipartment description: ".$dip['dip_description'];
                                                echo "\n";
                                                echo "\n";
                                                foreach ($dip['colorCods'] as $colCod) {
                                                          echo "Cod: ".$colCod['description'];
                                                          echo " - ";
                                                                echo "Number: ".$colCod['numbers']['num1'];
                                                                echo "\n";
                                                                echo "\n";
                                                }
                                }
                }

  }

?>

I need to extract the "num*" values from my JSON.

My code works fine, but it seems that the nested "for" cycles might not be the most efficient way to extract the "num*" values from my JSON.

Alternatives or samples?

7
  • 1
    what about recursive function for parsing nested arrays? Commented Apr 26, 2017 at 20:05
  • In my original json I've more "dipartments" ... I'll update my code sample Commented Apr 26, 2017 at 20:08
  • 3
    Take a look at the array_walk_recursive() function. Commented Apr 26, 2017 at 20:08
  • @Mubashar Iqbal I've updated my code .. now is nearer at my original data Commented Apr 26, 2017 at 20:22
  • Do you need the array keys and values containing num ? Commented Apr 26, 2017 at 20:35

2 Answers 2

2

You could use array_walk_recursive and then filter for the keys whose values you want to collect:

$result = [];
array_walk_recursive($theArray, function ($value, $key) use (&$result) {
    if (preg_match('/^num[0-9]+$/', $key)) {
        $result[] = $value;
    }
});

print_r($result);

See it run on eval.in

Sign up to request clarification or add additional context in comments.

Comments

0

Use recommendations from this post. PHP multidimensional array search by value

3 Comments

Link only answers will get obsolete if the link goes 404, please avoid them.
@PedroLobito It's a link to this site
@Jaime The question can still be deleted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.