Jump to content

Archived

This topic is now archived and is closed to further replies.

bagnallc

array

Recommended Posts

Hi there,

There are so many array functions and im gradually trying to learn them.

For my current purpose i am trying to identify the longest number of occurences when something did not happen. The best way i can explain this is with an example.

Say i have the following - 1, 2, 5, 9, 8, 7, 1, 4, 1, 5, 6, 7, 8, 8, 8, 9, 1, 3

and i want to return the fact that the longest sequence of numbers without 1 appearing was seven (when the chain was 5, 6, 7, 8, 8, 8, 9)

is there an easy way of doing this that you can think of?

many thanks

chris


Share this post


Link to post
Share on other sites
use a for loop over every character in the array, set the number/character you want to find the largest run without, i.e. in your example you would supply "1"

your for loop iterates over the array looking for "1", all the while adding up a counter, when the next time the "1" appears it stores the counter vairiable, it then resets the counter and starts counting again, when it finds another "1" it checks the counter value against the stored count value and saves whichever is the largest, so on so on untillt he array is finished then prinbtes out the stored counter value.

make sense??

Share this post


Link to post
Share on other sites
I used this
[code]
<?PHP
$array = array(1, 2, 5, 9, 8, 7, 1, 4, 1, 5, 6, 7, 8, 8, 8, 9, 1, 3);
$value = 1;                                              //the value you want to find out between i.e largest count between "1"'s
$answer = 0;                                            //initialise the answer
$count = 0;                                              //initialise the counter
for ($i = 0; $i < count($array); $i++) {          //set up the for loop based on the array elements count
$char = $array[$i];                                      //assign each value to the $char variable
if ($char == $value) {                                //check to see if it is the same as the supplied data i.e  1
    if ($count > $answer) {                      //if it is, check to see if the new value of count is larger than the stored value of answer
      $answer = $count;                                //if yes make the asnwer the same as count
      }
    $count = 0; //then reset the counter
}
else {
$count++;                                              //if it is not the same then increase the value of the counter
}
}

echo $answer;  //write out the answer

?>
[/code]

and got the following solution

[code]
7
[/code]

which is correct....

Share this post


Link to post
Share on other sites
or make it a function
[code]
function countArray($array,$value){
$answer = 0;
$count = 0;
for ($i = 0; $i < count($array); $i++) {
$char = $array[$i];
if ($char == $value) {
    if ($count > $answer) {
      $answer = $count;
      }
    $count = 0;
}
else {
$count++;
}
}
return $answer;
}
[/code]

Share this post


Link to post
Share on other sites
Actually guys ive struck a bit of a problem with this!

If the array was changed to

$array = array(1, 2, 5, 9, 8, 7, 1, 4, 1, 5, 6, 7, 8, 8, 8, 9, 3);

(ive taken the last one off)

then the answer returned is 5, when it should be 8. This seems to be as it is the last sequence of numbers in the array.

im playing around with it but havent had any joy as of yet??

Share this post


Link to post
Share on other sites
this should do it
[code=php:0]
function codeMode($arr, $value) {
        $count = 0;
foreach($arr as $val) {
    if($val == $value)
      $count = 0;
    else
      $count++;
}
return $count;
}
[/code]

EDIT: wait..no it won't gimme a sec

here
[code=php:0]
function codeMode($arr, $value) {
        $count = array();
$i = 0;
foreach($arr as $val) {
    if($val == $value)
      $i++;
    else {
      $count[$i]++;
}
}
rsort($count);
return $count[0];
}
[/code]

Share this post


Link to post
Share on other sites
cheers

i actually just got the other one working there by using

array_push($array, 1); which adds a one on the end so it does return the correct value

all a learning process for me.

thanks again

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.