how to access individual items in a POST array?

Posted on 2012-08-12
Last Modified: 2012-08-12
I have an array like this $perfs = $_POST['perfs'], which a couple of numbers.


I would like to be able to access the items by index, like one would in javascript for example:

$diff = $perfs[1] - $perfs[0];

but that results in: $diff => 2

What I'm looking for is: $diff => 262

when I echo $perfs[1], I get 3 not 1344799479334
when I echo $perfs[0], I get 1 not 1344799479072

How can I access my original values?
Question by:Kyle Hamilton
    LVL 6

    Assisted Solution

    You need to either explode that string on the comma or str_split to be able to access that like u want
    LVL 82

    Accepted Solution

    I t looks like the original data is just a string.  So like Steve said, you need to 'explode' the two parts into an array.

    $perfs = explode(",", $_POST['perfs']);
    echo $perfs[0];
    echo $perfs[1];
    echo $perfs[1] - $perfs[0];

    Open in new window

    LVL 25

    Author Closing Comment

    by:Kyle Hamilton
    That works. thanks guys.
    LVL 82

    Expert Comment

    by:Dave Baldwin
    You're welcome.
    LVL 25

    Author Comment

    by:Kyle Hamilton
    Just curious, why do they call it an array?

    I can see now how in my instance it really was a string (which I was passing via ajax):

    var data = ['perfs='+navigationStart, domLoading];

    But even when I've submitted forms, I would have to retrieve each input individually, for example: $email = $_POST['email'] and $name = $_POST["name']

    So when is it actually an array?
    LVL 82

    Expert Comment

    by:Dave Baldwin
    $_POST is the array, 'email' and 'name' are the keys.  Details here:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now