在PHP中重构多维数组以获得JSON

时间:2021-09-08 21:28:56

I'm trying to restructure a multidimensional array but i can't manage it. I'm not very good with array's... So I would like to request your assistance please !

我正在尝试重组一个多维数组,但我无法管理它。我不太喜欢阵列...所以我想请求你的帮助!

Here is my actual array :

这是我的实际数组:

$jsonArray = Array
(
[0] => Array
    (
        [category] => Array
            (
                [label] => avr 2016
            )

        [data] => Array
            (
                [value] => 39
            )

    )

[1] => Array
    (
        [category] => Array
            (
                [label] => mai 2016
            )

        [data] => Array
            (
                [value] => 335
            )

    )

[2] => Array
    (
        [category] => Array
            (
                [label] => jun 2016
            )

        [data] => Array
            (
                [value] => 206
            )

    )

)

Then i use json_encode to combine it with AJAX :

然后我用json_encode将它与AJAX结合起来:

echo json_encode($jsonArray, JSON_UNESCAPED_UNICODE);

And the result is :

结果是:

[{"category":{"label":"avr 2016"},"data":{"value":"39"}},{"category":{"label":"mai 2016"},"data":{"value":"335"}},{"category":{"label":"jun 2016"},"data":{"value":"206"}}]

I'm trying to use FusionCharts example from here !

我正试图从这里使用FusionCharts示例!

As you see i need to create structured arrays like this :

如你所见,我需要创建像这样的结构化数组:

"category": [
                    { "label": "Jan 2012" },
                    { "label": "Feb 2012" },
                    { "label": "Mar 2012" },
                    { "label": "Apr 2012" },

"data": [
                    { "value": "27400" },
                    { "value": "29800" },
                    { "value": "25800" },
                    { "value": "26800" },

Then in my AJAX code i set this for the charts :

然后在我的AJAX代码中我为图表设置了这个:

var apiChart = new FusionCharts({
                type: 'scrollline2d',
                renderAt: 'chart-totalAnnee',
                width: '550',
                height: '350',
                dataFormat: 'json',
                dataSource: {
                    "chart": chartProperties,
                    "categories": arrayNeedle1,
                    "dataset": arrayNeedle2
                }
            });

My final request is how i can make those arrays arrayNeedle1 & arrayNeedle2, please ?

我的最后一个请求是如何制作那些数组arrayNeedle1和arrayNeedle2,好吗?

Thank you in advance for your help and sorry for my poor skills in arrays !

提前感谢您的帮助,对不起我在阵列中的糟糕技能!

Best regards.

1 个解决方案

#1


1  

As you have multidimensional nested arrays, restructuring it in place would be overcomplicated way, instead you may create a new array with needed structure using the following approach:

由于您具有多维嵌套数组,因此在适当的位置重构它将会过于复杂,而您可以使用以下方法创建具有所需结构的新数组:

// supposing $arr is your initial array
$new_arr = ["category" => [], "data" => []];  // base structure
foreach ($arr as $item) {
    $new_arr["category"][] = ["label" => $item["category"]["label"]];
    $new_arr["data"][] = ["value" => $item["data"]["value"]];
}

echo json_encode($new_arr, JSON_PRETTY_PRINT);

The output:

{
    "category": [
        {
            "label": "avr 2016"
        },
        {
            "label": "mai 2016"
        },
        {
            "label": "jun 2016"
        }
    ],
    "data": [
        {
            "value": "39"
        },
        {
            "value": "335"
        },
        {
            "value": "206"
        }
    ]
}

#1


1  

As you have multidimensional nested arrays, restructuring it in place would be overcomplicated way, instead you may create a new array with needed structure using the following approach:

由于您具有多维嵌套数组,因此在适当的位置重构它将会过于复杂,而您可以使用以下方法创建具有所需结构的新数组:

// supposing $arr is your initial array
$new_arr = ["category" => [], "data" => []];  // base structure
foreach ($arr as $item) {
    $new_arr["category"][] = ["label" => $item["category"]["label"]];
    $new_arr["data"][] = ["value" => $item["data"]["value"]];
}

echo json_encode($new_arr, JSON_PRETTY_PRINT);

The output:

{
    "category": [
        {
            "label": "avr 2016"
        },
        {
            "label": "mai 2016"
        },
        {
            "label": "jun 2016"
        }
    ],
    "data": [
        {
            "value": "39"
        },
        {
            "value": "335"
        },
        {
            "value": "206"
        }
    ]
}