我有一个从jquery到php页面的数组,数组中总共有20个字符串,其中一些是空的。
如果一个阵列
array(2) {
[0]=>
string(2) "KI"
[1]=>
string(2) "GY"
}
在php中,我得到了类似的数组
if (isset($_POST[access_array])) {
$access = isset($_POST[access_array]) ? $_POST[access_array] :NULL ;
}
他们我检查它是否像一样是空的
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 ] );
}
我现在要做的是,将数组中的那些字符串访问到变量中像
$array_string1 =
$array_string2 =
$array_string3 =
$array_string4 =
$array_string5 =
blah
blah
$array_string20 =
然后在运行插入语句之前检查$array_string
中是否有任何一个是空的,该语句将存储类似的数据
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.
我面临的问题是如何将数组中的字符串分离为那些$array_string
(s)变量。我看到过类似的问题(相同的标题),但没有具体的答案
首先,如果数组关键字名称来自web,这是很危险的。。。他们可能会尝试覆盖已使用的重要密钥。(有点像Magic GPC,现在也被弃用了。)
所以实际上我真的不推荐这个
但你可以这样做:
$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