unexpected results with sort(array) - php

Odd.....
// array sort test
$_ar = array(
0 => "2015-02-23",
1 => "2015-02-21",
2 => "2015-02-28",
3 => "2015-03-20",
4 => "2015-03-14",
5 => "2015-03-21",
6 => "2015-02-21",
7 => "2015-02-28",
8 => "2015-03-07",
9 => "2015-03-14",
);
$_ar = sort($_ar);
var_dump($_ar);
// returns bool(true)
$__ar = array(
0 => "2015 02 23",
1 => "2015 02 21",
2 => "2015 02 28",
3 => "2015 03 20",
4 => "2015 03 14",
5 => "2015 03 21",
6 => "2015 02 21",
7 => "2015 02 28",
8 => "2015 03 07",
9 => "2015 03 14",
);
$__ar = sort($__ar);
var_dump($__ar);
// returns bool(true)
$ar = array(
0 => "20150223",
1 => "20150221",
2 => "20150228",
3 => "20150320",
4 => "20150314",
5 => "20150321",
6 => "20150221",
7 => "20150228",
8 => "20150307",
9 => "20150314",
);
$ar = sort($ar);
var_dump($ar);
// returns bool(true)
I am expecting this to return the array sorted by the date value. I thought maybe it was the - (hyphen) or spaces, but in all my examples my PHP var_dump simply returns bool(true) for each instance. Can someone confirm they get the same, or point out what I must be missing....
I have tried asort() - still the same.

You don't have to assign the return value of sort(). For more information about sort() see the manual: http://php.net/manual/en/function.sort.php
And a quote from there:
Returns TRUE on success or FALSE on failure.
So just to this:
sort($_ar);
Side Note:
I wouldn't recommend you to define variables with underscores at the start of the name, since this already get's used by defined php variables e.g. super globals or magic constants

The sort and asort function returns bool value. Just call this function and it will sort the array, don't store it , it returns true or false.Use the code below
// array sort test
$_ar = array(
0 => "2015-02-23",
1 => "2015-02-21",
2 => "2015-02-28",
3 => "2015-03-20",
4 => "2015-03-14",
5 => "2015-03-21",
6 => "2015-02-21",
7 => "2015-02-28",
8 => "2015-03-07",
9 => "2015-03-14",
);
sort($_ar);
var_dump($_ar);
// returns bool(true)
$__ar = array(
0 => "2015 02 23",
1 => "2015 02 21",
2 => "2015 02 28",
3 => "2015 03 20",
4 => "2015 03 14",
5 => "2015 03 21",
6 => "2015 02 21",
7 => "2015 02 28",
8 => "2015 03 07",
9 => "2015 03 14",
);
$__ar = sort($__ar);
var_dump($__ar);
// returns bool(true)
$ar = array(
0 => "20150223",
1 => "20150221",
2 => "20150228",
3 => "20150320",
4 => "20150314",
5 => "20150321",
6 => "20150221",
7 => "20150228",
8 => "20150307",
9 => "20150314",
);
sort($ar);
var_dump($ar);
// returns bool(true)
Hope this helps you

Related

Group rows of data by column value then store nested data, first and last occurrences, and counts within each group

I am trying to split an array of space-delimited strings, group by a particular column, then store the data within each group in a more convenient structure.
Sample data:
$dataArray = [
0 => "AAAAA 2023 01 25 01:04:00 ID:20fjrjeZZ",
1 => "AAAAA 2023 01 25 01:18:08 ID:13454B43A",
2 => "AAAAA 2023 01 25 02:00:02 ID:18f5hjeWe",
3 => "AAAAA 2023 01 25 04:10:13 ID:13454B43A",
4 => "BBBBB 2023 01 25 01:44:10 ID:Xj74320fj",
5 => "BBBBB 2023 01 25 07:08:58 ID:13454B43A",
6 => "BBBBB 2023 01 25 08:40:52 ID:Ftzkk800Y",
7 => "BBBBB 2023 01 25 14:10:13 ID:18f5hjeWe"
];
I split the rows on the space with:
$lines = explode(' ', $dataArray);
Then I want to push the individual parts (AAAA, 2023, 01, ...) into an array.
foreach($dataArray as $parts){
$spotArray[] = $parts[$parts][0];
$yearArray[] = $parts[$parts][1];
// ...
}
Then build a new structure with the new array parts:
foreach($dataArray as $key => $value){
$desiredArray[] = $spotArray[["user"[$yearArray[$hourArray]]], "first"[/** ... */]];
//...
}
Expected result:
$desiredArray = [
"AAAAA" => [
"user" => [
"ID:20fjrjeZZ" => ["01:04:00"],
"ID:13454B43A" => ["01:18:08", "04:10:12"],
"ID:18f5hjeWe" => ["02:00:02"]
],
"first" => "01:04:00",
"last" => "04:10:12",
"totaUser" => 3,
"totalAccess" => 4
],
"BBBBB" => [
"user" => [
"ID:Xj74320fj" => ["01:44:10"],
"ID:13454B43A" => ["07:08:58"],
"ID:Ftzkk800Y" => ["08:40:52"],
"ID:18f5hjeWe" => ["14:10:13"]
],
"first" => "01:44:10",
"last" => "14:10:13",
"totaUser" => 4,
"totalAccess" => 4
]
];
It is not at all necessary to run two loops.
Parse the space-delimited strings in your array and build/overwrite/sum as you iterate.
Code: (Demo)
$result = [];
foreach ($dataArray as $row) {
[$group, $y, $m, $d, $t, $id] = explode(' ', $row);
$result[$group]['user'][$id][] = $t; // accumulate nested elements
$result[$group]['first'] ??= $t; // only store the first occurrence
$result[$group]['last'] = $t; // keep overwriting each time
$result[$group]['totaluser'] = count($result[$group]['user']); // count what is accumulated
$result[$group]['totalAccess'] = ($result[$group]['totalAccess'] ?? 0) + 1; // increment
}
var_export($result);
You can even safely remove the unused $y, $m, and $d declarations if you wish. (Demo)
Output (from either snippet)
array (
'AAAAA' =>
array (
'user' =>
array (
'ID:20fjrjeZZ' =>
array (
0 => '01:04:00',
),
'ID:13454B43A' =>
array (
0 => '01:18:08',
1 => '04:10:13',
),
'ID:18f5hjeWe' =>
array (
0 => '02:00:02',
),
),
'first' => '01:04:00',
'last' => '04:10:13',
'totaluser' => 3,
'totalAccess' => 4,
),
'BBBBB' =>
array (
'user' =>
array (
'ID:Xj74320fj' =>
array (
0 => '01:44:10',
),
'ID:13454B43A' =>
array (
0 => '07:08:58',
),
'ID:Ftzkk800Y' =>
array (
0 => '08:40:52',
),
'ID:18f5hjeWe' =>
array (
0 => '14:10:13',
),
),
'first' => '01:44:10',
'last' => '14:10:13',
'totaluser' => 4,
'totalAccess' => 4,
),
)
You can find the answer to your question here
<?php
$dataArray = [
0 => "AAAAA 2023 01 25 01:04:00 ID:20fjrjeZZ",
1 => "AAAAA 2023 01 25 01:18:08 ID:13454B43A",
2 => "AAAAA 2023 01 25 02:00:02 ID:18f5hjeWe",
3 => "AAAAA 2023 01 25 04:10:13 ID:13454B43A",
4 => "BBBBB 2023 01 25 01:44:10 ID:Xj74320fj",
5 => "BBBBB 2023 01 25 07:08:58 ID:13454B43A",
6 => "BBBBB 2023 01 25 08:40:52 ID:Ftzkk800Y",
7 => "BBBBB 2023 01 25 14:10:13 ID:18f5hjeWe"
];
$finalArr = array();
$count_arr = array();
$count_arr1 = array();
foreach($dataArray as $parts){
$lines = explode(' ', $parts);
$finalArr[$lines[0]]['user'][$lines[5]][] = $lines[4];
$count_arr1[$lines[0]]['user'][$lines[5]] = $lines[4];
$count_arr[$lines[0]][] = 1;
}
foreach($finalArr as $key => $parts){
$finalArr[$key]['first'] = reset($count_arr1[$key]['user']);
$finalArr[$key]['last'] = end($count_arr1[$key]['user']);
$finalArr[$key]['totaUser'] = count($finalArr[$key]['user']);
$finalArr[$key]['totalAccess'] = count($count_arr[$key]);
}
print_r($finalArr);

I want to remove the common elements in two arrays using PHP [duplicate]

This question already has answers here:
How to remove an array value from another array using PHP question
(3 answers)
Closed 2 years ago.
I tried the following code and it should be working, but not getting the required result. What's wrong with the code? I have two arrays and I want to remove the common elements in both arrays so I wore the following code.
<?php
$aMgaMembersList= array (
0 => '9194962',
1 => '9197448',
2 => '9174039',
3 => '9199473',
4 => '9175598',
5 => '9197474',
6 => '9195444',
7 => '9195268',
8 => '9189438',
9 => '9175103',
10 => '9199619',
11 => '9195267',
12 => '9194463',
13 => '9196333',
14 => '9197471',
15 => '9198479',
16 => '9197472',
17 => '9185479',
18 => '9197452',
19 => '9197442',
20 => '9180861',
21 => '9194950',
22 => '9198464',
23 => '9199613',
24 => '9175939',
25 => '9195442',
26 => '9190203',
27 => '9199613',
) ;
$aRocketMembersList = array (
0 => '9174039',
1 => '9175103',
2 => '9175598',
3 => '9175939',
4 => '9180861',
5 => '9185479',
6 => '9189438',
7 => '9190203',
8 => '9194463',
9 => '9194950',
10 => '9194962',
11 => '9195267',
12 => '9195268',
13 => '9195442',
14 => '9195444',
15 => '9196333',
16 => '9197442',
17 => '9197448',
18 => '9197452',
19 => '9197471',
20 => '9197472',
21 => '9197474',
22 => '9198464',
23 => '9198479',
24 => '9199473',
25 => '9199613',
26 => '9199619',
27 => 'arun',
) ;
if (is_array($aRocketMembersList)) {
foreach ($aRocketMembersList as $rocketUsername) {
if (in_array($rocketUsername, $aMgaMembersList)) {
unset($aMgaMembersList[array_search($rocketUsername, $aMgaMembersList)]);
unset($aRocketMembersList[array_search($rocketUsername, $aRocketMembersList)]);
}
}
}
print_r($aRocketMembersList);
print_r($aMgaMembersList);
The out put is
Array
(
[27] => arun
)
Array
(
[27] => 9199613
)
The element 9199613 shouldn't be there. Why it's happening? I ran the code in a different environment and the result is same.
Here's a different function that works regardless of the order of Arrays:
<?php
function different($array1, $array2){
$m = array_merge($array1, $array2); $x = array_intersect($array1, $array2); $a = array_diff($m, $x); $b = array_diff($x, $m); $a = array_merge($a, $b);
$r = [];
foreach($a as $v){
$o = new StdClass; $k = array_search($v, $array1);
if($k === false)$k = array_search($v, $array2);
$o->$k = [$array1[$k], $array2[$k]]; $r[] = $o;
}
return $r;
}
$aMgaMembersList = [
0 => '9194962',
1 => '9197448',
2 => '9174039',
3 => '9199473',
4 => '9175598',
5 => '9197474',
6 => '9195444',
7 => '9195268',
8 => '9189438',
9 => '9175103',
10 => '9199619',
11 => '9195267',
12 => '9194463',
13 => '9196333',
14 => '9197471',
15 => '9198479',
16 => '9197472',
17 => '9185479',
18 => '9197452',
19 => '9197442',
20 => '9180861',
21 => '9194950',
22 => '9198464',
23 => '9199613',
24 => '9175939',
25 => '9195442',
26 => '9190203',
27 => '9199613'
];
$aRocketMembersList = [
0 => '9174039',
1 => '9175103',
2 => '9175598',
3 => '9175939',
4 => '9180861',
5 => '9185479',
6 => '9189438',
7 => '9190203',
8 => '9194463',
9 => '9194950',
10 => '9194962',
11 => '9195267',
12 => '9195268',
13 => '9195442',
14 => '9195444',
15 => '9196333',
16 => '9197442',
17 => '9197448',
18 => '9197452',
19 => '9197471',
20 => '9197472',
21 => '9197474',
22 => '9198464',
23 => '9198479',
24 => '9199473',
25 => '9199613',
26 => '9199619',
27 => 'arun'
];
$diffArray = different($aMgaMembersList, $aRocketMembersList);
$test = json_encode($diffArray);
?>
$tmp1 = array_diff($aMgaMembersList,$aRocketMembersList);
$tmp2 = array_diff($aRocketMembersList,$aMgaMembersList);
$final = array_unqiue(array_merge($tmp1, $tmp2));
unset($tmp1);unset($tmp2);//and maybe original arrays?
There are probably better solutions, but that should work for you. If you had associative arrays instead of numeric values you could exclude array_unqiue
EDIT:
I originally assumed you wanted the results in one array, if that's unnecessary just use the array_diff function twice, and you can maintain your original array names as desired. Again there are probably better solutions (more memory/processor efficient), but in most practical cases this will be fine. If you're working with extremely large data sets... do more research ;)

PHP Trader macd returns false

I want to use trader_macd but it returns always false.
I am using the default parameters:
$data = [
0 => "0.06945900",
1 => "0.06945200",
2 => "0.06948100",
3 => "0.06944100",
4 => "0.06939800",
5 => "0.06941800",
6 => "0.06942300",
7 => "0.06940000",
8 => "0.06937700",
9 => "0.06937200",
10 => "0.06940000",
11 => "0.06939800",
12 => "0.06941100",
13 => "0.06944500",
14 => "0.06940100",
15 => "0.06942600",
16 => "0.06941500",
17 => "0.06941400",
18 => "0.06939900",
19 => "0.06941400",
20 => "0.06940700",
21 => "0.06938100",
22 => "0.06940400",
23 => "0.06937400",
24 => "0.06937000",
25 => "0.06939700"]
$result = trader_macd($data, 12, 26, 9)
When I set the last parameter ($signalPeriod) then get an array with 0 values:
0 => array:1 [▼
24 => -0.0
]
1 => array:1 [▼
24 => -0.0
]
2 => array:1 [▼
24 => -0.0
]
When I am using other methods like trader_ema with same $data it works fine.
I also set trader.real_precision to 8.
ini_set('trader.real_precision', '8');
What I am doing wrong?
My Systems uses php 7.2.7 with trader 0.5.0.
You don't have enough data to calculate the signal line you chose (9 day EMA of MACD line). Add eight more entries to your data array, and you'll get a result. Or lower the signal line period

Laravel compare database and api

I have api base data like:
array:21 [▼
0 => "jne"
1 => "pos"
2 => "tiki"
3 => "esl"
4 => "pcp"
5 => "rpx"
6 => "cahaya"
7 => "dse"
8 => "first"
9 => "indah"
10 => "jet"
11 => "jnt"
12 => "ncs"
13 => "pahala"
14 => "pandu"
15 => "sap"
16 => "sicepat"
17 => "slis"
18 => "star"
19 => "nss"
20 => "wahana"
]
which i get with this code:
$cori = $rajaongkir->courier('all');
$cori = array_keys($cori);
I also have database table named couriers where i saved same names as you see in my dd above + status and dd of my table is:
Collection {#825 ▼
#items: array:19 [▼
0 => "jne"
1 => "pos"
2 => "tiki"
3 => "esl"
4 => "rpx"
5 => "cahaya"
6 => "dse"
7 => "first"
8 => "indah"
9 => "jet"
10 => "jnt"
11 => "ncs"
12 => "pahala"
13 => "pandu"
14 => "sap"
15 => "sicepat"
16 => "slis"
17 => "star"
18 => "nss"
]
}
this is how i get that:
$selectedcouriers = Courier::where('active', '=', '1')->pluck('courier');
PS: as you see my table dd has 2 names less than my api dd that's
because those 2 names active status is 0 (deactivated)
Problem
what I try to do here is compare between my API courier names and my Database courier names (which are the same by the way) and then return only those couriers with status of 1 in my database (ignore the rest of courier names in my api)
how can i do this compare?
My full code is:
$cori = $rajaongkir->courier('all'); // get all couriers from api
$cori = array_keys($cori); // retrieve only their names
$selectedcouriers = Courier::where('active', '=', '1')->pluck('courier'); // get my DB couriers with active status
//comparing code...?
Simple, use array_intersect()
$result = array_intersect($selectedcourier, $cori);

How to Create complex Array Structure in PHP

I have to make this kind of structure in array;
We have three ( 3 ) variables which creates this structure:
$numberOfParticipants = 38; // 38 is example
$numberOfParticipantsPerHeat = 8 // 8 is example
$numberOfHeats = 5; // 5 is example
Based on this variables I have this table:
The problem is that, I can't place the ' - ' or null after 31 OR 38. The task is that i have to make the arrays of array "almost equal" like the photo and must depend on the variables above. By the way, after I create the correct list I will slice the array to 5 or 6 or whatever parts I need this is not the problem, the problem is that I have to parse the list like this first. This is what I tried so far:
$calc1 = (int)round($numberOfParticipants * $numberOfParticipantsPerHeat, -1); //First round the numberOfParticipants to closest integer by 10
$readyArr = [];
for ($i = 1; $i <= $calc1; $i++) {
if ($i <= $numberOfParticipants) {
$readyArr[$i] = $i;
} else {
$readyArr[$i] = null;
}
}
The problem with this snippet is that it places the null at the end of the list not after 31, or based on the var.
This is the result I have:
array:40 [▼
1 => 1
2 => 2
3 => 3
4 => 4
5 => 5
6 => 6
7 => 7
8 => 8
9 => 9
10 => 10
11 => 11
12 => 12
13 => 13
14 => 14
15 => 15
16 => 16
17 => 17
18 => 18
19 => 19
20 => 20
21 => 21
22 => 22
23 => 23
24 => 24
25 => 25
26 => 26
27 => 27
28 => 28
29 => 29
30 => 30
31 => 31
32 => 32
33 => 33
34 => 34
35 => 35
36 => 36
37 => 37
38 => 38
39 => null
40 => null
]
The Array after partition I want should be:
array(
0 => array(0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6, 6 => 7, 7 => 8,),
1 => array(0 => 9, 1 => 10, 2 => 11, 3 => 12, 4 => 13, 5 => 14, 6 => 15, 7 => 16,),
2 => array(0 => 17, 1 => 18, 2 => 19, 3 => 20, 4 => 21, 5 => 22, 6 => 23, 7 => 24,),
3 => array(0 => 25, 1 => 26, 2 => 27, 3 => 28, 4 => 29, 5 => 30, 6 => 31, 7 => null,),
4 => array(0 => 32, 1 => 33, 2 => 34, 3 => 35, 4 => 36, 5 => 37, 6 => 38, 7 => null,),
);
Every help, every clue will be highly appreciated.
There are two things you need to know about the target structure:
How many players are in the first (which will always be the largest, if only by one) set.
$playersPerHeat = ceil($numberOfParticipants / $numberOfHeats);
// note this replaces your hard-coded $numberOfParticipantsPerHeat
You also need to know how many heats actually have that many, that is how many heats are actually full.
$fullHeats = $numberOfParticipants % $numberOfHeats ?: $numberOfHeats;
// The ?: bit means that if we get zero (ie. all equal heats), then we
// count all the heats instead, since they're all full.
Now it's easy!
$players = range(1,$numberOfParticipants);
$heats = array_merge(
array_chunk(
array_slice($players, 0, $fullHeats * $playersPerHeat),
$playersPerHeat
),
array_chunk(
array_slice($players, $fullHeats * $playersPerHeat),
$playersPerHeat - 1
)
);
That's it! Demo

Categories