【发布时间】:2021-10-12 17:55:57
【问题描述】:
这是我要转换的数组
Array
(
[0] => stdClass Object
(
[Item1] => 10/12/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[1] => stdClass Object
(
[Item1] => 10/11/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[2] => stdClass Object
(
[Item1] => 10/10/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[3] => stdClass Object
(
[Item1] => 10/09/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[4] => stdClass Object
(
[Item1] => 10/08/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[5] => stdClass Object
(
[Item1] => 10/07/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[6] => stdClass Object
(
[Item1] => 10/06/2021
[Item2] => Array
(
[0] => 3729195
[1] => 0
[2] => 0
[3] => 0
)
)
[7] => stdClass Object
(
[Item1] => 10/05/2021
[Item2] => Array
(
[0] => 7458390
[1] => 0
[2] => 0
[3] => 0
)
)
[8] => stdClass Object
(
[Item1] => 10/04/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[9] => stdClass Object
(
[Item1] => 10/03/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
)
我想要这样的数组输出 -
[{name:"", data:[0,0,0,0,0,0,3729195,7458390,0,0]},
{name:"", data:[0,0,0,0,0,0,0,0,0,0]},
{name:"", data:[0,0,0,0,0,0,0,0,0,0]},
{name:"", data:[0,0,0,0,0,0,0,0,0,0]}]
问题是[Item2][0]输入数组的第一个元素是每4行数据的第一个元素:[0],[Item2][1]的第二个元素是每个数据集的第二列所有 4 行的 data[1] 元素,我希望你明白我的意思。
日期应该像这样在另一个单独的列表中 -
['10/12/2021', '10/11/2021', '10/10/2021', '10/09/2021', '10/08/2021', '10/07/2021', '10/06/2021', '10/05/2021', '10/04/2021', '10/03/2021'],
我已经做了一些测试,这就是我目前所做的 -
$ChartDataNew = stripcslashes(trim(json_encode($jobs->charts_last10days), "\""));
$replace1 = str_replace('Item1', name, $ChartDataNew);
$replace2 = str_replace('Item2', data, $replace1);
print_r($replace2);
上面的代码给了我一个 JSON 对象的输出
[{"name":"10/12/2021","data":[0,0,0,0]},{"name":"10/11/2021","data":[0,0,0,0]},{"name":"10/10/2021","data":[0,0,0,0]},{"name":"10/09/2021","data":[0,0,0,0]},{"name":"10/08/2021","data":[0,0,0,0]},{"name":"10/07/2021","data":[0,0,0,0]},{"name":"10/06/2021","data":[3729195,0,0,0]},{"name":"10/05/2021","data":[7458390,0,0,0]},{"name":"10/04/2021","data":[0,0,0,0]},{"name":"10/03/2021","data":[0,0,0,0]}]
这个 JSON 看起来不错,但它不正确。首先,我想让上面给出的 JSON 输出没有“”,并且名称应该是名称:“”。当您看到我的 json 对象输出中的数据与输入数组中的日期不匹配时。
要获得我已经完成的日期列表,并且很好 -
$chartDataSeries = array();
$chartDateLabels = array();
$chartSeriesList = array();
foreach($jobs->charts_last10days as $chartData){
array_push($chartDateLabels, "'".$chartData->Item1."'");
array_push($chartDataSeries, $chartData->Item2);
}
$chartDateLabels1 = implode(', ', ($chartDateLabels));
echo ($chartDateLabels1);
它为我提供了日期列表的正确输出,请参阅
['10/12/2021', '10/11/2021', '10/10/2021', '10/09/2021', '10/08/2021', '10/07/2021', '10/06/2021', '10/05/2021', '10/04/2021', '10/03/2021']
请让我知道我做错了什么或帮助我如何做到这一点,可能是编写一些函数或任何其他方式来做到这一点。
提前致谢
【问题讨论】:
-
欢迎。您需要先执行appropriate amount of research 并自己进行一些尝试。如果您遇到特定的问题,请发布您的尝试,说明您遇到的问题以及当前发生的情况。我们在这里帮助您解决当前代码的具体问题,而不是为您编写所有代码(这里的正确答案需要我们这样做)
-
嗨,@MagnusEriksson 感谢您的回复,但我已经做了一些测试,看看我做了什么``` $ChartDataNew = stripcslashes(trim(json_encode($jobs->charts_last10days), "\"" )); $replace1 = str_replace('Item1', name, $ChartDataNew); $replace2 = str_replace('Item2', data, $replace1); print_r($replace2); ```
-
然后我得到了这个然后我得到了这个输出
[{"name":"10/12/2021","data":[0,0,0,0]},{"name":"10/11/2021","data":[0,0,0,0]},{"name":"10/10/2021","data":[0,0,0,0]},{"name":"10/09/2021","data":[0,0,0,0]},{"name":"10/08/2021","data":[0,0,0,0]},{"name":"10/07/2021","data":[0,0,0,0]},{"name":"10/06/2021","data":[3729195,0,0,0]},{"name":"10/05/2021","data":[7458390,0,0,0]},{"name":"10/04/2021","data":[0,0,0,0]},{"name":"10/03/2021","data":[0,0,0,0]}]但这不是正确的,所以这就是为什么我在这里寻求一些帮助来解决这个问题。 -
Edit 你的问题包括附加信息,而不是在 cmets 中传播。请阅读How to create a Minimal, Reproducible Example和how to ask