0

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

9
  • Why exactly would you want them to be in several variables? What output exactly do you get from the JavaScript? Commented Dec 2, 2013 at 13:18
  • what exactly is your question? are you looking to convert array into string? Commented Dec 2, 2013 at 13:18
  • 1
    No, you do not want this. You have a nice array there which you can access via $array[1]. Where in the world is the advantage in doing $array1 instead? Also: Why is $foo[bar] wrong? Commented Dec 2, 2013 at 13:20
  • Well then do $access[0], $access[1] etc. Where's the problem? Commented Dec 2, 2013 at 13:26
  • 1
    @deceze thanks, I was over thinking this. Wanted to do something that was complex when they was an easier way Commented Dec 2, 2013 at 13:27

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

are u referring to the second foreach statement?
@user3057514 I don't understand your question.
sorry if I was alittle confusing but what I wanted to ask was I wanted to know for the answer your provided. was this about my second foreach statement or just for all the foreach statement

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.