I'm trying to avoid a database by using arrays. I don't know if it's the right approach though, since I'm not so fluent in PHP.
Would this MySQL scenario be possible with arrays:
"Select all sub-arrays which age-value contain the string '23' (age) and combine their label-key into a new array".
$bears = array(
array(label => "Parent", ages => "23 24 25 26 27 28 29 30 31 32 33"),
array(label=>"Adult", ages => "19 20 21 22 23 24 25 26 27 28 29"),
array(label => "Teenager", ages => "14 15 16 17 18")
)
$klingons = array(
array(label => "Some", ages => "20 21 22 23 24 25 26 27 28"),
array(label => "Something", ages => "19 20 21 22 23"),
array(label => "Dark side", ages => "14 15 16 17 18")
)
Thanks!
You can do it via iteration like this:
$bears = array(
array('label' => "Parent", 'ages' => "23 24 25 26 27 28 29 30 31 32 33"),
array('label' =>"Adult", 'ages' => "19 20 21 22 23 24 25 26 27 28 29"),
array('label' => "Teenager", 'ages' => "14 15 16 17 18")
);
$newArray = array();
foreach($bears as $bear)
{
if(strpos($bear['ages'], '23') !== false)
$newArray[] = $bear['label'];
}
echo("Results:\n");
var_dump($newArray);
Note you have to encapsulate "label" in quotes in your arrays as they are strings.
Related
array:23 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 8
6 => 9
7 => 10
8 => 11
9 => 12
10 => 15
11 => 16
12 => 17
13 => 18
14 => 19
15 => 22
16 => 23
17 => 24
18 => 25
19 => 26
20 => 29
21 => 30
22 => 31
]
this is a array of working days apart from Sunday and Saturday and i have a table of months data and i need a Laravel where condition for comparing all data with whe
->whereYear('Clock_Day',$yearofdata)
->whereMonth('Clock_Day',$monthofdata)
->whereIn('Clock_Day','=',$workdays) //here can i use something like whereIn->whereday---for comparing all array values and get as per the data
There is no whereDayIn() or similar method, but you can do this:
->whereYear('Clock_Day', $yearofdata)
->whereMonth('Clock_Day', $monthofdata)
->where(function($q) use($workdays) {
foreach ($workdays as $day) {
$q->whereDay('Clock_Day', '=', $day, 'or');
}
})
If Clock_Day is a field in your table, you will need to extract the date part you are comparing for each piece (Year, Month, Day). You could possibly use whereIn with DB::raw:
->where(DB::raw('YEAR("Clock_Day")'),$yearofdata)
->where(DB::raw('MONTH("Clock_Day")'),$monthofdata)
->whereIn(DB::raw('DAYOFMONTH("Clock_Day")'),$workdays)
You would only use whereYear or whereMonth to compare those values against fields in your table called 'year' and 'month', but since you want to use the same 'Clock_Day' field for all comparisons you need to extract the relevant data for each part.
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
I am trying to create a multi-dimensional array from a text file so I can access each number. For example the first 7 would be $triangle[0][0] and the first 5 would be $triangle[0][1]. The code I've written so far is
<?php
$triangles = explode("\n", file_get_contents('18.txt'));
var_dump($triangles);
?>
This produced the following output. What do I need to change here? Thanks
array (size=15)
0 => string '75' (length=3)
1 => string '95 64' (length=6)
2 => string '17 47 82' (length=9)
3 => string '18 35 87 10' (length=12)
4 => string '20 04 82 47 65' (length=15)
5 => string '19 01 23 75 03 34' (length=18)
6 => string '88 02 77 73 07 63 67' (length=21)
7 => string '99 65 04 28 06 16 70 92' (length=24)
8 => string '41 41 26 56 83 40 80 70 33' (length=27)
9 => string '41 48 72 33 47 32 37 16 94 29' (length=30)
10 => string '53 71 44 65 25 43 91 52 97 51 14' (length=33)
11 => string '70 11 33 28 77 73 17 78 39 68 17 57' (length=36)
12 => string '91 71 52 38 17 14 91 43 58 50 27 29 48' (length=39)
13 => string '63 66 04 68 89 53 67 30 73 16 69 87 40 31' (length=42)
14 => string '04 62 98 27 23 09 70 98 73 93 38 53 60 04 23' (length=44)
You can use file to get the contents of the file into an array where each array element is a line from the file, then map str_split over that array to separate each line into individual characters.
$triangles = array_map('str_split', file('18.txt', FILE_IGNORE_NEW_LINES ));
echo $triangles[0][0]; // outputs 7
echo $triangles[0][1]; // outputs 5
If you don't want the spaces, you can use str_replace to remove them before splitting the line into individual characters.
$triangles = array_map(function($line) {
return str_split(str_replace(' ', '', $line));
}, file('18.txt', FILE_IGNORE_NEW_LINES ));
I have a problem with strtotime and the following code (I want all calendar weaks in the given time range)
$all_dates = array();
$tempdate = '2014-01-01';
$enddate = '2015-03-01';
while (strtotime($tempdate) <= strtotime($enddate)) {
$all_dates[] = date('Y-W', strtotime($tempdate));
$tempdate = date('Y-m-d', strtotime($tempdate . '+1 week'));
}
var_dump($all_dates);
the var_dump produces the following output
array (
0 => '2014-01',
1 => '2014-02',
2 => '2014-03',
3 => '2014-04',
4 => '2014-05',
5 => '2014-06',
6 => '2014-07',
7 => '2014-08',
8 => '2014-09',
9 => '2014-10',
10 => '2014-11',
11 => '2014-12',
12 => '2014-13',
13 => '2014-14',
14 => '2014-15',
15 => '2014-16',
16 => '2014-17',
17 => '2014-18',
18 => '2014-19',
19 => '2014-20',
20 => '2014-21',
21 => '2014-22',
22 => '2014-23',
23 => '2014-24',
24 => '2014-25',
25 => '2014-26',
26 => '2014-27',
27 => '2014-28',
28 => '2014-29',
29 => '2014-30',
30 => '2014-31',
31 => '2014-32',
32 => '2014-33',
33 => '2014-34',
34 => '2014-35',
35 => '2014-36',
36 => '2014-37',
37 => '2014-38',
38 => '2014-39',
39 => '2014-40',
40 => '2014-41',
41 => '2014-42',
42 => '2014-43',
43 => '2014-44',
44 => '2014-45',
45 => '2014-46',
46 => '2014-47',
47 => '2014-48',
48 => '2014-49',
49 => '2014-50',
50 => '2014-51',
51 => '2014-52',
52 => '2014-01',
53 => '2015-02',
54 => '2015-03',
55 => '2015-04',
56 => '2015-05',
57 => '2015-06',
58 => '2015-07',
59 => '2015-08',
60 => '2015-09',
)
The Problem is entry number 52 with '2014-01', it should be '2015-01'. With the timerange 2015-01-01 till 2016-03-01 the result is ok. Is this a bug in strtotime function?
I'm using PHP version 5.5.10
Thanks for your help.
Change this line and use the o iso year:
$all_dates[] = date('o-W', strtotime($tempdate));
// ^
This has to do with ISO week numbers. Also, strtotime() is a bad way to do date math and PHP recommends against it (thanks to leap years and daylight savings). Use the DateTime() classes to do this with accuracy:
$startdate = new DateTime('2014-01-01');
$enddate = new DateTime('2015-03-01');
$interval = new DateInterval('P7D');
$period = new DatePeriod($startdate , $interval, $enddate);
foreach($period as $date) {
$all_dates[] = $date->format('o-W'); // o modifier for ISO year
}
var_dump($all_dates);
Demo
How I can change this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 11
[8] => 21
[9] => 22
[10] => 23
[11] => 24
)
To this:
1-7, 11, 21-24
I have a list of numbers like this in PHP array, and I just want to make this list a little bit smaller.
2000: 3 6 7 11 15 17 25 36 42 43 45
2001: 2 3 4 5 6 9 10 11 12 13 34 37 45 46 47 48 49 50 51 52
2002: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 33 34 35 36 37 39 40 41 42 43 44 45 46 47 48 49 50 51 52
2003: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
2004: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19 21 22 23 24 25 26 27 28 29 30 31 32 33 34 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
2005: 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
2006: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
2007: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
Interesting task.
Here's a demo script that does exactly what you want.
Tweak to taste.
Code
<?php
$groups = array();
$active_group = 0;
$output = array();
$output_counter = 0;
$nums = array( 1, 2, 3, 4, 5, 6, 7, 11, 21, 22, 23, 24 );
foreach( $nums as $k => $num ) {
// if this isn't the first item, and the current number
// isn't one more than the previous one, increment the counter
if( $k !== 0 && $nums[$k] !== $nums[$k-1]+1 )
$active_group ++;
// add this number to a group
$groups[ $active_group ][] = $num;
}
// take the 1st and last of each group
foreach( $groups as $group ) {
$first = array_shift( array_values($group) );
$output[$output_counter][] = $first;
$last = array_pop( array_values($group) );
if( $first !== $last )
$output[$output_counter][] = $last;
$output_counter++;
}
echo '<pre>';
print_r($output);
?>
Output
Array
(
[0] => Array
(
[0] => 1
[1] => 7
)
[1] => Array
(
[0] => 11
)
[2] => Array
(
[0] => 21
[1] => 24
)
)
A single loop will do. You need need to keep track of the "previous" iteration's value and the "starting" value for storing ranged data.
Code:
$prev = -1; // initialize out of range
foreach ($numbers as $n) {
if (!isset($start)) { // first iteration
$start = $n; // declare $start
} elseif ($n != $prev + 1) { // not consecutive
$result[] = $start == $prev ? $prev : "$start-$prev"; // store single or ranged values
$start = $n; // update $start
}
$prev = $n; // declare / update $prev
}
$result[] = $start == $prev ? $prev : $start . '-' . $prev; // store final iteration data
echo implode(', ', $result); // comma delimit the values
Output from: $numbers = [1, 2, 3, 4, 5, 6, 7, 11, 21, 22, 23, 24]; (Demo)
1-7, 11, 21-24
Output from: $numbers = [1, 3, 5, 6, 11, 21, 22, 23, 24, 26]; (Demo)
1, 3, 5-6, 11, 21-24, 26
Here is a way to both compress an array of integers into the string format you want and to expand that string format back out to an array of integers.
function compress($expanded) {
$low = -1;
$prevNum = -1;
$expanded = array_unique($expanded);
sort($expanded, SORT_NUMERIC);
foreach($expanded as $num) {
if($low == -1) {
$low = $num;
} else if($num - $prevNum > 1) {
$compact[] = ($prevNum - $low >= 1) ? sprintf("%d-%d", $low, $prevNum) : $prevNum;
$low = $num;
}
$prevNum = $num;
}
if($low != -1 ) {
$compact[] = ($num - $low >= 1) ? sprintf("%d-%d", $low, $num) : $num;
}
return implode(",", $compact);
}
public static function expand($compact) {
$expanded = Array();
$compact = explode(",", $compact);
foreach($compact as $num) {
if( is_numeric($num) ) {
$expanded[] = $num;
} else {
list($low, $high) = explode("-", $num);
if( is_numeric($low) && is_numeric($high) && $low < $high) {
for($i = $low;$i <= $high;$i++) {
$expanded[] = $i;
}
}
}
}
return $expanded;
}
//Placeholder array
$b = array();
// Slice array (where to slice)
$s = array(11, 21);
foreach ($array as $year => $a) {
for($i = 0; $i < count($a); $i++) {
for($ii = 0; $ii < count($s); $ii++) {
if($i == 0) {
$b[$year]['<' . $s[$ii]][] = $a[$i];
break;
} else if ( isset($a[$i+1]) && $a[$i] < $s[$ii] && $a[$i+1] >=$s[$ii]){
$b[$year]['<' . $s[$ii]][] = $a[$i];
if (isset($s[$ii+1])) {
$b[$year]['<' . $s[$ii+1]][] = $a[$i+1];
} else {
$b[$year]['>' . $s[$ii]][] = $a[$i+1];
}
break;
} else if ( !isset($s[$ii+1]) && $i == count($a) - 1) {
$b[$year]['>' . $s[$ii]][] = $a[$i];
break;
}
}
}
}
$array
The list of numbers
OUTPUT ($b):
array
2000 =>
array
'<11' =>
array
0 => int 3
1 => int 7
'<21' =>
array
0 => int 11
1 => int 17
'>21' =>
array
0 => int 25
1 => int 45
2001 =>
array
'<11' =>
array
0 => int 2
1 => int 10
'<21' =>
array
0 => int 11
1 => int 13
'>21' =>
array
0 => int 34
1 => int 52
2002 =>
array
'<11' =>
array
0 => int 1
1 => int 10
'<21' =>
array
0 => int 11
1 => int 20
'>21' =>
array
0 => int 21
1 => int 52
2003 =>
array
'<11' =>
array
0 => int 1
1 => int 10
'<21' =>
array
0 => int 11
1 => int 20
'>21' =>
array
0 => int 21
1 => int 51
2004 =>
array
'<11' =>
array
0 => int 1
1 => int 10
'<21' =>
array
0 => int 11
1 => int 19
'>21' =>
array
0 => int 21
1 => int 52
2005 =>
array
'<11' =>
array
0 => int 1
1 => int 10
'<21' =>
array
0 => int 11
1 => int 20
'>21' =>
array
0 => int 21
1 => int 52
2006 =>
array
'<11' =>
array
0 => int 1
1 => int 10
'<21' =>
array
0 => int 11
1 => int 20
'>21' =>
array
0 => int 21
1 => int 52
2007 =>
array
'<11' =>
array
0 => int 1
1 => int 10
'<21' =>
array
0 => int 11
1 => int 20
'>21' =>
array
0 => int 21
1 => int 52
NOTE: Just change the values (11) and (21) to suit your needs. You can add more values.
Heres an example:
$query = "SELECT '1-11' Range, COUNT(rank) rank
FROM promoted WHERE rank between 1 and 11
union all
SELECT '12-21' Range, COUNT(rank) rank
from promoted
where rank between 12 and 21
union all
SELECT '22-31' Range, count(rank) rank
from promoted
where rank between 22 and 31
union all
SELECT '32-40' Range, count(rank) rank
from promoted
where rank between 22 and 31
union all
SELECT rank, count(rank) FROM promoted WHERE rank = '40'";