0

i am looking to get invoices from the below JSON in an seperate array.

[  
   {  
      "id":1,
      "name":"Demo Name",
      "email":"[email protected]",
      "mobile":"881730344",
      "mobilealt":"78693446777",
      "gender":"male",
      "address":"102a sdf",
      "description":"Super admin role and super admin permissions",
      "profileimage":"profileimage\/team1.jpg",
      "status":"active",
      "created_at":null,
      "updated_at":"2016-06-29 17:08:24",
      "deleted_at":null,
      "under_id":0,
      "assign_at":"2016-05-11 10:41:50",
      "children":[  
         {  
            "id":17,
            "under_id":1,
            "name":"Some Name",
            "children":[  
               {  
                  "id":35,
                  "under_id":17,
                  "name":"test sub user ",
                  "children":[  

                  ],
                  "invoices":[  

                  ]
               },
               {  
                  "id":36,
                  "under_id":17,
                  "name":"Harsh",
                  "children":[  

                  ],
                  "invoices":[  

                  ]
               }
            ],
            "invoices":[  

            ]
         },
         {  
            "id":18,
            "under_id":1,
            "name":"Demo Demo Sname",
            "children":[  

            ],
            "invoices":[  
               {  
                  "order_id":31,
                  "sum":161500,
                  "month":"May",
                  "user_id":18
               }
            ]
         },
         {  
            "id":20,
            "under_id":1,
            "name":"Divya",
            "children":[  

            ],
            "invoices":[  

            ]
         },
         {  
            "id":25,
            "under_id":1,
            "name":"Dhiraj",
            "children":[  
               {  
                  "id":19,
                  "under_id":25,
                  "name":"New user",
                  "children":[  
                     {  
                        "id":33,
                        "under_id":19,
                        "name":"Demo Sub Sub user",
                        "children":[  

                        ],
                        "invoices":[  

                        ]
                     }
                  ],
                  "invoices":[  
                     {  
                        "order_id":32,
                        "sum":40000,
                        "month":"July",
                        "user_id":19
                     }
                  ]
               }
            ],
            "invoices":[  

            ]
         }
      ]
   }
]

I am thinking of recursive loop. But i tried the below method, which do not seems to be working.

public function take_invoices($array){
      static $arrinvoices = [];
      foreach($array as $item){
          if(count($item->invoice) > 0){
            dd($item->invoice);              //------------->Laravel function to die and dump. its always empty
              $arrinvoices[] = $item->invoice;
          }
          if(count($item->children) > 0){
            $this->take_invoices($item->children);
          }
      }

    return $arrinvoices;
  }

Thank you! (in advance!)

3
  • there are many empty "invoices", should they be omitted? Commented Jun 30, 2016 at 7:07
  • you have several invoices... Which one you need?? Commented Jun 30, 2016 at 7:07
  • Yes, i am trying to skip the empty once. Commented Jun 30, 2016 at 7:07

1 Answer 1

2

The solution using preg_match_all and preg_replace functions:

// $json_data - is your initial json string
// the next step takes all non-empty 'invoices'
preg_match_all("/\"invoices\":\[([^]]+)\]/", preg_replace("/\f|\v|\h|\t|\s+/", "", $json_data), $matches);
$invoices = [];
if (!empty($matches) && isset($matches[1])) {
    $invoices = json_decode("[" . implode(",", $matches[1]) . "]", true);
}

print_r($invoices);

The output:

Array
(
    [0] => Array
        (
            [order_id] => 31
            [sum] => 161500
            [month] => May
            [user_id] => 18
        )

    [1] => Array
        (
            [order_id] => 32
            [sum] => 40000
            [month] => July
            [user_id] => 19
        )
)
Sign up to request clarification or add additional context in comments.

6 Comments

I am getting the invoices, but now when i am trying to get the children then its not displaying children.
@user3201500, you have asked about to get invoices. As invoices are independent items in that structure, but children items are items with unknown nested levels
Yes actually, i am trying to create a global function to get any value from this json object. So it can easily be fetched.
and how should look the expected result when searched for children?
Each children will come as in array, same as invoices.
|

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.