I have a $times array, which contains:
array(8) {
[0]=>
string(5) "10:00"
[1]=>
string(5) "13:00"
[2]=>
string(5) "10:00"
[3]=>
string(5) "11:00"
[4]=>
string(5) "12:00"
[5]=>
string(5) "13:00"
[6]=>
string(5) "14:00"
[7]=>
string(5) "15:00"
}
How can I a) sort it so it starts with lowest value b) only have one entry of each time? (no duplicates, currently theres two 10:00s and 13:00s etc)
Why not just use PHP's inbuilt functions:
$input = array_unique($input);
sort($input);
print_r($input);
Well, you could always use the php-shipped standard functions for arrays:
Array Unique
and sort()
$array = array_unique(sort($a));
Related
Hello i have an array_dif function between 2 arrays and the result it's not as it should.I don't understand why it does not return the status as difference. First array is data second is row and the third is the result. In the result it should also be status because the value is different.
$result = array_diff($data,$row );
array(9) {
["scooter_id"]=>
string(6) "RO0001"
["battery_lvl"]=>
string(2) "80"
["lat"]=>
string(9) "44.312150"
["lng"]=>
string(9) "23.872900"
["alt"]=>
string(1) "0"
["speed"]=>
string(1) "0"
["status"]=>
string(1) "2"
["ip"]=>
string(14) "213.233.101.62"
["port"]=>
int(24600)
}
array(11) {
["battery_lvl"]=>
string(2) "80"
["nr_satelites"]=>
string(1) "1"
["lat"]=>
string(9) "44.312154"
["longi"]=>
string(9) "23.873007"
["alt"]=>
string(1) "0"
["speed"]=>
string(1) "0"
["status"]=>
string(1) "1"
["location"]=>
string(7) "romania"
["ip"]=>
string(14) "213.233.101.62"
["port"]=>
string(5) "24600"
["status_intermediar"]=>
string(1) "2"
}
array(3) {
["scooter_id"]=>
string(6) "RO0001"
["lat"]=>
string(9) "44.312150"
["lng"]=>
string(9) "23.872900"
}
array_diff checks only the values.
Because your 2nd array contains ["status_intermediar"]=> string(1) "2" it finds the value so it doesn't see it as a difference
If you want to check both keys and values you should use array_diff_assoc
Also if you want to find all the different values from BOTH arrays you should run it twice
$difference1=array_diff_assoc($array1,$array2);
$difference2=array_diff_assoc($array2,$array1);
array_dif is one way function ("Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays."- https://www.php.net/manual/en/function.array-diff.php).
If you want all diffs, you have to call it twice: array_dif($first, $second) and array_dif($second, $one) and optionally merge results.
$array_difference1 = array_merge(array_diff($array1, $array2),
array_diff($array2, $array1));
$array_differnce = array_merge(array_diff($array_difference1, $array3),
array_diff($array3, $array_difference1));
How does one iterate in reverse over php associative array? https://stackoverflow.com/a/10777617/1032531 gives solutions for a non-associated array.
My attempt:
$a=['5'=>'five','3'=>'three','7'=>'seven'];
var_dump($a);
foreach($a as $k=>$v){echo("$k $v\n");}
$a=array_reverse($a);
var_dump($a);
foreach($a as $k=>$v){echo("$k $v\n");}
produces the following results:
array(3) {
[5]=>
string(4) "five"
[3]=>
string(5) "three"
[7]=>
string(5) "seven"
}
5 five
3 three
7 seven
array(3) {
[0]=>
string(5) "seven"
[1]=>
string(5) "three"
[2]=>
string(4) "five"
}
0 seven
1 three
2 five
I wish to preserve the keys, and return:
array(3) {
[5]=>
string(4) "five"
[3]=>
string(5) "three"
[7]=>
string(5) "seven"
}
5 five
3 three
7 seven
array(3) {
[7]=>
string(5) "seven"
[3]=>
string(5) "three"
[5]=>
string(4) "five"
}
7 seven
3 three
5 five
Just use $a=array_reverse($a,true); instead of $a=array_reverse($a); for keep key.
array_reverse() have a second optional parameter for preserve keys. default value is false.
Read doc here
You were very close - you had all the key words already - and just need to remember that the PHP manual is your friend :)
The manual page for array_reverse lists an optional argument $preserve_keys, which defaults to false.
So you just need to change $a=array_reverse($a); to $a=array_reverse($a, true);, and you should get the result you were after.
I want to sort an array of varchar data in ascending order through PHP code.
I have tried doing it, the result I am getting is :
ABC1
ABC10
ABC11
ABC11A
ABC11B
ABC2
ABC2A
ABC20
ABC3
But i want :
ABC1
ABC2
ABC2A
ABC3
ABC10
ABC11
ABC11A
ABC11B
ABC20
Is there any way to achieve this?
$myarray= array("ABC1","ABC10","ABC11","ABC11A","ABC11B","ABC2","ABC2A","ABC20","ABC3");
natsort($myarray);
var_dump($myarray);
result
array(9) {
[0]=>
string(4) "ABC1"
[5]=>
string(4) "ABC2"
[6]=>
string(5) "ABC2A"
[8]=>
string(4) "ABC3"
[1]=>
string(5) "ABC10"
[2]=>
string(5) "ABC11"
[3]=>
string(6) "ABC11A"
[4]=>
string(6) "ABC11B"
[7]=>
string(5) "ABC20"
}
UPDATE due to discussion in comments
$keys = array_keys($myarray);
natsort($keys);
$newarray = array();
foreach ($keys as $k) $newarray[] = $myarray[$k];
Using json_encode to encode an array of dates, it sometimes does one thing, sometimes does another.
For example, if I'm trying to encode something like:
array(6) {
[0]=>
string(6) "Jun-24"
[1]=>
string(6) "Jun-25"
[2]=>
string(6) "Jun-28"
[3]=>
string(11) "Training-24"
[4]=>
string(6) "Jun-29"
[5]=>
string(6) "Jun-30"
}
It will output
["Jun-24","Jun-25","Jun-28","Training-24","Jun-29","Jun-30"]
However, when I try to encode something like:
array(17) {
[0]=>
string(6) "Jun-23"
[1]=>
string(6) "Jun-24"
[2]=>
string(6) "Jun-28"
[3]=>
string(11) "Training-24"
[4]=>
string(6) "Jun-29"
[5]=>
string(6) "Jun-30"
[6]=>
string(6) "Jul-06"
[7]=>
string(6) "Jul-07"
[9]=>
string(6) "Jul-09"
[10]=>
string(6) "Jul-16"
[11]=>
string(6) "Jul-17"
[12]=>
string(6) "Jul-20"
[13]=>
string(6) "Jul-23"
[14]=>
string(6) "Jul-24"
[15]=>
string(6) "Jul-30"
[16]=>
string(6) "Aug-01"
[17]=>
string(6) "Aug-05"
}
It will output
{"0":"Jun-23","1":"Jun-24","2":"Jun-28","3":"Training-24","4":"Jun-29","5":"Jun-30","6":"Jul-06","7":"Jul-07","9":"Jul-09","10":"Jul-16","11":"Jul-17","12":"Jul-20","13":"Jul-23","14":"Jul-24","15":"Jul-30","16":"Aug-01","17":"Aug-05"}
(Sorry, couldn't find a smaller example where it fails)
Point being, why does it do this? The options are the same, the array is structured the same, what's the issue?
Your PHP array is missing entry 8, so is a mapping (object) and not a list (array).
You don't have key [8] set in your second example. According to the documentation a sequential array with an unset key will be encoded as a JSON object and not a JSON array.
In your first example the array is numbered sequentially from zero. PHP treats this as a conventional array and encodes it accordingly.
In your second example element 8 is missing. PHP treats this as an associative array and encodes the keys accordingly.
It is because of the indexing issue,
when your index is not proper it will behave like this.
the best way to resolve is reindexing.
$array = array_values($array);
Do like this just before converting to JSON.
Just when you encode the array, do this which will reindex array values:
$encoded = json_encode(array_values($myArray));
I have two arrays something like this
1) array(3) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(3) "med" }
2) array(4) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(4) "other" [3]=> string(3) "med" }
now i want to merge and delete the double entries of the two arrays, important thing is here to keep the order of the first array in the final array (max,min,med -> from first array, and then all others from second array)
the two arrays have different lengths array(3) and array(4)
$myfinalarray = (array_unique(array_merge($arr_first, $arr_last)));
the problem is the order is lost
result:
array(4) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(4) "other" [3]=> string(3) "med" }
what i need is this
array(4) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(3) "med" [3]=> string(4) "other" }
You should erase the double entries from 2nd array first:
$array_last = array_diff($arr_last, $arr_first);
and then
$myfinalarray = (array_unique(array_merge($arr_first, $arr_last)));
Order is not lost, but if you want to sort array in alphabetical order use sort function
$myfinalarray=sort($myfinalarray);
Check the below code,
<?php
$arr1=array(0 => "max",1 => "min",2 => "med");
$arr3=array(0 => "max",1 => "min",2 => "other",3 =>"med");
$myfinalarray = (array_unique(array_merge($arr1, $arr3)));
print_r($myfinalarray);
?>
the output willbe like,
Array
(
[0] => max
[1] => min
[2] => med
[5] => other
)
check here