PHP get the last 3 elements of an associative array while preserving the keys?
If you want to preserve key, you can pass in true as the fourth argument:
array_slice($a, -3, 3, true);
stay with the last 15 elements of an array
Use array_slice($a, -15, 15, true);
where $a
is the array.
-15
tells the function to start from 15 elements from the end.
15
tells the function to get 15 elements.
true
tells the function to also preserve the keys, and I am assuming you do.
If you do not want to preserve the keys, you can use array_slice($a, -15);
Since the third argument is missing, it will get the elements until the last is reached (so, 15)
How to get last key in an array?
A solution would be to use a combination of end
and key
(quoting) :
end()
advances array 's internal pointer to the last element, and returns its value.key()
returns the index element of the current array position.
So, a portion of code such as this one should do the trick :
$array = array(
'first' => 123,
'second' => 456,
'last' => 789,
);
end($array); // move the internal pointer to the end of the array
$key = key($array); // fetches the key of the element pointed to by the internal pointer
var_dump($key);
Will output :
string 'last' (length=4)
i.e. the key of the last element of my array.
After this has been done the array's internal pointer will be at the end of the array. As pointed out in the comments, you may want to run reset()
on the array to bring the pointer back to the beginning of the array.
get key and value of second last element of an array
I don't know, why you use a map, when in fact you want an ordered list instead, but
$tmp = array_values($array);
echo $tmp[count($tmp) -2];
should do it. With php5.4 this should work either
echo array_values($array)[count($array)-2];
Rotate array elements to the left (move first element to last and re-index)
Most of the current answers are correct, but only if you don't care about your indices:
$arr = array('foo' => 'bar', 'baz' => 'qux', 'wibble' => 'wobble');
array_push($arr, array_shift($arr));
print_r($arr);
Output:
Array
(
[baz] => qux
[wibble] => wobble
[0] => bar
)
To preserve your indices you can do something like:
$arr = array('foo' => 'bar', 'baz' => 'qux', 'wibble' => 'wobble');
$keys = array_keys($arr);
$val = $arr[$keys[0]];
unset($arr[$keys[0]]);
$arr[$keys[0]] = $val;
print_r($arr);
Output:
Array
(
[baz] => qux
[wibble] => wobble
[foo] => bar
)
Perhaps someone can do the rotation more succinctly than my four-line method, but this works anyway.
How to insert element into arrays at specific position?
array_slice()
can be used to extract parts of the array, and the union array operator (+
) can recombine the parts.
$res = array_slice($array, 0, 3, true) +
array("my_key" => "my_value") +
array_slice($array, 3, count($array)-3, true);
This example:
$array = array(
'zero' => '0',
'one' => '1',
'two' => '2',
'three' => '3',
);
$res = array_slice($array, 0, 3, true) +
array("my_key" => "my_value") +
array_slice($array, 3, count($array) - 1, true) ;
print_r($res);
gives:
Array
(
[zero] => 0
[one] => 1
[two] => 2
[my_key] => my_value
[three] => 3
)
How do i get the part of the url in php
You could do this:
<?php
$url = "http://www.yoursite/one/two/three/drink.pdf";
$ex = explode('/', $url);
$arr = array_slice($ex, -3, 3);
$output = '/'.implode('/', $arr);
var_dump($output); // Outputs /two/three/drink.pdf
First, you use explode
to break the url
string into an array
. Then use array_slice
to get the last three elements of the array and finally implode
to glue back the array elements using /
as the glue.
Putting it all in one line would look like:
echo '/'.implode('/', array_slice(explode('/', $url), -3, 3));
How can I merge two associative arrays and preserve the global order of the entries?
Note that this solution will only work if the two arrays have the same length:
$arr1 = [ 'a' => '1', 'b' => 2 ];
$arr2 = [ 'h' => 'c', 'j' => '3' ];
$count = count($arr1);
$keys1 = array_keys($arr1);
$keys2 = array_keys($arr2);
$result = [];
for ($i = 0; $i < $count; $i++) {
$key1 = $keys1[$i];
$result[$key1] = $arr1[$key1];
$key2 = $keys2[$i];
$result[$key2] = $arr2[$key2];
}
print_r($result);
Output:
Array
(
[a] => 1
[h] => c
[b] => 2
[j] => 3
)
Edited based on mickmackusa's comment below.
Related Topics
Can't Make Laravel 4 to Work on Localhost
Best Way to Autoload Classes in PHP
How to Include File Outside Document Root
PHP Pdo Insert Batch Multiple Rows with Placeholders
Assets Not Referencing to Public Folder (Laravel)
Python Format Datetime with "St", "Nd", "Rd", "Th" (English Ordinal Suffix) Like PHP's "S"
PHP Directory List from Remote Server
Upper Memory Limit for PHP/Apache
Getting the Full Url Including the Query String After Hash
How to Remove Empty Paragraph Tags from String
Curl Requires Curlopt_Ssl_Verifypeer=False
Multi-Byte Safe Wordwrap() Function for Utf-8
When and Why Should $_Request Be Used Instead of $_Get/$_Post/$_Cookie
Phpmysql Error - #1273 - #1273 - Unknown Collation: 'Utf8Mb4_General_Ci'