Problem trying to get value from an array

Asked

Viewed 97 times

1

I’m trying to work with an array I receive by POST, but I’m not getting the values contained within the array.

When I give print_r($_POST) get:

Array ( [access] => Array ( ['Kminput'] => 1000 ['Timeout'] => 2017-06-16 14:20:58 ['status'] => 0 ) )

I’ve tried to access it using $_POST["acesso"]["KMEntrada"] but get the following error message:

Undefined index: Kmentrada

  • 2

    echo $var['acesso']['KMEntrada'] that?

  • I’ve tried it before, but it doesn’t work.

  • Error appears?

  • Undefined index: Kmentrada

  • That inside a for? by the way your code array is different from the question array.

  • I take this code array print_r($_POST);

  • Using the code you passed echo($_POST['acesso']['KMEntrada']);

  • 1

    Now it’s worked out... the code has to stay like this echo($_POST['acesso']["'KMEntrada'"]);

Show 3 more comments

3 answers

0

Have almost certainty that its biggest problem is in the , you’re passing her, most likely thus:

request = function() {
  $.ajax({
    url: "https://requestb.in/1fzmwxx1",
    type: "POST",
    data: {
      "acesso['KMEntrada']": 1000,
      "acesso['HorarioSaida']": '2017-06-16 14:20:58',
      "acesso['Status']": 0
    },
    success: function(returnjson) {},
    error: function(returnjson) {}
  });
}();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

See on requestb.in how the information arrives:

RAW BODY

Access%5B'Kminput'%5D=1000&access%5B'Timeout'%5D=2017-06-16+14%3A20%3A58&access%5B'Status'%5D=0

To see better:

RAW BODY URL ENCODED

access['Kmentrada']=1000&access['Horariosaida ']=2017-06-16 14:20:58&access['Status']=0

When all you had to do was pass like this:

RAW BODY

%5BKMEntrate%5D=1000&access%5BHorarioSaida%5D=2017-06-16+14%3A20%3A58&access%5BStatus%5D=0

RAW BODY URL ENCODED

access[Kminput]=1000&access[Scheduleleft]=2 017-06-16 14:20:58&access[Status]=0

What you did to access variables within the array $_POST['acesso']["'FrotaID'"] works as a patch, a gambiarra, and not as a definitive solution, it is not recommended...

Because it wouldn’t be recommended?

If in the backend you, or some other developer by chance try to convert the $_POST for an object, you will not be able to access the variables within the variable acesso, see an example of what I mean:

$_POST["acesso"] = (object) Array( "'KMEntrada'" => 1000, "'HorarioSaida'" => '2017-06-16 14:20:58', "'Status'" => 0, "teste" => 540 );
$teste = (object) $_POST; // casting da variável
echo $teste ->acesso->teste // Imprime 540
echo $teste ->acesso->'KMEntrada' // invalido

If by chance you try to access json using javascript, you will also have a syntax error:

$arr = array(
    "acesso" => array(
        "'KMEntrada'" => 1000,
        "'HorarioSaida'" => "2017-06-16 14:20:58",
        "'Status'" => 0
    )
);
$v = json_encode($arr);
echo "<script>";
echo "var a = ".$v.";";
echo "console.log(a.acesso);";
echo "console.log(a.acesso.'KMEntrada');";
echo "</script>";

Furthermore, I do not see any reasonable reason to pass a variable with special characters, if this statement is incorrect, I even ask you to correct me:

There is no context where it is valid to define a variable in a queryString with special characters like quotes, arroba, dollar sign and so on, this could even cause problems when recovering variables passed in the backend.


Just know how you are sending this array to your backend and make the fix there, if it is by ajax for example:

request = function() {
  $.ajax({
    url: "https://requestb.in/1fzmwxx1",
    type: "POST",
    data: {
      "acesso[KMEntrada]": 1000,
      "acesso[HorarioSaida]": '2017-06-16 14:20:58',
      "acesso[Status]": 0
    },
    success: function(returnjson) {},
    error: function(returnjson) {}
  });
}();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

See on requestb.in the information that arrives:

FORM/POST PARAMETERS

[Scheduleexit]: 2017-06-16 14:20:58

access[Status]: 0

access[Kminput]: 1000

RAW BODY

%5BKMEntrate%5D=1000&access%5BHorarioSaida%5D=2017-06-16+14%3A20%3A58&access%5BStatus%5D=0

This way, ai yes you can easily access the variables in the correct way, that would be $_POST['acesso']['KMEntrada']

0

To return any value do so

$meuArray = array('acesso' => array('KMEntrada' => 1000, 'HorarioSaida' => '2017-06-16 14:20:58', 'Status' => 0));

echo $meuArray['acesso']['KMEntrada'] ."\n";

echo $meuArray['acesso']['HorarioSaida'] ."\n";

echo $meuArray['acesso']['Status'] ."\n";

example - ideone

0

To recover the value, I had to enter the following code:

$_POST['acesso']["'FrotaID'"]

  • 3

    That doesn’t make any sense!!!

  • 1

    Can you edit the question and add more details of your problem? As Marcelo said, your solution does not make sense. rray had already indicated the correct solution in the comments and gave error. Do not ignore the error. If in the right way it went wrong, there’s something wrong with your code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.