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

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

问题描述

我有用于呈现使用PHPGraphLib的曲线图的阵列。我可以使这项工作很好,但只能用硬codeD值。

我得到可能的语法错误从Netbeans的警告。

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

  //创建新的图形对象,并添加图形数据
$图=新PHPGraphLib(650400);
$数据=阵列(00:00=> -9,
                        00:15=> -8,
                        00:30=> -3.5,
                        0点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($的数据,03:15=> 16); //这是我不能获得工作的一部分//绘图数据
$ graph-> addData($的数据);


解决方案

到一个新的元素添加到一个关联数组的语法是:

  $数据[03:15] = 16;

array_push 用于与价值观,不相关联的元素。它通常只与具有数字指标,而不是关联数组,因为它通过在阵列中加入1到最高的数字索引生成密钥的阵列中。

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.

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);

解决方案

The syntax to add a new element to an associative array is:

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

array_push is used with values, not associative elements. It's normally used only with arrays that have numeric indexes, not associative arrays, as it generates the key by adding 1 to the highest numeric index in the array.

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

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