how to access individual items in a POST array?

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?
LVL 25
Kyle HamiltonData ScientistAsked:
Who is Participating?
Dave BaldwinFixer of ProblemsCommented:
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

You need to either explode that string on the comma or str_split to be able to access that like u want
Kyle HamiltonData ScientistAuthor Commented:
That works. thanks guys.
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Dave BaldwinFixer of ProblemsCommented:
You're welcome.
Kyle HamiltonData ScientistAuthor Commented:
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?
Dave BaldwinFixer of ProblemsCommented:
$_POST is the array, 'email' and 'name' are the keys.  Details here:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.