0

I need to create an array in a php file, and add values to it (like $array[] = something) every time I call that php file through ajax

I have tried with global and sessions, but doesn't work I have something like this in the file:

$_GET['index'] is changed every time the php is called from the javascript (ajax) code.

if(isset($_GET['value']) && isset($_GET['next']) && isset($_GET['actl']) && isset($_GET['index'])){
  $_SESSION['filters'][$_GET['index']]['id_feature'] = $_GET['actl'];
  $_SESSION['filters'][$_GET['index']]['value'] = $_GET['value'];

  print_r($_SESSION['filters']);
}

Problem is that resets its values and always shows the last values added to the array.

please excuse me for my english

6
  • Did you call session_start()? Commented Apr 2, 2013 at 17:10
  • no I didn't, where should I do that, at the beginning of the file? is there a problem if that function is used in other files?, I mean, it resets the sessions variables? Commented Apr 2, 2013 at 17:12
  • Call it before you start using anything to do with sessions. At the beginning of the file is usually a good place. Multiple calls to session_start should be no problem but you shouldn't really be having that issue if you design your scripts well. Commented Apr 2, 2013 at 17:14
  • is there a way to do this without using sessions? Commented Apr 2, 2013 at 17:16
  • isset takes multiple parameters so you can do isset($_GET['value'],$_GET['next'],$_GET['actl'],$_GET['index']) Commented Apr 2, 2013 at 17:23

1 Answer 1

1

not an answer, just a comment with a lot of code

This:

$a = array();
$a[]['id_feature'] = $_GET['actl'];
$a[]['value'] = $_GET['value'];

Will probably do this:

array(
    0 => array(
        "id_feature" => "foo"
    ),
    1 => array(
        "value" => "bar"
    )
)

But, you probably want to get this:

array(
    0 => array(
        "id_feature" => "foo",
        "value" => "bar"
    )
)

Write it like:

$a = array();
$a[] = array(
    'id_feature' => $_GET['actl'],
    'value' => $_GET['value']
);
Sign up to request clarification or add additional context in comments.

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.