0

I have got this array which comes from a database:

   $new_array = array(
     0 => array(2753,8,16,21,39,50,52,4),
     1 => array(2754,11,18,31,35,39,42,34),
     2 => array(2755,7,19,34,39,40,52,8)
     );
     echo '<pre>',print_r($new_array),'</pre>';

Then i change this array to be displayed in brackets by using this php code:

     foreach ($new_array as $new_array2) {
    echo '[';
    foreach ($new_array2 AS $value){
        if (1 == strlen($value)) {
            $zero=0;
            $value = '"'.$zero.$value.'"';
        }
        echo $value;
        if($value!==end($new_array2)){ //referencias: http://stackoverflow.com/a/8780881/1883256
            if(strpos($value, "'") > 0 || strpos($value, '"') > 0){
            // contains either " or '
            echo '';
            }else{echo', ';}
        }/*else{echo 'f';}*/
    }
    echo ']';//referencias: http://www.mydigitallife.info/how-to-access-php-array-and-multidimensional-nested-arrays-code-syntax/
    if($new_array2!==end($new_array)){
        echo ',';
    }else{ echo '';}
}
echo ']';

And the array finally looks like this:

[2753, "08", 16, 21, 39, 50, 52, "04", ],[2754, 11, 18, 31, 35, 39, 42, 34],[2755, "07", 19, 34, 39, 40, 52, "08", ]] 

Something i want is to add a leading zero to single digit numbers. This works fine, but the problem is that, when a single digit number appears at the end of each array, it still has a comma, which causes my script to fail. Looks like the end() function is being ignored when this single digit number comes between double quotes.

I am using the end() function to check whether the last element of an array has been reached or not. If not, then add a comma, if so, don't add a comma. However, when the value is "04", "05", etc. The comma is still added even if it is the last value (check the first array starting with 2753 and notice the other arrays look fine).

How do i fix this?

Pathros
  • 10,042
  • 20
  • 90
  • 156

3 Answers3

2

How about something like this?

$result = array();
     $new_array = array(
     0 => array(2753,8,16,21,39,50,52,4),
     1 => array(2754,11,18,31,35,39,42,34),
     2 => array(2755,7,19,34,39,40,52,8)
     );

     foreach($new_array as $new_array2){
        $temp = array();

        foreach($new_array2 as $val){
            if (strlen($val) == 1) {
                $temp[] = '"' . str_pad($val,2,"0",STR_PAD_LEFT) . '"';
            } else {
                $temp[] = $val;
            }
        }

        $result[] = '[' . implode(', ',$temp) . ']';
     }

     echo implode(',',$result);

This will result to

[2753, "08", 16, 21, 39, 50, 52, "04"],[2754, 11, 18, 31, 35, 39, 42, 34],[2755, "07", 19, 34, 39, 40, 52, "08"] 
Eddie
  • 26,593
  • 6
  • 36
  • 58
2

Always add the comma, then when you've generated the entire array, strip it off:

     $new_array = array(
 0 => array(2753,8,16,21,39,50,52,4),
 1 => array(2754,11,18,31,35,39,42,34),
 2 => array(2755,7,19,34,39,40,52,8)
 );

$out = "";
foreach ($new_array as $new_array2) {
    $out .= '[';
    foreach ($new_array2 AS $value){
        if (1 == strlen($value)) {
            $out .= '"0'.$value.'"';
        } else {
            $out .= $value;
        }
        $out .=', ';
    }
    $out = substr($out,0,-2). '], ';
}
$out = substr($out,0,-2);
echo $out;

Here's a codepad sample

1

Easiest is to implode instead of concatenating your own string:

foreach($new_array as $values) {
    $values = array_map(function($v) { return sprintf('%1$02s', $v); }, $values);
    $result[] = '['.implode(',', $values).']';
}
$result = implode(',', $result);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87