0

This is my php code:

$allids_arr = $_REQUEST['allids'];
print_r($allids_arr);
echo $arr_count = count($allids_arr);

The array printed like this:

Array (
     [0] => 26 
     [1] => 27 
     [2] => 28 
     [3] => 29 
     [4] => 30 
     [5] => 31 
     [6] => 32 
     [7] => 33 
)

But the count display as 1.

But the correct answer is 8.

What is the problem in my code?

EDIT:

The array was i created: This is the code for my array creation:

$allids = array();
        $ikall = 0;
        foreach($alldata as $rwosall){
            $allids[$ikall] = $rwosall['journelmodel']['id'];
            $ikall++;
        }   
        $this->set('alldataids', $allids);

This is in my controller.And in my view page:

<input type="hidden" readonly="" id="allids" class="input1" name="allids" value="<?php print_r($alldataids);?>">

This value was i requested when form submitted.

8
  • Instead of echo $arr_count = count($allids_arr); just use echo count($allids_arr); Commented Nov 17, 2012 at 6:56
  • i already tested that but it return 1... Commented Nov 17, 2012 at 6:57
  • 1
    You must be doing something else in the code that you aren't showing us. Try putting echo count($_REQUEST['allids']); at the top of your script. Then put $allids_arr = $_REQUEST['allids']; right afterwards. Then put echo count($allids_arr); right after that. No reason that should not give you consistent output. Also please print_r($_REQUEST) to make sure you are doing that right. Commented Nov 17, 2012 at 7:26
  • 2
    @Kichu What exactly do you expect your <input..> field to do with output from print_r($array). That doesn't look like it'll work. Commented Nov 17, 2012 at 7:40
  • 1
    Cthulthu is right, that form will submit a single string formatted to look like an array. You need to either pack/unpack that data using, for example, JSON, or pass it in multiple inputs, one value per input. Commented Nov 17, 2012 at 7:42

4 Answers 4

2

First of all there are few mistakes in your code.

  1. You assign array in hidden field using print_r function which is not an array.
  2. When you submit the value and get the value using $_REQUEST which is treated as a string.It looks like array that is why count is returning 1.

Solution : 1. You can assign value as comma separated like "x,y,z"

Sign up to request clarification or add additional context in comments.

Comments

0

you need:

echo count($allids_arr);

or

$arr_count = count($allids_arr);
echo $arr_count;

:)

6 Comments

but I tried in my code to do a $foo = array("hi", 2, 8); echo $c = count($foo); and it does print 3
Actually his version should work as well. The return value from an assignment is the RHS.
@Kichu do echo count($allids_arr, 1); It will count recursively, in case some elements are nested deeper. Not sure
@Kichu are you sure you didn't do anything in between printing of the array and printing the count? Can you somehow make it into 3 lines of self-contained code or so that will show this behavior?
i cant understand your suggestion...do you mean to dont write it in 3 line of code is it?
|
0

$_REQUEST, by default, contains the contents of $_GET, $_POST and $_COOKIE.

But it's only a default, which depends on variables_order ; and not sure you want to work with cookies.

If I had to choose, I would probably not use $_REQUEST, and I would choose $_GET or $_POST -- depending on what my application should do (i.e. one or the other, but not both) : generally speaking :

You should use `$_GET` when someone is requesting data from your application.
And you should use `$_POST` when someone is pushing (inserting or updating ; or deleting) data to your application.

Either way, there will not be much of a difference about performances : the difference will be negligible, compared to what the rest of your script will do.

2 Comments

This would not explain the inconsistency in his output.
print_r($allids_arr); echo $arr_count = count($allids_arr); //this code work fine.there must be default in somewhere $_REQUEST.use print_r($_REQUEST);
0

Try use this:

<?php
    $foo = $_REQUEST['foo'];
    print_r($foo);
    echo $arr_count = count($foo);
?>

with this query:

sample.php?foo[]=test&foo[]=baz&foo[]=foo

It return correct result:

Array ( [0] => test [1] => baz [2] => foo ) 3

...probably if you use the same name for array and params, work well.

hope this help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.