Re: array_search function bugged?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, March 19, 2006 4:46 pm, je killen wrote:
> The following code does not produce the correct results (for my
> purposes):
>
> function code($str, $match, $formula)
>   {
> for($i = 0; $i < count($str); $i++)
>   {
> $formula[$i] = array_search($str[$i], $str);// <<======|| no bueno
>    //print $formula[$i]; not right
>   if($formula[$i] < $i)
>    {$str[$i] = '';}
>    }
>   //print'<br>'; //----- etc (lots more code)------->
>
> The code takes a string of ascii letters forming a word and is
> supposed
> to create a list of indexes
> in the proper sequence for reconstructing the word. The object of the
> code in context is to take
> any word and create an array of unique letters with no repeats so that
> gd can be used to produce
> images of each letter.

Almost for sure, this function could be used instead:
http://www.php.net/manual/en/function.count-chars.php

You'll be throwing away the information about how many of each letter.

I really don't understand, though, why you are doing things the way
you describe...

> The letters are then reassembled in the browser
> to form the word. The
> $formula above is supposed to tell the browser in what sequence to
> display each letter, including
> using the same letter image in repeat locations. For this it fails
> miserably.
>
> These are the results of test steps:
>
> dissatisfaction (the test word)
> Processed input string:   dissatisfaction (code output at key step to
> verify)
> at creation of formula:    012245128410511314 (formula sampled at ' no
> bueno' line in code)
> from browser source array:    012245128410511314 (formula pasted from
> browser javascript source)
>
> The letter images spell out:
> disstfisntidfiiait  which is in accordance with the formula as far as
> I
> can tell.

How do you distinguish in:
012245128410511314
between one-digit and two-digit integers?

dissatisfaction
          11111
012345678901234

0->d
1->i
2->s
3->NULL (2 is 's')
4->a
5->t
6->NULL (1 is 'i')
7->NULL (2 is 's')
8->f
9->NULL (4 is 'a')
10->c
11->NULL (5 is 't')
12->NULL (1 is 'i')
13->o
14->n

Going from just the numbers, now I get:
012245128410511314
dissatisfaidtiiNULLia

So, given the input 012245128410511314, you are never going to get the
original word back from just that...

I don't think your formula makes sense in the first place...

If you just want to represent each character as a GD image, and not
duplicate, so the browser can cache images, you can just do:

<?php
for ($i = 0, $len = strlen($word); $i < $len; $i++){
  $char = $string[$i];
  ?><img src="drawchar.php/char=<?php echo $char?>" /><?php
}
?>

You can then use $_SYSTEM['PATH_INFO'] (or whatever it's called) to
get 'char=?' and do like:
list($key, $value) = explode('=', $_SYSTEM['PATHINFO']);
$$key = $value;

> There is one other weakness I've discovered, if arrays are created in
> one code sequence and are called
> to print in loops more than once or processed in different code
> sections. The second time the same array
> is called it has significantly degraded, loosing values from index
> positions.

If you are using list/each or something like that, there is an
"internal pointer" in the array which you are mucking with, and most
likely confusing yourself with.

Try using 'foreach' which does not use that internal pointer.

> the seems to be no bug report accommodation in the php.net site so I'm
> posting here.

Posting here FIRST is good, especially since you have not encountered
an actual bug yet :-)

After that, though, there *IS* a place to search for, and ultimately
report bugs:
http://bugs.php.net

But please don't clog that with reports before checking them out
thoroughly, so you really ARE reporting bugs in PHP.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux