Home > other >  i need help for empty array in array_column
i need help for empty array in array_column

Time:02-05

I tried in many ways, but none is my specific problem, I tried to use what I know, but to no avail. I'm trying to use file_get_contents to get the json from 1 file.json on my server, and calculate a specific array, but after the "array_column" it returns an empty array.

<?php  
$json_url = "http://myserver/file.json"; 
$json = file_get_contents($json_url);

$data  = json_decode($json, true); 
$price = array_column($data, 'valor');

print_r($price);

//output = array() | empyty array

the $json return the correct json, in the $data return the corret decoded json, but in $price, return empty array()

the $json content of my json file in my server is similar to it:

{
"1228421731": [
    {
        "valor": 10,
        "datelimite": 1644024834,
        "date": 1643420034,
        "codigo": "ALYCGKU76S"
    }
],
"1111925985": [
    {
        "valor": 25,
        "datelimite": 1644066350,
        "date": 1643461550,
        "codigo": "HK8NQROGBW"
    }
],
"1413051871": [
    {
        "valor": 50,
        "datelimite": 1644084293,
        "date": 1643479493,
        "codigo": "4NGJ523J6H"
    }
],
"1209626299": [
    {
        "valor": 10,
        "datelimite": 1644091732,
        "date": 1643486932,
        "codigo": "W7LRGY3FHZ"
    }
],
"1803561706": [
    {
        "valor": 10,
        "datelimite": 1644257719,
        "date": 1643652919,
        "codigo": "GE8IDPZN9H"
    },
    {
        "valor": 19865091226,
        "datelimite": 1644288984,
        "date": 1643684184,
        "codigo": "F5Q9TPE43SWAFX"
    }
],
"1710879952": [
    {
        "valor": 19865091226,
        "datelimite": 1644274668,
        "date": 1643669868,
        "codigo": "P5M4E6RP1ZPEUR"
    }
],
"1907375762": [
    {
        "valor": 10,
        "datelimite": 1644377257,
        "date": 1643772457,
        "codigo": "E8OKPVCYVWRHUI"
    }
],
"1863764959": [
    {
        "valor": 25,
        "datelimite": 1644427955,
        "date": 1643823155,
        "codigo": "L78EZKJZJ93UYW"
    }
],
"1831713303": [
    {
        "valor": 10,
        "datelimite": 1644442109,
        "date": 1643837309,
        "codigo": "3J84VTS5FRS6OI"
    }
],
"5193759120": [
    {
        "valor": 10,
        "datelimite": 1644453308,
        "date": 1643848508,
        "codigo": "88942HFHZ5JJ56"
    }
],
"1785872541": [
    {
        "valor": 10,
        "datelimite": 1644504518,
        "date": 1643899718,
        "codigo": "LBML4O31RLC7DW"
    }
],
"1666986497": [
    {
        "valor": 1,
        "datelimite": 1644667829,
        "date": 1644063029,
        "codigo": "HXHPK6XLNBD89E"
    }
]

}

how can i use array_column for this json type? for the array 'valor'?

CodePudding user response:

You can try this

array_column(array_merge(...(json_decode($json,true))),"valor")

This will return an array with all the values of valor

CodePudding user response:

$data is in this format

Array
(
    [1228421731] => Array
        (
            [0] => Array
                (
                    [valor] => 10
                    [datelimite] => 1644024834
                    [date] => 1643420034
                    [codigo] => ALYCGKU76S
                )

        )

    [1111925985] => Array
        (
            [0] => Array
                (
                    [valor] => 25
                    [datelimite] => 1644066350
                    [date] => 1643461550
                    [codigo] => HK8NQROGBW
                )

        )

    [1413051871] => Array
        (
            [0] => Array
                (
                    [valor] => 50
                    [datelimite] => 1644084293
                    [date] => 1643479493
                    [codigo] => 4NGJ523J6H
                )

        )

    [1209626299] => Array
        (
            [0] => Array
                (
                    [valor] => 10
                    [datelimite] => 1644091732
                    [date] => 1643486932
                    [codigo] => W7LRGY3FHZ
                )

        )

    [1803561706] => Array
        (
            [0] => Array
                (
                    [valor] => 10
                    [datelimite] => 1644257719
                    [date] => 1643652919
                    [codigo] => GE8IDPZN9H
                )

            [1] => Array
                (
                    [valor] => 19865091226
                    [datelimite] => 1644288984
                    [date] => 1643684184
                    [codigo] => F5Q9TPE43SWAFX
                )

        )

    [1710879952] => Array
        (
            [0] => Array
                (
                    [valor] => 19865091226
                    [datelimite] => 1644274668
                    [date] => 1643669868
                    [codigo] => P5M4E6RP1ZPEUR
                )

        )

    [1907375762] => Array
        (
            [0] => Array
                (
                    [valor] => 10
                    [datelimite] => 1644377257
                    [date] => 1643772457
                    [codigo] => E8OKPVCYVWRHUI
                )

        )

    [1863764959] => Array
        (
            [0] => Array
                (
                    [valor] => 25
                    [datelimite] => 1644427955
                    [date] => 1643823155
                    [codigo] => L78EZKJZJ93UYW
                )

        )

    [1831713303] => Array
        (
            [0] => Array
                (
                    [valor] => 10
                    [datelimite] => 1644442109
                    [date] => 1643837309
                    [codigo] => 3J84VTS5FRS6OI
                )

        )

    [5193759120] => Array
        (
            [0] => Array
                (
                    [valor] => 10
                    [datelimite] => 1644453308
                    [date] => 1643848508
                    [codigo] => 88942HFHZ5JJ56
                )

        )

    [1785872541] => Array
        (
            [0] => Array
                (
                    [valor] => 10
                    [datelimite] => 1644504518
                    [date] => 1643899718
                    [codigo] => LBML4O31RLC7DW
                )

        )

    [1666986497] => Array
        (
            [0] => Array
                (
                    [valor] => 1
                    [datelimite] => 1644667829
                    [date] => 1644063029
                    [codigo] => HXHPK6XLNBD89E
                )

        )

)

so I think you can try to get the first array element in each array and then operate on those arrays, like this

$price = array_column(array_column($data, 0),'valor');

This will give the results

CodePudding user response:

If you want an array of all valor values:

$flatArray = array_reduce(
    json_decode($json, true),
    fn($carry, $item) => $carry = array_merge($carry, array_column($item, 'valor')),
    []
);

If you want an array grouped by valors belonging together:

$groupedArray = array_reduce(
    json_decode($json, true),
    fn($carry, $item) => $carry = array_merge($carry, [ array_column($item, 'valor') ]),
    [])
;
  •  Tags:  
  • Related