0

i have this Json output and i want to get specific values from it, this is the json :

[
    [
        [
            "a:1:{s:3:\"ALL\";d:100;}",
            "a:1:{s:3:\"ALL\";d:200;}",
            "a:1:{s:3:\"ALL\";d:89;}"
        ],
        [
            "a:1:{s:3:\"USD\";d:100;}"
        ],
        [
            "a:2:{s:3:\"ALL\";d:300;s:3:\"USD\";d:250;}"
        ],
        [
            "a:1:{s:3:\"ALL\";d:30;}",
            "a:2:{s:3:\"ALL\";d:100;s:3:\"USD\";d:200;}"
        ],
        [
            "a:1:{s:3:\"USD\";d:200;}"
        ]
    ]
]

So i want to get the first d value only which are the numbers, for the moment i tried something like this:

$values = explode('-',$this->currency);
        return $values[0];

But its not working, i know i have to use a foreach to get the rest but dont know how to get the value for one.

EDIT I did this code:

 $values = explode('-',$this->currency);

      foreach ($values as $val){
          $arr[] = unserialize($val);
      }
      return $arr;

And this is what i get

[
    [
        [
            {
                "ALL": 100
            },
            {
                "ALL": 200
            },
            {
                "ALL": 89
            }
        ],
        [
            {
                "USD": 100
            }
        ],
        [
            {
                "ALL": 300,
                "USD": 250
            }
        ],
        [
            {
                "ALL": 30
            },
            {
                "ALL": 100,
                "USD": 200
            }
        ],
        [
            {
                "USD": 200
            }
        ]
    ]
]

So right now the output is correct but i need to get specific values for example on the first array we have 3 ALL values 100, 200, 89 i want to make a loop that gives me separatly 100, 200, 89 so i can find their total sum. The rest for the other numbers

SECOND EDIT

If i just make

$values = explode('-',$this->currency);
        
      return $values;

i get this result:

[
    [
        [
            "a:1:{s:3:\"ALL\";d:100;}",
            "a:1:{s:3:\"ALL\";d:200;}",
            "a:1:{s:3:\"ALL\";d:89;}"
        ],
        [
            "a:1:{s:3:\"USD\";d:100;}"
        ],
        [
            "a:2:{s:3:\"ALL\";d:300;s:3:\"USD\";d:250;}"
        ],
        [
            "a:1:{s:3:\"ALL\";d:30;}",
            "a:2:{s:3:\"ALL\";d:100;s:3:\"USD\";d:200;}"
        ],
        [
            "a:1:{s:3:\"USD\";d:200;}"
        ]
    ]
]
laraCoder
  • 261
  • 15
  • Does this answer your question? [Get specific value from JSON response](https://stackoverflow.com/questions/37897943/get-specific-value-from-json-response) – Álvaro Feb 23 '23 at 08:33
  • Use [json\_​decode](https://www.php.net/manual/en/function.json-decode.php) to decode, then [unserialize](https://www.php.net/manual/en/function.unserialize.php) the value you want – brombeer Feb 23 '23 at 08:33
  • @brombeer i can not use `json_decode` because its array and gives me error i did straight `unserialize` and i managed to get only the numbers as i want but how can i fetch only 1 number, this is the Json result i get now : – laraCoder Feb 23 '23 at 09:17
  • "_i can not use json_decode because its array_" You mentioned twice that this is JSON - and it is JSON - so you can use `json_decode` on it, I don't see any problem with that. – brombeer Feb 23 '23 at 09:22
  • @brombeer well when i make json_decode($value) i get this error `Argument #1 ($json) must be of type string, array given in file ` and the code i have is this: ` $values = explode('-',$this->currency); return json_decode($values);` – laraCoder Feb 23 '23 at 09:24
  • @laraCoder What is your expected output? Please add your desired output. – NIKUNJ PATEL Feb 23 '23 at 09:24
  • 1
    We have no idea (since you didn't post) what `$this->currency` is or what it contains and why you'd need to `explode()` it. Did you `dd($values)`? – brombeer Feb 23 '23 at 09:26
  • @brombeer i added second edit – laraCoder Feb 23 '23 at 09:28
  • @brombeer as you can see i have the values formatted in that style so from all that i need to get only the currency and their values and find total sum for each small group example first group has 100,200,89 i need to find the sum to be 389 – laraCoder Feb 23 '23 at 09:29
  • @laraCoder Please add sample response in json format what you want instead of writing in text. We can't understand your desired output. – NIKUNJ PATEL Feb 23 '23 at 09:29
  • @NIKUNJPATEL the desired output is graphic in JS, what i need is for each of those sections example first section contains 3 values, `100` , `200`, `89` i need to find the sum of these 3 values which is `389` thats the desired output and put it in graphic Js – laraCoder Feb 23 '23 at 09:32
  • @laraCoder No, My mean that you need to add sample json array response. Like you have mentioned that `i get this result:` like that post the JSON array result. – NIKUNJ PATEL Feb 23 '23 at 09:35
  • @NIKUNJPATEL i did post everything do you want to make a chat to see the whole code from the query of the database to here? – laraCoder Feb 23 '23 at 09:53

1 Answers1

1

If you are using Laravel, better to make use of the collection. Suppose $contents is the array containing serialized values you can unserialize like as below.

$unserializedArray = collect($contents)
                        ->map(function($content){
                                /**
                                * Looping the inital array
                                */
                                return collect($content)
                                        ->map(function($data){
                                            /**
                                            * Looping the inner array
                                            */
                                            return collect($data)
                                                    ->map(function($seralized){
                                                        /**
                                                        * Unserializing the  
                                                        * seralized values 
                                                        */  
                                                        return unserialize($seralized);
                                                    });

                                        });
                            })->toArray();

You can write the same code mentioned above in short hand as below :

$unserializedArray = collect($content)
                        ->map(fn($data)=>collect($data)
                                ->map(fn($fArr)=>collect($fArr)
                                        ->map(fn($serialized)=>unserialize($serialized))))
                                            ->toArray();

Getting the value using the keys

$selectedKey = 'ALL';
$response    = collect($unserializedArray)
                 ->map(function($unserialized)use($selectedKey){
                     return collect($unserialized)
                              ->map(function($unserializeData)use($selectedKey){
                                 return collect($unserializeData)
                                            ->pluck($selectedKey);
                              })->collapse();
                 })->collapse()->filter()->values();                           

The $response is the collection of the $selectedKey so using it you can easily get sum or the array of values

 /**
  * Sum 
  */

  $sum = collect($response)->sum()

 /**
  * Array 
  */

  $responseArray = collect($response)->toArray()

You can also do all this as below as well :

        $selectedKey = 'ALL';
        $unserializedArray = collect($contents)
                                ->map(fn($content)=>
                                        collect($content)
                                            ->map(fn($serilizeArray) => 
                                                    collect($serilizeArray)
                                                        ->map(fn($serialized)=>unserialize($serialized))
                                                        ->pluck($selectedKey))
                                            ->collapse())
                                ->collapse();
        
        $sum   = collect($unserializedArray)->sum();
        
        $array = collect($unserializedArray)->toArray();
Jeybin George
  • 448
  • 2
  • 8