Cannot get array value in php

Have a php array from POST data   $this->log->write(print_r($array , true));

Array
(
[accept] => */*
[accept-encoding] => gzip, deflate
[signature] =>    37df88b6f845c21b1cda84cf3d3b94b0b15759b74f7387ceb0e9c8a6247c211f
[connection] => keep-alive
[content-length] => 610
[user-agent] => python-requests/2.10.0
)

$this->log->write(var_export($array , true).'var_export');

 array (
'accept' . "\0" . '' => '*/*',
'accept-encoding' . "\0" . '' => 'gzip, deflate',
'signature' . "\0" . '' => '37df88b6f845c21b1cda84cf3d3b94b0b15759b74f7387ceb0e9c8a6247c211f',
'connection' . "\0" . '' => 'keep-alive',
'content-length' . "\0" . '' => '610',
'user-agent' . "\0" . '' => 'python-requests/2.10.0',
)var_export

The problem that I cannot access the array with $array['signature'];is empty.

Sorry, the question looks like a noob, but it is not. Work with arrays before and without problems. Tried this as well as $array["signature"]; $array->signature;empty. Please, help

+4
source share
2 answers

As you can see, your keys are somehow added with \0(which is the end-line character in C if my memory is good)

That's why when you try to collect the key signature, there is nothing, because the keysignature\0

, , $array['signature' . "\0"] .

:

$keys = array_map(function($key){ return trim($key); }, array_keys($array));
$array = array_combine($keys, array_values($array));
  • 1:
  • 2:

, ... , PHP7

, , . . Github issue https://github.com/CopernicaMarketingSoftware/PHP-CPP/issues/248 , , PHP7 : p ;)

+1

:

$array["signature\0"];

"\0" "NULL character", , .

"" "\0" , "", 10 , - 9, .

C, , "\0" , .

, . foreach :

foreach($array as $key=>$val){unset($array[$key]);$array[trim($key)] = $val;}
echo $array['signature'];

. .

,

+1

Source: https://habr.com/ru/post/1662282/


All Articles