简体   繁体   English

从数组键PHP删除方括号

[英]Remove Square Bracket From Array Key PHP

I'm needing some help with an array. 我需要一些有关数组的帮助。 I know this is more than likely a simple solution, but I'm new so I apologize! 我知道这很可能是一个简单的解决方案,但是我是新手,所以我深表歉意! I've been stuck on this for a while. 我已经坚持了一段时间。

This is the current array that I've got: 这是我目前拥有的数组:

Array ( [0] => 3 [1] => 2 [2] => 5 [3] => 2 )

I need the array to look like this: 我需要数组看起来像这样:

Array ( 0 => 3, 1 => 2, 2 => 5, 3 => 2 )

I'm trying to pass this information to HighCharts and it won't accept the array in the format I currently have. 我正在尝试将此信息传递给HighCharts,它将不接受我当前使用的格式的数组。

Thank you!!! 谢谢!!!

Something like this should work 这样的事情应该工作

// your array according to your question
$someArray = array();
$someArray[0] = 3;
$someArray[1] = 2;
$someArray[2] = 5;
$someArray[3] = 2;

// begin string output
$toHighCharts = 'Array(';

// loop through current array items
foreach($someArray as $k=>$v)
{
    // append string with desired format
    $toHghCharts.= $k.'=>'.$v.',';
}

// remove trailing comma
$toHighCharts = substr($toHighCharts, 0, -1);

// end string output
$toHghCharts.= ')';

// output string output
echo $toHighCharts;

have you tried to convert the Array to a JavaScript Array with jsonencode? 您是否尝试过使用jsonencode将数组转换为JavaScript数组?

For example: 例如:

var jsarray= <?php echo json_encode($phparray); ?>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM