5

I am trying to iterate over this json file and filter out the unwanted elements I would like to split the result so I have have a list of Customers or a list of Suppliers json File:

{
  "$descriptor": "Test",
  "$resources": [
    {
      "$uuid": "281d393c-7c32-4640-aca2-c286f6467bb1",
      "$descriptor": "",
      "customerSupplierFlag": "Customer"
    },
    {
      "$uuid": "87a132a9-95d3-47fd-8667-77cca9c78436",
      "$descriptor": "",
      "customerSupplierFlag": "Customer"
    },
    {
      "$uuid": "8a75345c-73c7-4852-865c-f468c93c8306",
      "$descriptor": "",
      "customerSupplierFlag": "Supplier"
    },
    {
      "$uuid": "a2705e38-18d8-4669-a2fb-f18a87cd1cc6",
      "$descriptor": "",
      "customerSupplierFlag": "Supplier"
    }
  ]
}

for example

    {
      "$uuid": "281d393c-7c32-4640-aca2-c286f6467bb1",
      "$descriptor": "",
      "customerSupplierFlag": "Customer"
    },
    {
      "$uuid": "87a132a9-95d3-47fd-8667-77cca9c78436",
      "$descriptor": "",
      "customerSupplierFlag": "Customer"
    },

my php code is

$array = json_decode($result, true);


for ($i = 0; $i < count($array['$resources']); $i++){
    foreach ($array['$resources'][$i]['customerSupplierFlag'][Customer] as $item)
    {
        // do what you want
        echo $item['customerSupplierFlag' ]. '<br>';
        echo $item['$uuid'] . '<br>';
    }
}

I have been struggling with this for a few days now an appear to be going over the same articles and getting now were any suggestions would be appreciated.

3
  • You don't have $array['$resources'][$i]['customerSupplierFlag'][Customer] elements in your json. Only $array['$resources'][$i]['customerSupplierFlag'] Commented Aug 1, 2015 at 16:18
  • Customer is the is the value held in customerSupplierFlag filed that I would like to filter I believe it is a similar expression to ('//$resources[$uuid>=87a132a9-95d3-47fd-8667-77cca9c78436][customerSupplierFlag="Customer"]'); that is used in xpath Commented Aug 1, 2015 at 16:44
  • It's not xpath here, it's a plain array, received from JSON. Commented Aug 1, 2015 at 16:50

3 Answers 3

13
$data = file_get_contents('./your_json_data.txt');

$json = json_decode($data, 1);

$resources = $json['$resources'];

$suppliers = array();
$customers = array();

foreach ($resources as $rkey => $resource){

    if ($resource['customerSupplierFlag'] == 'Customer'){

        $customers[] = $resource;

    } else if ($resource['customerSupplierFlag'] == 'Supplier') {

        $suppliers[] = $resource;

    }

}

header('Content-Type: application/json');

echo json_encode($customers);
echo json_encode($suppliers);

die();
Sign up to request clarification or add additional context in comments.

1 Comment

depending on where the json data is coming from and how often it is updated, you might consider storing the filtered data in a json file itself, and then you can just pass-through the json directly through the webserver instead of loading up php and filtering the data on each request.
5
$array = json_decode($json, true);
$flag = "Supplier";
$resources = array_filter($array, function ($var) use ($flag) {
    return ($var['customerSupplierFlag'] == $flag);
});
print_r($resources);

4 Comments

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
Nothing complex about the code. It's quite straight forward. Just use two basic builtin PHP functions. Refer to PHP documentation: 1. json_decode at php.net/manual/en/function.json-decode.php 2. array_filter at php.net/manual/en/function.array-filter.php
The point is not whether or not code is complex or not, an explanation will help the OP and future visitors to your question to understand your solution. Answers shouldn't be about blindly copy/paste a piece of code and pray it works.
Thanks for adding this code, even if you don't have time for an explanation, I still am grateful.
1
$json_a = json_decode($string, true);
            
 foreach($json_a as $jsonDataKey => $jsonDataValue){
    foreach($jsonDataValue as $jsonArrayKey => $jsonArrayValue){
                print_r($jsonArrayValue['id']);
                print_r($jsonArrayValue['name']);
        }
          }
                        
            

Comments

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.