Problem trying to get value from an array

1

I'm trying to work with an array that I get by POST, but I'm not able to get the values contained within the array.

When I give print_r($_POST) I get:

  

Array ([access] => Array (['KMEntrance'] => 1000 ['ActionTime '] = >   2017-06-16 14:20:58 ['Status'] = > 0))

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

  

Undefined index: KMEntrada

    
asked by anonymous 16.06.2017 / 19:49

3 answers

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

    
16.06.2017 / 20:08
0

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

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

    
16.06.2017 / 20:10
0

I have almost sure that your biggest problem is in , you're probably probably like this:

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 requestb.in as the information arrives:

  

RAW BODY

     

access% 5B'KENnetwork '% 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 ['KMEntrance'] = 1000 & access ['SleepTime'] = 2017-06-16 14: 20: 58 & access ['Status'] = 0

When in fact it was enough to pass like this:

  

RAW BODY

     

Access% 5BKInsert% 5D = 1000 & Access% 5BrotherSaid% 5D = 2017-06-16 + 14% 3A20% 3A58 & Access% 5BStatus% 5D = 0

     

RAW BODY URL ENCODED

     

Access [KMEntrade] = 1000 & access [Sleeptime] = 2 017-06-16 14: 20: 58 & Access [Status] = 0

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

Why would not it be recommended?

If on the backend you, or some other developer by chance try to convert $_POST to an object, you could not access the variables within the acesso variable, 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 you try to access json using javascript, you also get 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>";

In addition, I do not see any reason to pass a variable with special characters, if this statement is incorrect, I even ask you to rectify it:

  

There is no context where it is worth defining a variable in a   queryString with special characters like quotes, arroba, dollar sign and etc, this could even bring problems when retrieving the variables passed in the backend

.

Just know how you are sending this array to your backend and do the correction, if it is for 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 requestb.in for arriving information:

  

FORM / POST PARAMETERS

     

access [SeChange]: 2017-06-16 14:20:58

     

access [Status]: 0

     

access [KMEntrade]: 1000

     

RAW BODY

     

Access% 5BKInsert% 5D = 1000 & Access% 5BrotherSaid% 5D = 2017-06-16 + 14% 3A20% 3A58 & Access% 5BStatus% 5D = 0

So, yes, you can easily access the variables of the correct mode, which would be $_POST['acesso']['KMEntrada']

    
28.06.2017 / 03:44