This question already has answers here:
How to reindex an array?
(6 answers)
Closed 7 years ago.
I apologize in advance for this question - I'm sure it's been answered before, (hell, I've done it before), I just can't find anything in the search and my brain is drawing a blank...
I have an array in PHP where print_r outputs:
Array
(
[0] => VALUE
[1] => VALUE
[2] => VALUE
[3] => VALUE
[4] => VALUE
)
I use unset($_SESSION['item'][$lineID]); to unset a particular line from the array, and I'm left with:
Array
(
[0] => VALUE
[1] => VALUE
[3] => VALUE
[4] => VALUE
)
Element [2] has been removed, so the resulting array is no longer numerically in order
(e.g. - [0][1][3][4] instead of [0][1][2][3] ).
What PHP command do I run on the resulting array to "take out blanks" and reformat it so that it is numerically in order from 1 to n
To reset array keys after unset()
$array = array_values($array);
Related
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 1 year ago.
Consider this array:
Array( [Wingspan] => 5
[Scythe] => 1
[Spirit Island] => 2
[Everdell] => 1)
How can I sort this array with the highest value(5) as first, then 2 then lowest (1) as the last? I used this:
print_r(rsort($_POST['item']), SORT_NUMERIC);
But I get this:
Array
(
[0] => 2
[1] => 1
[2] => 1
[3] => 1
)
It changes the key, but this is wrong. I expect to get this:
Array( [Wingspan] => 5
[Spirit Island] => 2
[Scythe] => 1
[Everdell] => 1)
I searched and found this: Sort an Array by numerical value but that did not help.
Use the method
arsort($_POST['item'])
instead of rsort. rsort only works for simple arrays, not for associative ones. But anyways usually if the order of te items matters to you, probably an associative array is not the best choice. You could use a simple array with objects inside containing the values and the keys at once
This question already has answers here:
How to rearrange the array element from 0th index in the sequence to end?
(1 answer)
How to reindex an array?
(6 answers)
Closed 2 years ago.
I have a session variable like this:
$list = Session::get('list_id')
the $list variable value has a value of Array ( [0] => A, [1] => B,[2] => C, [4] => D)
when I am using unset($list [0]);
I am getting a value of Array ([1] => B,[2] => C, [4] => D)
The problem that I am getting is all of my parameters has a parameters of $list[0] I need the index 0 always, so that value that I am looking for after I remove the index 0 is Array ([0] => B,[1] => C, [2] => D)
so that I can store it again on my session.
is there a way to do this?
Use array_values function if you are sure your array has only numerical index keys.
See this link for more details; https://www.php.net/manual/en/function.array-values.php
Do not use unset.
array_shift — Shift an element off the beginning of array
$removedElement = array_shift($yourArray);
array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down.
This question already has answers here:
Check if specific array key exists in multidimensional array - PHP
(8 answers)
Closed 5 years ago.
Array
(
[leadAssignType] => 3
[ratio] => Array
(
[0] => Array
(
[assigned_to] => 12
[ratio] => 5
)
[1] => Array
(
[assigned_to] => 13
[ratio] => 3
)
)
)
Hello all ! I am struck in one problem. This is my array getting after form submit and I just want to check whether key assigned_to is present for at least once. If it is not present then it should show error message. How can I do that?
simple use array_column to get the specific column from multidimensional array . if the count of array is more than zero key exists otherwise key not exists so show the error message .
if(count(array_column($array['ratio'],'assigned_to'))>0){
echo "key exist in the multi-dimensional array";
}else{
echo "key not present ";
}
This question already has answers here:
PHP Sorting Array ASC
(3 answers)
Closed 6 years ago.
Hello everyone I have a small problem with the php sort I have basically a variable example
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
rsort($prova);
echo $prova[0];
but that's out 4,v Instead I would like so 1,x
Use sort() simply.
<?php
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova);
echo $prova[0]; // Prints 1,x
?>
See it working live
have a look at http://php.net/manual/en/function.sort.php, here you can use second parameter i.e. sort_flags
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova, SORT_STRING); //SORT_STRING - compare items as strings
print_r($prova);
sort($prova, SORT_NUMERIC); //SORT_NUMERIC - compare items numerically
print_r($prova);
output
Array
(
[0] => 1,x
[1] => 2,f
[2] => 22,a
[3] => 4,v
)
Array
(
[0] => 1,x
[1] => 2,f
[2] => 4,v
[3] => 22,a
)
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
below is my array
$myarray = Array(
[1] => Array (['mytime']=>1),
[7] => Array(['mytime']=>2),
[2] => Array(['mytime']=>3),
[3] => Array(['mytime']=>4)
);
I want to sort output of this array based on keys...
$myarray = Array(
[1] => Array (['mytime']=>1),
[2] => Array(['mytime']=>3),
[3] => Array(['mytime']=>4),
[7] => Array(['mytime']=>2)
);
I have already tried ksort($myarray) it displays 1
anyways to fix this??
ksort() does this:
ksort($myarray);
Note: sorting functions do not return a new sorted array; they simply sort the array passed, and return true or false. Thus, ksort($myarray) will return 1 when successful, and $myarray will be sorted.
It's very clear if you read the docs: http://php.net/manual/en/function.ksort.php