Splice function php - php

I can't understand how the splice function is working.
If I have these two arrays, I want to replace the array after an offset
Let's say I want to replace after the '2'
$t=array(1,2,3,4,5,6);
$t2=array(0,0);
What should I do to get [1,2,0,0]
I tried this, but obviously not working array_splice($t,2,0,$t2)
Thanks.

If we check the PHP documentation, We can see that your offset is wrong.
Right now, your third parameter is 0, meaning that amount of items we need to replace is 0.
This will do the trick:
array_splice($t, 2, count($t), $t2);
You can try it out here where it gives the following result:
array(4) { [0]=> int(1) 1=> int(2) 2=> int(0) [3]=>
int(0) }
Bare in mind that when executing array_splice, two different results are available.
The return value of array_splice, which returns an array consisting of the extracted elements. (In this case, it would return 3, 4, 5, 6 because those are the elements that were removed)
Since we pass the array by reference, the array itself will contain the result you are looking for.

Related

why would you wrap a variable in square brackets? php 7 [duplicate]

Is there a way to explode a string into variables e.g
some_function($min, $max, "3, 20");
such that $min is assigned the value 3 and $max is assigned the value 20.
I know I can simply use
$data = explode("3, 20");
just wondering if there is another way.
PHP's language construct list() can perform multiple assignments to variables (or even other array keys) by assigning an array.
list($min, $max) = explode(",", "3,20");
However, you would still need to apply a trim() to your variables since the $max value would have a leading space, or replace explode() with preg_split('/\s*,\s*/', $string) to split it on commas and surrounding whitespace.
Note: Use caution with list() to be sure that the array you're assigning contains the same number of elements as list() has variables.
In PHP 5.x, when assigning a value directly to another array, as an element of that array, list() values are assigned from right to left in PHP 5.x, not left to right. In other words, you'll end up with array that is populated backwards (last value, first).
https://www.php.net/manual/en/migration70.incompatible.php
In PHP 7.x list() arguments are assigned from left to right, when assigning elements directly to an array. In other words, you'll end up with the first value as the first element in the recipient array.
<?php
list($a[], $a[], $a[]) = [1, 2, 3];
var_dump($a);
?>
PHP Manual
PHP 5.X Last value gets the first element position, but the recipient must an array (in this case $a, is the array)!
array(3) {
[0]=>
int(3)
[1]=>
int(2)
[2]=>
int(1)
}
PHP 7.x First value becomes the first array element.
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
PHP Manual
Since PHP 7.1 there is a new feature called
Symmetric array destructuring: The shorthand array syntax ([]) may now
be used to destructure arrays for assignments (including within
foreach), as an alternative to the existing list() syntax, which is
still supported.
That means that you can do now:
[$one, $two] = explode(";", "one;two");
echo $one; // one
echo $two; // two
As an alternative to list(), I find it myself a very elegant and readable solution that arrived with 7.1+ ... it is the same, but better.
Note: As the quote indicates, you can use the shorthand array syntax ([]) to assign variables in foreach declarations, but that's not related to the question, so check it out!
Documentation: https://www.php.net/manual/en/migration71.new-features.php

Manually Sort An Array with no common key?

I've tried many solutions here but they all seem to require a common key to sort the array by.
My var_dump() for the array is as follows:
array(10) { [0]=> string(11) "Agriculture" [2]=> string(6) "Metals" [12]=> string(10) "Sanitation" [14]=> string(19) "Health & Beauty" [22]=> string(13) "Oil & Gas" [27]=> string(12) "Construction" [31]=> string(13) "Manufacturing" [58]=> string(8) "Retailer" [61]=> string(11) "Distributor" [77]=> string(7) "Service" }
I'd like to be able to sort the strings in the array into the following order:
Service
Distributor
Retailer
Manufacturing
Construction
Oil & Gas
Health & Beauty
Sanitation
Metals
Agriculture
I know this is not an ideal solution but I really need to manually sort these.
Any help would be super appreciated. Thanks!
The main difference is that this one is generated from a database, so removing one from the databases should also remove it from the array. Hence why I can't manually create it.
So, I assume, that if one is added to the database, it should somehow also be "available"?
If one is renamed in the Database, sorting should still work?
The only reliable solution would be to add another column to the database table, let's call it position - and then fetch the entries and sort them by the position value, which could be 1 upto 10.
Hint on that: Start with a step size of thausand (1000,2000,3000,...) This allows you to add an item later somewhere in between without changing all subsequent item as well. (i.e. creating an entry with position 1500 would become the new second entry, and everything else will automatically shift one position down. If another "new second entry" shall be inserted, you can use 1250 and so on... Gives you some "time" until you have to finally reindex the positions - Use decimal/double if you want to keep this going forever^^)
For me it looks like you want to sort you array by index in descending order. For that you should use krsort() function.
http://php.net/manual/en/function.krsort.php
To sort an array in a reverse order use
array array_reverse ( array $array [, bool $preserve_keys = FALSE ]
Parameters
array
The input array.
preserve_keys
If set to TRUE numeric keys are preserved. Non-numeric keys are not affected by this setting and will always be preserved.
http://php.net/manual/en/function.array-reverse.php

explode string into variables

Is there a way to explode a string into variables e.g
some_function($min, $max, "3, 20");
such that $min is assigned the value 3 and $max is assigned the value 20.
I know I can simply use
$data = explode("3, 20");
just wondering if there is another way.
PHP's language construct list() can perform multiple assignments to variables (or even other array keys) by assigning an array.
list($min, $max) = explode(",", "3,20");
However, you would still need to apply a trim() to your variables since the $max value would have a leading space, or replace explode() with preg_split('/\s*,\s*/', $string) to split it on commas and surrounding whitespace.
Note: Use caution with list() to be sure that the array you're assigning contains the same number of elements as list() has variables.
In PHP 5.x, when assigning a value directly to another array, as an element of that array, list() values are assigned from right to left in PHP 5.x, not left to right. In other words, you'll end up with array that is populated backwards (last value, first).
https://www.php.net/manual/en/migration70.incompatible.php
In PHP 7.x list() arguments are assigned from left to right, when assigning elements directly to an array. In other words, you'll end up with the first value as the first element in the recipient array.
<?php
list($a[], $a[], $a[]) = [1, 2, 3];
var_dump($a);
?>
PHP Manual
PHP 5.X Last value gets the first element position, but the recipient must an array (in this case $a, is the array)!
array(3) {
[0]=>
int(3)
[1]=>
int(2)
[2]=>
int(1)
}
PHP 7.x First value becomes the first array element.
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
PHP Manual
Since PHP 7.1 there is a new feature called
Symmetric array destructuring: The shorthand array syntax ([]) may now
be used to destructure arrays for assignments (including within
foreach), as an alternative to the existing list() syntax, which is
still supported.
That means that you can do now:
[$one, $two] = explode(";", "one;two");
echo $one; // one
echo $two; // two
As an alternative to list(), I find it myself a very elegant and readable solution that arrived with 7.1+ ... it is the same, but better.
Note: As the quote indicates, you can use the shorthand array syntax ([]) to assign variables in foreach declarations, but that's not related to the question, so check it out!
Documentation: https://www.php.net/manual/en/migration71.new-features.php

Listing files in numerical order instead of alphabetical?

Basically, I have a bunch of files with a common prefix (logo%d.jpg) .
When they are viewed using ls or even when looping through a directory in PHP, I don't receive them in numerical order, meaning logo1.jpg, logo2.jpg.
Instead I get them in alphabetical order, like:
logo1.jpg, logo10.jpg, logo11.jpg ... logo 19.jpg, logo2.jpg (Instead of logo20.jpg)
Is there a way to ouput them in numerical order? logo1, logo2, logo3 .. etc.
You could put them in an array and sort the array with the natsortĀ­Docs function:
$array = array('logo1','logo2','logo12');
natsort($array);
Which gives (Demo):
array(3) {
[0]=>
string(5) "logo1"
[1]=>
string(5) "logo2"
[2]=>
string(6) "logo12"
}
The order you're looking for is often called natural order.
Alternatively, you could prefix the numbers, e.g. if you're already using sprintf to name the files, so that the standard sort order would still work:
`logo%03d.jpg`
Which would generate
logo001.jpg
for decimal 1.
Load into an array and use natsort()
If you're using ls like you say...
ls | sort -n
will do the trick.

PHP - Sorting an Array

I am trying to sort an array that contains numbers that range in substantial values. The result I want to get is a descending order of those numbers from the array I am retrieving from a MySQL Server. So far I have created this to test out the "sort" function:
<?php
$numbers = array("100", "50", "70", "1000");
sort($numbers);
echo var_dump($numbers);
?>
And the result I get is this:
array(4) { [0]=> string(2) "50" [1]=> string(2) "70" [2]=> string(3) "100" [3]=> string(4) "1000" }
I can see that the numbers are listing from smallest to largest, but I want it to list from the biggest integer to the smallest integer. Also I don't understand why it has text other than the integers. If anyone could help me out on this, I would greatly appreciate it.
Thanks,
Kevin
You need rsort to sort in reverse order:
rsort($numbers);
More Info:
http://php.net/manual/en/function.rsort.php
rsort() reverse sorts the array :)
you can use rsort to sort it descending.
http://www.developertutorials.com/tutorials/php/sorting-array-php-051114-1019/
Let me show you can find an answer yourself.
navigate to the manual page for the function you are currently using: http://php.net/sort
note especially easy address - just eight characters and a function name. Very handy.
scroll down to the See also section.
Pick appropriate function.
Done!
See, it's not that hard. And no need to accept any answers, cause you answered question yourself.
As for the text, there isn't any. Just try to use this array for something useful and see

Categories