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
Related
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 ;)
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
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
I can't figure out why asort isn't working. Neither does any other sort work. $hs['hs_type'] are values that come from MySQL query.
$results = $query->result_array();
$hs_types = array();
foreach($results as $hs) {
$hs_types[$hs['hs_type']]++;
}
$projects = array();
foreach($hs_types as $hs) {
array_push($projects, $hs);
}
asort($projects);
var_dump for my array before sort: array (size=15)
* 8 => int 1709
* 13 => int 26
* 7 => int 474
* 14 => int 800
* 11 => int 282
* 6 => int 61
* 5 => int 23
* 15 => int 181
* 3 => int 2
* 19 => int 3
* 9 => int 50
* 1 => int 44
* 2 => int 2
* 4 => int 4
* 18 => int 13
var_dump for my array after sort: array (size=15)
* 8 => int 2
* 12 => int 2
* 9 => int 3
* 13 => int 4
* 14 => int 13
* 6 => int 23
* 1 => int 26
* 11 => int 44
* 10 => int 50
* 5 => int 61
* 7 => int 181
* 4 => int 282
* 2 => int 474
* 3 => int 800
* 0 => int 1709
What I wanted:
* 3 => int 2
* 2 => int 2
* 19 => int 3
* 4 => int 4
* 18 => int 13
* 5 => int 23
* 13 => int 26
* 1 => int 44
* 9 => int 50
* 15 => int 181
* 11 => int 282
* 7 => int 474
* 14 => int 800
* 8 => int 1709
Problem is that you are getting everything into your nicely keyed array, then pushing each item into the $projects array (losing the key) then doing a asort on the $projects array.
It shows up with this minor test script:-
<?php
$hs_types = array(8 => 1709,
13 => 26,
7 => 474,
14 => 800,
11 => 282,
6 => 61,
5 => 23,
15 => 181,
3 => 2,
19 => 3,
9 => 50,
1 => 44,
2 => 2,
4 => 4,
18 => 13);
Echo "\r\nOriginal array\r\n";
print_r($hs_types);
$projects = array();
foreach($hs_types as $hs)
{
array_push($projects, $hs);
}
asort($projects);
Echo "\r\nPushed array sorted\r\n";
print_r($projects);
asort($hs_types);
Echo "\r\nOriginal array sorted\r\n";
print_r($hs_types);
?>
but it looks like it would be easier to just get the sorted list in SQL in the first place.
I tried using your array with asort() and it worked perfectly, coming back with the result you expected:
<?php
//your array
$a = array(
8 => 1709,
13 => 26,
7 => 474,
14 => 800,
11 => 282,
6 => 61,
5 => 23,
15 => 181,
3 => 2,
19 => 3,
9 => 50,
1 => 44,
2 => 2,
4 => 4,
18 => 13
);
//print unsorted array
print_r($a);
//sort the array
asort($a);
//print the sorted array
echo "<br/><br/>";
print_r($a);
If this really isn't working for you, why not do the sorting in the SQL query instead? Then you wouldn't need to bother re-sorting it in PHP.
Are you sure you are using asort instead of sort ? I tried your code and it works perfectly :
<?php
$myArray = array(
8 => 1709,
13 => 26,
7 => 474,
14 => 800,
11 => 282,
6 => 61,
5 => 23,
15 => 181,
3 => 2,
19 => 3,
9 => 50,
1 => 44,
2 => 2,
4 => 4,
18 => 13,
);
var_dump($myArray);
asort($myArray);
var_dump($myArray);
?>
My result :
Say I have an array with keys representing id and values representing parent:
4 => 0
2 => 0
5 => 2
6 => 5
8 => 0
9 => 0
10 => 8
12 => 0
13 => 0
14 => 0
18 => 7
19 => 18
20 => 19
21 => 20
22 => 21
23 => 22
24 => 23
28 => 20
7 => 5
You could also read this as an object:
{
id : 4,
parent : 0
} // etc...
The multidimensional array I'd want to achieve from this would be:
4 => 0
2 => 5
=> 6
=> 7
=> 18
=> 19
=> 20
=> 21
=> 22
=> 23
=> 24
=> 28
8 => 10
9 => 0
12 => 0
13 => 0
14 => 0
How would I go about doing this?
If you write a little helper function to rework your data to a structure similar to:
$input = array(
array('id' => '4', 'parent' => '0'),
// ...
);
which could be achieved with something like:
$data = array_map(function ($entry) {
list($id, $parent) = array_map('trim', explode('=>', $entry));
return array(
'id' => $id,
'parent' => $parent
);
}, explode("\n", $data));
you could then use a function I used in a similar question:
function flatToNested($d, $r = 0, $p = 'parent', $k = 'id', $c = 'children') {
$m = array();
foreach ($d as $e) {
isset($m[$e[$p]]) ?: $m[$e[$p]] = array();
isset($m[$e[$k]]) ?: $m[$e[$k]] = array();
$m[$e[$p]][] = array_merge($e, array($c => &$m[$e[$k]]));
}
return $m[$r];
}
to produce a nested array with:
$nested = flatToNested($data);
demo: http://codepad.viper-7.com/HAZxaA