$VIN=2GH445GH52W334556
I want that to be broke into an array with the following elements :
Code: Select all
<?php
$vin_array[0]="2";
$vin_array[1]="G"; etc......
?>Moderator: General Moderators
Code: Select all
<?php
$vin_array[0]="2";
$vin_array[1]="G"; etc......
?>Code: Select all
<?php
function str_to_array($string) {
$len = strlen($string);
$str_array = array();
for ($i=0; $i<$len; $i++) {
$str_array[] = $string{$i};
}
return $str_array;
}
?>Code: Select all
<?php
$VIN='2GH445GH52W334556';
function str2array($string)
{
return explode("\x06", wordwrap($string, 1, "\x06", true));
}
print_r(str2array($VIN));
?>Code: Select all
Array
(
ї0] => 2
ї1] => G
ї2] => H
ї3] => 4
ї4] => 4
ї5] => 5
ї6] => G
ї7] => H
ї8] => 5
ї9] => 2
ї10] => W
ї11] => 3
ї12] => 3
ї13] => 4
ї14] => 5
ї15] => 5
ї16] => 6
)Code: Select all
<?php
function str2array($string)
{
return explode("\x06", wordwrap($string, 1, "\x06", true));
}
$VIN='2GH445GH52W334556';
$array = str2array($VIN);
foreach ($array as $k => $v)
{
if (is_numeric($v))
{
$array[$k] = (int) $v;
}
else
{
// do whatever processing
// required on non-numerical
// chars
}
}
var_dump($array);
?>Code: Select all
<?php
function str2array($string)
{
return explode("\x06", wordwrap($string, 1, "\x06", true));
}
function convert(&$v, $k)
{
if (is_numeric($v))
{
$v = (int) $v;
}
else
{
// do whatever processing
// required on non-numerical
// chars
}
}
$VIN='2GH445GH52W334556';
$array = str2array($VIN);
array_walk($array, 'convert');
var_dump($array);
?>Code: Select all
array(17) {
ї0]=>
int(2)
ї1]=>
string(1) "G"
ї2]=>
string(1) "H"
ї3]=>
int(4)
ї4]=>
int(4)
ї5]=>
int(5)
ї6]=>
string(1) "G"
ї7]=>
string(1) "H"
ї8]=>
int(5)
ї9]=>
int(2)
ї10]=>
string(1) "W"
ї11]=>
int(3)
ї12]=>
int(3)
ї13]=>
int(4)
ї14]=>
int(5)
ї15]=>
int(5)
ї16]=>
int(6)
}