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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么