dongyi6195 2013-12-02 13:16
浏览 52

将数组分成不同的变量

I have an array getting to from jquery to my php page, into total they are 20 strings in the array, some of which are empty.

an example if one of the array

array(2) {
      [0]=>
      string(2) "KI"
      [1]=>
      string(2) "GY"
    }

In php I get the array like

if (isset($_POST[access_array])) {
   $access = isset($_POST[access_array]) ? $_POST[access_array] :NULL ;
}

and they I check to see if it is empty like

foreach( $access as $key => $value ) {
    if( is_array( $value ) ) {
        foreach( $value as $key2 => $value2 ) {
            if( empty( $value2 ) ) 
                unset( $access[ $key ][ $key2 ] );
        }
    }
    if( empty( $access[ $key ] ) )
        unset( $access[ $key ] );
}

what I am trying to do now is, access those strings in the array into variables like

$array_string1 = 
$array_string2 =
$array_string3 = 
$array_string4 = 
$array_string5 =
blah
blah
$array_string20 =

and then check to see if any of the $array_string(s) is empty before running an insert statement which will store the data like

table: lvl

id   name    arraystring
1    john     'HI','HT','OP'   <---- that is just any example so HI will be `$array_string1`, HT `$array_string2` or `$array_string18`. Something like that.

The issue I am facing how is separating the strings in the array into those $array_string(s) variables. I have seen similar questions(same title) but no specify answers

  • 写回答

1条回答 默认 最新

  • doujun1495 2013-12-02 13:28
    关注

    First off, this is dangerous in case the array key names are coming from the web... They might try and overwrite important, already used keys. (Somewhat like Magic GPC, which is now also deprecated.)

    So actually I really DO NOT recommend this

    But you could do something like this:

    $array = array("a" => "apple", "b" => "bear");
    print_r($array); // echoes:
    //Array
    //(
    //    [a] => apple
    //    [b] => bear
    //)
    
    foreach ($array as $key=>$val)
        $$key = $val;
    
    echo $a; // echoes apple
    echo $b; // echoes bear
    
    评论

报告相同问题?