PHP - 将元素附加到数组 [英] PHP - Append elements to array

查看:35
本文介绍了PHP - 将元素附加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,用于使用 PHPGraphLib 呈现图形.我可以使这项工作正常进行,但只能使用硬编码值.

I have an array that is used to render a graph using PHPGraphLib. I can make this work fine, but only with hard coded values.

我收到来自 Netbeans 的可能的语法错误"警告.

I get "POSSIBLE syntax error" warning from Netbeans.

向这种类型的数组追加元素的正确方法是什么?

What is the correct way of appending elements to this type of array?

//Create new graph object and add graph data
$graph = new PHPGraphLib(650,400);
$data = array           ("00:00" => -9,
                        "00:15" => -8,
                        "00:30" => -3.5,
                        "00:45" => 5, 
                        "01:00" => 11,
                        "01:15" => 12.5,
                        "01:30" => 10.5,
                        "01:45" => 11,
                        "02:00" => 2,
                        "02:15" => -2,
                        "02:30" => 2,
                        "02:45" => -2,
                        "03:00" => 14);

array_push($data, "03:15" => 16);  //This is the part I cannot get to work

//Plot data
$graph->addData($data);

推荐答案

将您的 array_push(...) 替换为:

$data['03:15'] = 16;

使用 array_push() 您只能向数组添加值.不是你想要的键.

With array_push() you can only add values to arrays. Not keys as you want.

这篇关于PHP - 将元素附加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆