I have to translate a python code to php, but I don't know how this python code work in php :
dec = data_ar[4:len(data_ar)]
I don't know what's the colon ":" between [ ]! Thanks
I have to translate a python code to php, but I don't know how this python code work in php :
dec = data_ar[4:len(data_ar)]
I don't know what's the colon ":" between [ ]! Thanks
That's just Python's array slicing notation, so I think what you want is array_slice
:
array_slice($data_ar, 4);
If you're trying to achieve the exact functionality in PHP:
<?php
$data_ar = array(1,2,3,4,5,6,7);
$dec = array_slice($data_ar, 4, count($data_ar)-4);
Now, the interesting thing here is the len(data_ar)
. If you provide the colon without a second argument, "until the end" is assumed. So this piece of functionality should actually have been written like so:
dec = data_ar[4:]
Likewise, we can modify the PHP side of things to reflect the same simplicity:
<?php
$data_ar = array(1,2,3,4,5,6,7);
$dec = array_slice($data_ar, 4);
print_r($dec); // Returns array(5,6,7)
Colon means "to" in most languages. This means index 4 to the length of the array (the end).
In PHP, you can use array_slice
like this:
$dec = array_slice($data_ar, 4)