开发者

php is_array() test on $value[] - not sure why it won't register as an array

I am just doing some experimenting. How do I print the value of this $chvalues[] or test (as below) it's an array. Is it possible to print this out as-is? $chvalues[] (yes my input field is properly defined with name=seminar etc...)

$chvalues = array();
if (isset($_POST['seminar'])) {
    foreach ($_POST['seminar'] as $ch => $value) {
        $chvalues[] = $value;
        if(is_array('$chvalues[]')) {
  开发者_StackOverflow社区          echo "Yes. It's an Array.";
        }
    }
}

I am trying to test this:

if(is_array('$chvalues[]')) { ...

I get this:

Fatal error: Cannot use [] for reading in...


  1. You are reading the array as a string by using single quotes around it (which should just return false).

  2. You should use if(is_array($chvalues)) without the brackets.

  3. $chvalues will always be an array, because you just assigned it to one the line before, as well as at the start of your code, before the loop.

Maybe you meant to check $value, or run the is_array() after the loop and not initialize $chvalues as an array before running the loop.


Following a variable by [] is only for appending new elements to the end of the array within the variable. If you want to look at the array then just use the variable itself.

if(is_array($chvalues)) {
   ...


You only use $value[] for accessing the "top" element of the $value array, the unset next position, if you will. Change it to $chvalues in the is_array() function. To print an array, it depends on output. Check print_r for debugging. Also don't use quotes as they make the array act like a string, in this case (because they are single quotes) the string '$chvalues[]' is being tested for being an array.


Two problems here. First, everytime you do $chvalues[] with empty brackets, you are appending a new empty element onto your array. So when you do:

if(is_array($chvalues[])) 

You always get an empty element, never an array.

Instead you probably want:

if(is_array($chvalues)) 

Second problem is you cannot single-quote '$chvalues[]' as you have done. It should have no quotes as in my first code block above.


don't use $chvalues[], just $chvalues (without single quotes inside the is_array function).

$chvalues = array();
if (isset($_POST['seminar'])) {
    foreach ($_POST['seminar'] as $ch => $value) {
        $chvalues = $value;
        if(is_array($chvalues)) {
            echo "Yes. It's an Array.";
        }
    }
}


You really want, is_array($chvalues) (which will always be true in your code) or, if you're trying to find out if the last item added to $chvalues is an array, you should call is_array($value). If you don't have that option (not sure why you wouldn't), then you can use is_array(end($chvalues)) to test the last thing added to the array. I will warn, though, that you will need to call reset to iterate through $chvalues with foreach.

Of course, you could also call $arr = array_keys( $chvalues ); is_array($chvalues[end($arr)]);, but that gets very silly (especially since I left out the last paren originally).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜