[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

how to access individual items in a POST array?

I have an array like this $perfs = $_POST['perfs'], which a couple of numbers.

1344799479072,1344799479334

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?
0
Kyle Hamilton
Asked:
Kyle Hamilton
  • 3
  • 2
2 Solutions
 
stevepwalesCommented:
You need to either explode that string on the comma or str_split to be able to access that like u want
0
 
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.  http://us.php.net/manual/en/function.explode.php

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

Open in new window

0
 
Kyle HamiltonData ScientistAuthor Commented:
That works. thanks guys.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Dave BaldwinFixer of ProblemsCommented:
You're welcome.
0
 
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?
0
 
Dave BaldwinFixer of ProblemsCommented:
$_POST is the array, 'email' and 'name' are the keys.  Details here: http://us.php.net/manual/en/language.types.array.php
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now