Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:从数组的最后一项中删除逗号_Php_Arrays - Fatal编程技术网

PHP:从数组的最后一项中删除逗号

PHP:从数组的最后一项中删除逗号,php,arrays,Php,Arrays,假设我有这个数组: ['Jun 13',529], ['Jul 13',550], ['Aug 13',1005], ['Sep 13',1021], ['Oct 13',1027], 从上述数组中删除最后一项的最快/最简单方法是什么 因此,生成的数组仅包含以下值: ['Jun 13',529], ['Jul 13',550], ['Aug 13',1005], ['Sep 13',1021], ['Oct 13',1027] 实际代码: $i = 0; while($gra

假设我有这个数组:

['Jun 13',529],

['Jul 13',550],

['Aug 13',1005],

['Sep 13',1021],

['Oct 13',1027],
从上述数组中删除最后一项的最快/最简单方法是什么

因此,生成的数组仅包含以下值:

['Jun 13',529],

['Jul 13',550],

['Aug 13',1005],

['Sep 13',1021],

['Oct 13',1027]
实际代码:

$i = 0;
while($graph_data = $con->db_fetch_array($graph_data_rs))
{
    $year = $graph_data['year'];
    $month = $graph_data['month'];
    $count = $graph_data['count'];
    $total_count = $graph_data['total_count'];

    // for get last 2 digits of year
    $shortYear = substr($year, -2, 2);

    // for get month name in Jan,Feb format
    $timestamp = mktime(0, 0, 0, $month, 1);
    $monthName = date('M', $timestamp );

    $data1 = "['".$monthName.' '.$shortYear."',".$total_count."],";

    $i++;
}
提前感谢…

    <?php
    $arr = array(
        "['Jun 13',529],",
        "['Jul 13',550],"
    );
    $arr[] = rtrim(array_pop($arr), ', \t\n\r');
    print_r($arr);
    
    // output:
    
    // Array
    // (
    //     [0] => ['Jun 13',529],
    //     [1] => ['Jul 13',550]
    // )
    
  • 如果在变量中有该数组,并且需要一个字符串,则可以使用获取一个由粘合字符分隔的字符串
  • 如果已经有一个字符串,可以使用删除该字符串右侧的最后一个字符
  • 如果您有一个数组,其中值是一个字符串
    ['Oct 13',1027]
    (以逗号结尾),您可以使用上面相同的选项,并且:
    • 您可以使用上面提到的一些函数
    • 您可以获取最后一个元素,并对其使用如下代码:
在字符串数组上使用的代码示例:

<?php
$values = array("['Oct 13',1027],", "['Oct 13',1027],");
$lastIndex = count($values)-1;
$lastValue = $values[$lastIndex];
$values[$lastIndex] = rtrim($lastValue, ',');

将其设置为实际阵列,然后内爆。不确定会是什么(如果json:you可以做得更好,并且不会让值本身伪造数组,但这将作为一个大小留给读者)


类似于@srain的东西,但使用
array\u push

$values = array("['Oct 13',1027],", "['Oct 13',1027],");

$last = array_pop($values); //pop last element
array_push( $values, rtrim($last, ',') ); //push it by removing comma

var_dump($values);

//output
/*

array
  0 => string '['Oct 13',1027],' (length=16)
  1 => string '['Oct 13',1027]' (length=15)

*/

您是如何生成此“数组”的?此数组的键是什么?这些值是字符串吗?检查
内爆
函数$data1=“['”$monthName.'.$shortYear.“,”$total_count.“,”;获取while循环和打印数组中的所有值,如print\u r($data1);你有一个非常奇怪的阵列,为什么?这是应该让你去->把你的东西放在一个数组中,然后你应该看看json_编码
$values = array("['Oct 13',1027],", "['Oct 13',1027],");

$last = array_pop($values); //pop last element
array_push( $values, rtrim($last, ',') ); //push it by removing comma

var_dump($values);

//output
/*

array
  0 => string '['Oct 13',1027],' (length=16)
  1 => string '['Oct 13',1027]' (length=15)

*/