-3

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 !

4

3 回答 3

0
$vars = array('2_AAA','2_AAA_1','2_AA_B','2_AA_B_1','AA_AB');

foreach ($vars as $v):
    $vArr = explode('_', $v);
    $name = '';
    for($i=0; $i<sizeof($vArr); $i++){
            if(is_numeric($vArr[0]))
        $vid = $vArr[0];
    else
        $vid = 0;

    if(!is_numeric($vArr[$i])){
        if($name)
            $name .= '_'.$vArr[$i]; 
        else
            $name = $vArr[$i];
    }
    $name_real = $v;
}

echo '<br/>------------------------------------------------------<br/>';
echo "VID -->" . $vid . "<br/>";
echo "NAME -->" . $name . "<br/>";
echo "NAME_REAL -->" . $name_real . "<br/>";

endforeach;
于 2012-05-02T12:17:13.583 回答
0

这个怎么样?以下代码给出了您似乎期望的输出。

<?php

$vars = array(
    array( 'val' => '2_AAA' ),
    array( 'val' => '2_AAA_1' ),
    array( 'val' => '2_AA_B' ),
    array( 'val' => '2_AA_B_1' ),
    array( 'val' => 'AA_AB' )
);

foreach ($vars as $v) {
    $vArr = explode('_', $v['val']);

    if( is_numeric( $vArr[0] ) ) {
        $vid = $vArr[0];
        $name = $vArr[1];
        $name_real = $v['val'];
    }
    else {
        $vid = 0;
        $name = $v['val'];
        $name_real = $v['val'];
    }

    echo "VID --> $vid\n";
    echo "NAME --> $name\n";
    echo "NAME_REAL --> $name_real\n";
    echo "\n";

}
于 2012-05-02T12:08:07.590 回答
0

尝试这样做..这不是一个解决方案,但这会让你大致了解如何做到这一点:--

$vars = array('0' => '2_AAA',
              '1' => '2_AAA_1',
              '2' => '2_AA_B',
              '3' => '2_AA_B_1',
              '4' => 'AA_AB');

foreach ($vars as $v):
$i=0;
    $vArr = explode('_', $v);
print_r($vArr);echo "</br>";
    if (is_numeric($vArr[$i]) && count($vArr[0])) {
        $vid = $vArr[$i];
        $name = trim($vArr[$i+1]);
        $name_real = $var;
    } else {

    }

    echo "VID -->" .$vid ; echo "</br>";
    echo "NAME -->" . $vArr[$i+1];echo "</br>";
    echo "NAME_REAL -->" . $name_real;echo "</br>";

endforeach;
于 2012-05-02T12:12:51.387 回答