I want to converting values from array before inserting in MySQL. I used the following code, but it only works for some cases (2_AAA, 2_AAA_1) bun not works for cases (2_AA_B, 2_AA_B_1, AA_AB).
$vars = array('val' => '2_AAA',
'val' => '2_AAA_1',
'val' => '2_AA_B',
'val' => '2_AA_B_1',
'val' => 'AA_AB');
foreach ($vars as $v):
$vArr = explode('_', $v['val']);
if (is_numeric($vArr[0]) && count($vArr[0])) {
$vid = $vArr[0];
$name = trim($vArr[1]);
$name_real = $var;
} else {
$vid = 0;
$name = $v['val'];
$name_real = $v['val'];
}
echo "VID -->" . $vid . "\n";
echo "NAME -->" . $vArr[0] . "\n";
echo "NAME_REAL -->" . $name_real . "\n";
endforeach;
From this array I want to get following result
//VID -->2
//NAME -->AAA
//NAME_REAL -->2_AAA
//VID -->2
//NAME -->AAA
//NAME_REAL -->2_AAA_1
//VID -->2
//NAME -->AA_B
//NAME_REAL -->2_AA_B
//VID -->2
//NAME -->AA_B
//NAME_REAL -->2_AA_B_1
//VID -->0
//NAME -->AA_AB
//NAME_REAL -->AA_AB
If can not prefix before name the VID is 0.
How can I make this ?
Thanks in advance !