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?