Jump to content
bubbles

PHP Array

Recommended Posts

Posted

Salut,

Am un array de genul :

Array
(
[0] => Array
(
[0] => YES
[1] => 2015-05-01
)

[1] => Array
(
[0] => YES
[1] => 2015-05-02
)

[2] => Array
(
[0] => YES
[1] => 2015-05-03
)

[3] => Array
(
[0] => YES
[1] => 2015-05-04
)

[4] => Array
(
[0] => YES
[1] => 2015-05-05
)

[5] => Array
(
[0] => YES
[1] => 2015-05-06
)

[6] => Array
(
[0] => NO
[1] => 2015-05-07
)

[7] => Array
(
[0] => YES
[1] => 2015-05-08
)

[8] => Array
(
[0] => NO
[1] => 2015-05-09
)

[9] => Array
(
[0] => YES
[1] => 2015-05-10
)

[10] => Array
(
[0] => YES
[1] => 2015-05-11
)

[11] => Array
(
[0] => YES
[1] => 2015-05-12
)

[12] => Array
(
[0] => NO
[1] => 2015-05-13
)

[13] => Array
(
[0] => YES
[1] => 2015-05-14
)

[14] => Array
(
[0] => NO
[1] => 2015-05-15
)

[15] => Array
(
[0] => YES
[1] => 2015-05-16
)

[16] => Array
(
[0] => YES
[1] => 2015-05-17
)

[17] => Array
(
[0] => NO
[1] => 2015-05-18
)

[18] => Array
(
[0] => YES
[1] => 2015-05-19
)

[19] => Array
(
[0] => YES
[1] => 2015-05-20
)

[20] => Array
(
[0] => YES
[1] => 2015-05-21
)

[21] => Array
(
[0] => NO
[1] => 2015-05-22
)

[22] => Array
(
[0] => NO
[1] => 2015-05-23
)

[23] => Array
(
[0] => NO
[1] => 2015-05-24
)

[24] => Array
(
[0] => NO
[1] => 2015-05-25
)

[25] => Array
(
[0] => YES
[1] => 2015-05-26
)

[26] => Array
(
[0] => YES
[1] => 2015-05-27
)

)

Si as vrea sa aflu YES de cate ori a aparut cel mai mult CONSECUTIV si implicit data de start/end.

Daca e sa fac manual YES a aparut consecutiv cel mai mult de 6 ori perioada (2015-05-01 / 2015-05-06).

O idee ?

Merci.

Posted

Nu am testat, dar este o idee


$stats = array(); //aici tii numaratoarea
$stats['count'] = 0;
$stats['count_old'] = 0;

foreach($array as $data)//$array este arrayul tau
{
if($data[0] == 'YES')
{
if($stats['count'] == 0) $stats['start'] = $data[1]; //la primul YES adaugi 'start'
$stats['end'] = $data[1]; //de fiecare data cand apare YES updatezi 'end'
$stats['count']++; //de cate ori apare YES
}
else
{
if($stats['count'] > $stats['count_old']) //mentii maximul de YES consecutive in 'count_old'
{
$stats['start_old'] = $stats['start'];
$stats['end_old'] = $stats['end'];
$stats['count_old'] = $stats['count'];
}
$stats['count'] = 0;
}
}

//iti mai trebuie un if in caz ca $stats['count'] iti da mai mare decat $stats['count_old'] cand se termina foreach

Posted

$max = array();
$last_max = array();

foreach ($ARR as $i=>$v) {
if ($v[0] == "Yes") {
$max['start'] = ($max['count'] == 0) ? $v[1] : $max['start'];
$max['count']++;

if ($max['count'] > $last_max['count']) {
$last_max['count'] = $max['count'];
$last_max['start'] = $max['start'];
$last_max['end'] = $v[1];
}
}
else {
$max['count'] = 0;
}
}

ah, am scris acelasi lucru, cailor.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...