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?

4
  • 1
    are you trying to create json arrays? Commented May 20, 2014 at 2:45
  • you're using !== which is comparing 'type', if you need to match an integer to a string try using != Commented May 20, 2014 at 2:46
  • @meda Yes, indeed. I am working with dataTables and is working fine. But i want those leading zeros for single digit numbers. Commented May 20, 2014 at 2:48
  • @joe42 I tried != but the comma is still there :/ Commented May 20, 2014 at 2:50

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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"] 

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.