您好我必须创建以下数组
{{x: 140224, y: 50.1},{x: 140225, y: 50.0},{x: 140226, y: 50.0},{x: 140227, y: 50.0},{x: 140228, y: 50.1},{x: 140229, y: 50.0},{x: 140230, y: 50.0},{x: 140231, y: 50.0},{x: 140232, y: 50.0},{x: 140233, y: 50.0},{x: 140234, y: 50.0},{x: 140235, y: 50.0},{x: 140236, y: 50.0},{x: 140237, y: 50.0},{x: 140238, y: 50.0},{x: 140239, y: 50.0},{x: 140240, y: 50.0},{x: 140241, y: 50.0},{x: 140242, y: 50.0},{x: 140243, y: 50.0},{x: 140244, y: 50.0}}
没有第一个{element [0]和last}所以它应该看起来像这个
{x: 140224, y: 50.1},{x: 140225, y: 50.0},{x: 140226, y: 50.0},{x: 140227, y: 50.0},{x: 140228, y: 50.1},{x: 140229, y: 50.0},{x: 140230, y: 50.0},{x: 140231, y: 50.0},{x: 140232, y: 50.0},{x: 140233, y: 50.0},{x: 140234, y: 50.0},{x: 140235, y: 50.0},{x: 140236, y: 50.0},{x: 140237, y: 50.0},{x: 140238, y: 50.0},{x: 140239, y: 50.0},{x: 140240, y: 50.0},{x: 140241, y: 50.0},{x: 140242, y: 50.0},{x: 140243, y: 50.0},{x: 140244, y: 50.0}
我必须生成此字符串的代码:
$arraytime[] = array('x: ' . $measurement[2] . ', y: ' . $measurement[12]);
$js_arraytime = json_encode($arraytime);
$js_arraytime = str_replace('"', '' ,$js_arraytime);//
$js_arraytime = str_replace("[", '{', $js_arraytime);
$js_arraytime = str_replace("]", '}', $js_arraytime);
echo $js_arraytime;
如果我尝试用其他东西替换$ js_arraytime [0],则每个数组的第一个元素都在改变。有人可以帮帮我吗
答案 0 :(得分:1)
根据你发布的代码和输出,你可以通过简单的字符串操作做你想做的事情,但首先你必须摆脱内部数组。
更改此行:
$arraytime[] = array('x: ' . $measurement[2] . ', y: ' . $measurement[12]);
为:
$arraytime[] = 'x: ' . $measurement[2] . ', y: ' . $measurement[12];
然后使用implode()
将数组转换为字符串:
/* Set up sample array */
$arraytime = array ('x: 140224, y: 50.1',
'x: 140225, y: 50.0',
'x: 140226, y: 50.0',
'x: 140227, y: 50.0');
$js_arraytime = '{' . implode ('},{', $arraytime) . '}';
echo $js_arraytime;
输出:
{x: 140224, y: 50.1},{x: 140225, y: 50.0},{x: 140226, y: 50.0},{x: 140227, y: 50.0}
答案 1 :(得分:0)
这个substr($js_arraytime, 1,-1);
答案 2 :(得分:0)
尝试利用json_encode
中的对象而不是带字符串的数组,然后使用str_replace
删除[
字符。
$arraytime[] = array('x' => $measurement[2], 'y' => $measurement[12]);
$js_arraytime = json_encode($arraytime);
$js_arraytime = str_replace(array('[',']','"'), '', $js_arraytime);
echo $js_arraytime;