[PHP] array_search and in_array
Hi everyone. I´m facing a big trouble that a don understand why is it happenin. I´ve got an array with 1046 elements in it like this. Here is a part of a print_r of my array. Array ( [0] = 0001-01 [1] = 0003-01 [2] = 0140-01 [3] = 0141-01 [4] = 0142-01 [5] = 0170-03 [6] = 0181-01 [7] = 0182-04 [8] = 0186-06 [9] = 0186-08 [10] = 0550-01 [11] = 0720-01 [12] = 0730-01 [13] = 0740-01 [14] = 0750-01 [15] = 1311-07 [16] = 1316-01 [17] = 1316-02 [18] = 1316-03 [19] = 278980198657138 [20] = 278980198657139 [21] = 278980198657141 [22] = 278980198657142... I get some data from mysql and try to use both functions that´s in the subject to test if the code returned by database is in array, but it isn´t working. By a for i pass by each register returned by database and print the code and if it´s in my array, something like this: for($y=0; $y$con-count;$y++){ $con-Seek($y); echo $con-result['cod_loja']. - .(array_search($con-result['cod_loja'],$_SESSION[cod_loja])?Found:Not found).br; } Here is a part of result of the code above: 0003-01 - Not found 0140-01 - Not found 0141-01 - Not found 0142-01 - Not found 0170-03 - Not found 0181-01 - Not found 0182-04 - Not found 0186-06 - Not found 0186-08 - Not found 0550-01 - Not found Notice that the code 0003-01 is one code that is in my arrasy, but the function do not found it. Could anyone help me in this, please? Thanks a lot. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search function bugged? update
On 27/03/06, je killen [EMAIL PROTECTED] wrote: Hi all Here is an update on my problem reported with the array_search function. to those who directed me to the bug report page, thanks. There are a number of reports specific to this function. Maybe event someone else has written a fix. here is my solution to my problem. There are no open bug reports for array_search(). Loads of bogus ones though. array_search() also completely the wrong tool for the job - the code you posted is much more complicated and slower than it needs to be. That whole palaver that you go through to produce $reduced could be replaced with: $reduced = array_merge(array(), array_unique($str)); That nested loop you use to produce $final would be rather simpler and faster if you just flipped $reduced and used a hash lookup. $charmap = array_flip($reduced); foreach ($str as $char) { $final[] = $charmap[$char]; } -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_search function bugged? update
Hi all Here is an update on my problem reported with the array_search function. to those who directed me to the bug report page, thanks. There are a number of reports specific to this function. Maybe event someone else has written a fix. here is my solution to my problem. The following code* was developed and finalized by J.E.Killen 3/2006 * accept of course for array_search() itself. function word_wise(array $input, int $what) //returns an array {$str = array(); $stra = array(); $str = str_split($input); //turns string into array $stra = $str; // saves a copy for final reconcilliation $tmp = '';// used to hold and transfer values $final = array(); // ***THE WHOLE POINT $reduced = array(); // reduction array $formula = array(); // interum array $output = array(); // test output. for($i = 0; $i count($str); $i++) {$formula[$i] = array_search($str[$i], $str); if($formula[$i] $i) // use of $formula {$str[$i] = '';} // repeats eliminated from $str }; // if $i is greater than formula[$i], formula[$i] is a repeat. for($i = 0; $i count($str); $i++) {if($str[$i] != '') // looks for empty strings in $str { array_push($reduced, $str[$i]); // fills $reduced with // non empty $str items // to produce an array of // all unique letters } }; for($i = 0; $i count($stra); $i++) {for($j = 0; $j count($reduced); $j++) { if($stra[$i] == $reduced[$j])//produces the final formula for {$final[$i] = $j; } // reconstruction of the word. } } for($i = 0; $i count($final); $i++) { $tmp = $final[$i]; $output[$i] = $reduced[$tmp]; //test run reconstructs word from $final and $reduced. } switch($what) //options to chose what to return {case 1: return $reduced; //reduction to all unique letters break; case 2: return $stra;//original break; case 3: return $final; // reconstruction formula break; case 4: return $output; //test break; } } // end word_wise() Just want to return something to the php community Jeff K -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_search function bugged update errata
sorry all for the mistake in the code; in this line 'array $input' should be string $input. function word_wise(array $input, int $what) //returns an array so it reads function word_wise(string $input, int $what) //returns an array this should be the only mistake. I added this stuff as an afterthought after all the code was working perfectly in my test code. best JK -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search function bugged?
On Mon, March 20, 2006 8:35 pm, je killen wrote: I really don't understand, though, why you are doing things the way you describe... I only want one image file for each distinct letter, no repeats because I can't put two or more files with the same name in the same dir. The images need to be drug around DHTML style and reassembled into the correct word. Javascript needs to be able to establish when the chars are in the correct order so I need to have php feed it a formula to refer to. Okay, now we are getting somewhere! The easiest way to meet the unique filename requirement is to name each file after its letter: a.png b.png c.png Of course, they only need to APPEAR that way to the browser and Javascript. In reality, you may not actually have a file named a.png at all. For example, if your HTML is like this: img src=/drawchar.php/a.png / And you have a PHP script drawchar.php like this: ?php //EG: $path_info will be /a.png, //because that is what is in tha URL AFTER the PHP script name $path_info = $_SERVER['PATH_INFO']; //get JUST the one letter $char = substr($path_info, 1, 1); //Create the image: $image = imagecreatetruecolor(40, 10); //Boring colors, but works for now: $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); //make the background white: imagefilledrectangle($image, 0, 0, 39, 9, $white); //draw the character: $font = 3; //Fonts can be 1 through 5 $x = 2; $y = 38; imagechar($image, $font, $x, $y, $char, $black); //Find out how big the image is: ob_start(); imagepng($image); $image_raw = ob_get_contents(); ob_end_clean(); $image_length = strlen($image_raw); //Send suitable headers: header(Content-type: image/png); header(Content-length: $image_length); //Send the data: echo $image_raw; ? You can now just pass the word 'dissatisfaction' around, and use PHP or JavaScript to output individual images. I'll do it in PHP: ?php $word = 'dissatisfaction'; for ($i = 0, $len = strlen($word); $i $len; $i++){ $char = $word[$i]; //or use substr if you like echo img src=\/drawchar.php/$char.png\ /; } ? I haven't run this code to test it, so there might be a typo, but it's going to be MUCH easier to maintain than what you've got going... -- 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
Re: [PHP] array_search function bugged?
On Mon, March 20, 2006 8:47 pm, je killen wrote: I am using simple for loops. If I create and populate an array and test it with a print statement in a for loop, it prints out all the current values. It I again call it to print in a for loop in subsequent code it has exhibited a degraded state. Show us source code that does this. You've made a mistake in your code. Most likely you've altered the array without realizing it, or your variables in your for() loops are mixed up. Easy to do as a beginner. However, if your familiar with FreeBSD, it has a ports and packages mechanism for installing software. I had difficulty with it when installing Apache so I bypassed it and installed Apache from source just according to the install There is simply no way this is your cause. -- 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
Re: [PHP] array_search function bugged?
Richard Lynch wrote: On Mon, March 20, 2006 8:35 pm, je killen wrote: I really don't understand, though, why you are doing things the way you describe... I only want one image file for each distinct letter, no repeats because I can't put two or more files with the same name in the same dir. The images need to be drug around DHTML style and reassembled into the correct word. Javascript needs to be able to establish when the chars are in the correct order so I need to have php feed it a formula to refer to. Okay, now we are getting somewhere! The easiest way to meet the unique filename requirement is to name each file after its letter: a.png b.png c.png Of course, they only need to APPEAR that way to the browser and Javascript. In reality, you may not actually have a file named a.png at all. For example, if your HTML is like this: img src=/drawchar.php/a.png / And you have a PHP script drawchar.php like this: ?php //EG: $path_info will be /a.png, //because that is what is in tha URL AFTER the PHP script name $path_info = $_SERVER['PATH_INFO']; //get JUST the one letter $char = substr($path_info, 1, 1); //Create the image: $image = imagecreatetruecolor(40, 10); //Boring colors, but works for now: $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); //make the background white: imagefilledrectangle($image, 0, 0, 39, 9, $white); //draw the character: $font = 3; //Fonts can be 1 through 5 $x = 2; $y = 38; imagechar($image, $font, $x, $y, $char, $black); //Find out how big the image is: ob_start(); At this point, could you have an switch statement that checked the extension that you sent in the url and make it run the corresponding imagepng(), imagejpg(), and imagegif() ? imagepng($image); $image_raw = ob_get_contents(); ob_end_clean(); $image_length = strlen($image_raw); //Send suitable headers: and then here send the corresponding header too. Just a thought about making it even more universal. jl header(Content-type: image/png); header(Content-length: $image_length); //Send the data: echo $image_raw; ? You can now just pass the word 'dissatisfaction' around, and use PHP or JavaScript to output individual images. I'll do it in PHP: ?php $word = 'dissatisfaction'; for ($i = 0, $len = strlen($word); $i $len; $i++){ $char = $word[$i]; //or use substr if you like echo img src=\/drawchar.php/$char.png\ /; } ? I haven't run this code to test it, so there might be a typo, but it's going to be MUCH easier to maintain than what you've got going... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search function bugged?
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 1 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
[PHP] array_search function bugged?
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. 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. Is this a bug or am I misusing this function? server platform: FreeBSD v6.0 Apache version: 1.3.34 php version: 5.1.2 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. the seems to be no bug report accommodation in the php.net site so I'm posting here. Thanks in advance, JK
Re: [PHP] array_search function bugged?
On 19 Mar 2006, at 22:46, je killen wrote: 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 Ignoring your code (and the supposed 'bug') for a second - why don't you just use the count_chars() function? After all, it is designed to do almost exactly what you are trying to recreate here. http://uk2.php.net/manual/en/function.count-chars.php the seems to be no bug report accommodation in the php.net site so I'm posting here. I'm not sure you looked very hard. There is a link that says 'reporting bugs' in the top nav, which takes you here: http:// bugs.php.net/ Cheers, Rich -- http://www.corephp.co.uk Zend Certified Engineer PHP Development Services -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search and multi-dimensional arrays
jonathan wrote: I'd like to return the first key value in the following array such that England would return 1 rather than 3 which is the second key value. Any help would be greatly appreciated. $c[1][]=Vietnam; $c[1][]=China; $c[1][]=Thailand; $c[1][]=England; $c[2][]=USA; $c[2][]=Japan; print_r($c); // Array ( [1] = Array ( [0] = Vietnam [1] = China [2] = Thailand [3] = England ) [2] = Array ( [0] = USA [1] = Japan ) ) foreach($c as $row) { echo array_search(England,$row); } // prints 3 -jonathan --PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php give this a shot ?php function findIndexof($ar, $str) { foreach($ar AS $k = $v) { if ( is_array($v) ) { $val = findIndexof($v, $str); if( $val !== false ) { return $val; } } elseif ( strtolower($v) == strtolower($str) ) { return $k; } } return FALSE; } $c[1][] = Vietnam; $c[1][] = China; $c[1][] = Thailand; $c[1][] = England; $c[2][] = USA; $c[2][] = Japan; echo findIndexof($c, 'Vietnam'); ? You should be able to search arrays of any depth Hope this woks for you. Jim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search and multi-dimensional arrays
List Manager wrote: jonathan wrote: I'd like to return the first key value in the following array such that England would return 1 rather than 3 which is the second key value. Any help would be greatly appreciated. $c[1][]=Vietnam; $c[1][]=China; $c[1][]=Thailand; $c[1][]=England; $c[2][]=USA; $c[2][]=Japan; print_r($c); // Array ( [1] = Array ( [0] = Vietnam [1] = China [2] = Thailand [3] = England ) [2] = Array ( [0] = USA [1] = Japan ) ) foreach($c as $row) { echo array_search(England,$row); } // prints 3 -jonathan --PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php give this a shot ?php function findIndexof($ar, $str) { foreach($ar AS $k = $v) { if ( is_array($v) ) { $val = findIndexof($v, $str); if( $val !== false ) { return $val; } } elseif ( strtolower($v) == strtolower($str) ) { return $k; } } return FALSE; } $c[1][] = Vietnam; $c[1][] = China; $c[1][] = Thailand; $c[1][] = England; $c[2][] = USA; $c[2][] = Japan; echo findIndexof($c, 'Vietnam'); ? You should be able to search arrays of any depth Hope this woks for you. Jim Or, a lot easier: foreach($c as $key=$row) { if(false !== array_search(England,$row)) { echo $key; } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search and multi-dimensional arrays
M. Sokolewicz wrote: List Manager wrote: jonathan wrote: I'd like to return the first key value in the following array such that England would return 1 rather than 3 which is the second key value. Any help would be greatly appreciated. $c[1][]=Vietnam; $c[1][]=China; $c[1][]=Thailand; $c[1][]=England; $c[2][]=USA; $c[2][]=Japan; print_r($c); // Array ( [1] = Array ( [0] = Vietnam [1] = China [2] = Thailand [3] = England ) [2] = Array ( [0] = USA [1] = Japan ) ) foreach($c as $row) { echo array_search(England,$row); } // prints 3 -jonathan --PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php give this a shot ?php function findIndexof($ar, $str) { foreach($ar AS $k = $v) { if ( is_array($v) ) { $val = findIndexof($v, $str); if( $val !== false ) { return $val; } } elseif ( strtolower($v) == strtolower($str) ) { return $k; } } return FALSE; } $c[1][] = Vietnam; $c[1][] = China; $c[1][] = Thailand; $c[1][] = England; $c[2][] = USA; $c[2][] = Japan; echo findIndexof($c, 'Vietnam'); ? You should be able to search arrays of any depth Hope this woks for you. Jim Or, a lot easier: foreach($c as $key=$row) { if(false !== array_search(England,$row)) { echo $key; } } Well, this is fine, if you know that the array will always be two levels, not one, not three, but two. If this is the case, then fine. But, don't you think it would be nice to have a function that will work no matter what it is passed? Jim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search and multi-dimensional arrays
looks like this works. Is there any problem with it that people can see? $c[1][]=Vietnam; $c[1][]=China; $c[1][]=Thailand; $c[1][]=England; $c[2][]=USA; $c[2][]=Japan; print_r($c); foreach($c as $key=$value) { if(array_search(England,$value)) { echo $key; } } thanks, jonathan On Mar 13, 2006, at 3:54 PM, jonathan wrote: I'd like to return the first key value in the following array such that England would return 1 rather than 3 which is the second key value. Any help would be greatly appreciated. $c[1][]=Vietnam; $c[1][]=China; $c[1][]=Thailand; $c[1][]=England; $c[2][]=USA; $c[2][]=Japan; print_r($c); // Array ( [1] = Array ( [0] = Vietnam [1] = China [2] = Thailand [3] = England ) [2] = Array ( [0] = USA [1] = Japan ) ) foreach($c as $row) { echo array_search(England,$row); } // prints 3 -jonathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search and multi-dimensional arrays
On Mar 13, 2006, at 5:54 PM, jonathan wrote: I'd like to return the first key value in the following array such that England would return 1 rather than 3 which is the second key value. Any help would be greatly appreciated. $c[1][]=Vietnam; $c[1][]=China; $c[1][]=Thailand; $c[1][]=England; $c[2][]=USA; $c[2][]=Japan; print_r($c); // Array ( [1] = Array ( [0] = Vietnam [1] = China [2] = Thailand [3] = England ) [2] = Array ( [0] = USA [1] = Japan ) ) foreach($c as $row) { echo array_search(England,$row); } // prints 3 -jonathan Multi-dimensional arrays can be a bit tricky. I've wanted to do something similar to what you're trying to accomplish. I have not tested this, but I think it works: ? foreach ($c as $i = $value) { foreach ($value as $j = $country) { if ($country == England) echo $i; } } ? Hope that helps. ~Philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_search() with preg_match?
I need to search an array for a string, with a twist. E.g.: $array = array(0 = 'this sky is blue', 1 = 'pencils are orange', 2 = 'I like green apples', 3 = 'strawberries are red'); $key = array_search('green', $array); With the above code, nothing will be returned, but I would like it to return 2 for the key containing green. Any suggestions? ...René -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_search does not find the first element
Hi there, I am trying to find values inside an array. This array always starts with 0. unfortunatelly array_search start searching with the array element 1. So the first element is always overlooked. How could I shift this array to start with 1, or make array-search start with 0? The array comes out of an xml webservice, so I can not rally modify the results. array_search($check_date, $results) Thanx for any help on that. Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search does not find the first element
Hi, Wednesday, March 31, 2004, 10:20:06 PM, you wrote: M Hi there, M I am trying to find values inside an array. This array always starts with 0. M unfortunatelly array_search start searching with the array element 1. M So the first element is always overlooked. M How could I shift this array to start with 1, or make array-search start with 0? M The array comes out of an xml webservice, so I can not rally modify the results. M array_search($check_date, $results) M Thanx for any help on that. M Merlin If you are checking the return value you will need to use === as 0 will fail in == tests. As far as I know array_search() starts at the 0 key. -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search does not find the first element
Merlin wrote: I am trying to find values inside an array. This array always starts with 0. unfortunatelly array_search start searching with the array element 1. So the first element is always overlooked. Please read the text inside the big Warning box on the following page: http://us2.php.net/array_search Thanks. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_search
I've been using array_search in my scripts for a while now, but have come across a problem. My new page has a textarea field. If I enter any new lines in the textarea, array_search returns false. I have tried using html_encode, addslashes, serialize and nothing yet has worked. My only other options that I can think of would be to limit the textarea to one line (change it to a regular text input field) or do a regex on the posted data and replace the line returns with something else (which I have tried without success). Kinda stuck here.. Not sure what I should try next... Thanks, Jake McHenry Nittany Travel MIS Coordinator http://www.nittanytravel.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] array_search
[snip] I've been using array_search in my scripts for a while now, but have come across a problem. My new page has a textarea field. If I enter any new lines in the textarea, array_search returns false. I have tried using html_encode, addslashes, serialize and nothing yet has worked. My only other options that I can think of would be to limit the textarea to one line (change it to a regular text input field) or do a regex on the posted data and replace the line returns with something else (which I have tried without success). [/snip] Just a SWAG, but you are exploding the textarea into an array using spaces for the explosion? If so use a replace function to replace the \n characters, then do your thing...following is untested... $foo = $_POST['textarea']; $newFoo = str_replace(\n, , $foo); $arrayFoo = explode( , $newFoo); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] array_search
-Original Message- From: Jay Blanchard [mailto:[EMAIL PROTECTED] Sent: Thursday, November 20, 2003 2:48 PM To: Jake McHenry; [EMAIL PROTECTED] Subject: RE: [PHP] array_search [snip] I've been using array_search in my scripts for a while now, but have come across a problem. My new page has a textarea field. If I enter any new lines in the textarea, array_search returns false. I have tried using html_encode, addslashes, serialize and nothing yet has worked. My only other options that I can think of would be to limit the textarea to one line (change it to a regular text input field) or do a regex on the posted data and replace the line returns with something else (which I have tried without success). [/snip] Just a SWAG, but you are exploding the textarea into an array using spaces for the explosion? If so use a replace function to replace the \n characters, then do your thing...following is untested... $foo = $_POST['textarea']; $newFoo = str_replace(\n, , $foo); $arrayFoo = explode( , $newFoo); No, I wasn't doing this. I will try it though. Thanks, Jake McHenry Nittany Travel MIS Coordinator http://www.nittanytravel.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] array_search
-Original Message- From: Jake McHenry [mailto:[EMAIL PROTECTED] Sent: Thursday, November 20, 2003 2:51 PM To: 'Jay Blanchard' Cc: [EMAIL PROTECTED] Subject: RE: [PHP] array_search -Original Message- From: Jay Blanchard [mailto:[EMAIL PROTECTED] Sent: Thursday, November 20, 2003 2:48 PM To: Jake McHenry; [EMAIL PROTECTED] Subject: RE: [PHP] array_search [snip] I've been using array_search in my scripts for a while now, but have come across a problem. My new page has a textarea field. If I enter any new lines in the textarea, array_search returns false. I have tried using html_encode, addslashes, serialize and nothing yet has worked. My only other options that I can think of would be to limit the textarea to one line (change it to a regular text input field) or do a regex on the posted data and replace the line returns with something else (which I have tried without success). [/snip] Just a SWAG, but you are exploding the textarea into an array using spaces for the explosion? If so use a replace function to replace the \n characters, then do your thing...following is untested... $foo = $_POST['textarea']; $newFoo = str_replace(\n, , $foo); $arrayFoo = explode( , $newFoo); No, I wasn't doing this. I will try it though. Thanks, Jake McHenry Nittany Travel MIS Coordinator http://www.nittanytravel.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php I had to change it from \n to \r\n, but this really has no usefullness over just changing the textarea into a regular text input field. Just to keep things simple, I think this is what I'm going to do. Thanks for the info Thanks, Jake McHenry Nittany Travel MIS Coordinator http://www.nittanytravel.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] array_search
On Thu, 20 Nov 2003, Jay Blanchard wrote: $foo = $_POST['textarea']; $newFoo = str_replace(\n, , $foo); $arrayFoo = explode( , $newFoo); In the code above any spaces on the lines will also be delimiters... I missed that part of the requirement...? Maybe I'm not understanding the problem, but why not just use something like $myarray = preg_split('/[\r\n]+/', $textarea) ? -- Kelly Hallman //Ultrafancy/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search
From: Jake McHenry [EMAIL PROTECTED] I've been using array_search in my scripts for a while now, but have come across a problem. My new page has a textarea field. If I enter any new lines in the textarea, array_search returns false. [snip] Kinda stuck here.. Not sure what I should try next... How about showing us how you're using array_search, since it has nothing to do with textareas or line breaks except in the context you're using it in... ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Array_search wildcard
Is there such a thing as an array_search wildcard? Say I want to find anything beginning with RE and having zero or more characters following it, how would I do that?
Re: [PHP] Array_search wildcard
$found=array(); foreach($array as $key = $value) { if(ereg('RE.+',$value)) { // optionaly use eregi for case insensitiveness $found[$key]=$value; } } Liam Gibbs wrote: Is there such a thing as an array_search wildcard? Say I want to find anything beginning with RE and having zero or more characters following it, how would I do that? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array_search wildcard
$found=array(); foreach($array as $key = $value) { if(ereg('RE.+',$value)) { // optionaly use eregi for case insensitiveness $found[$key]=$value; } } Thanks, Marek. With some minor adjustments, this worked out fine! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_search() problem
Hi I'm runing a PHP Version 4.0.4pl1 on Solaris 8 X86 on apache, and I'm trying to search an array while(list($findId) = mysql_fetch_row($resultNextLast)){ $NextLast[] = $findId; } $currArrayPos = array_search($ImgId, $NextLast); and this code results this error: Fatal error: Call to undefined function: array_search() Any body have any idea what could be wrong? Thanks David
Re: [PHP] array_search() problem
Hi I'm runing a PHP Version 4.0.4pl1 on Solaris 8 X86 on apache, and I'm trying to search an array From the manual: array_search (PHP 4 = 4.0.5) send earlier to this list: Re: [PHP] Array - Match From: Bas Jobsen [EMAIL PROTECTED] To: N. Pari Purna Chand [EMAIL PROTECTED], [EMAIL PROTECTED] ? $sub = abcd; $subs[] = cde; $subs[] = iyu; $subs[] = abc; $subs[] = xyx; $match=false; foreach($subs as $value) { if($sub==$value){$match=true; break;} } if($match) echo 'found!'; else echo 'not found!'; //or better use //for PHP 4 if(in_array($sub,$subs))echo 'found!'; else echo 'not found!'; //also possible //for PHP 4 = 4.0.5 4.2.0 if(is_null(($b=array_search($sub,$subs echo 'not found!'; else echo 'found! In $sub['.$b.']'; //or for PHP 4 = 4.2.0 if(($b=array_search($sub,$subs))) echo 'found! In $sub['.$b.']'; else echo 'not found!'; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search() problem
What I was looking for was finding out what was the index of thet value, lets say I have a array with some values like (23, 56, 45, 47) and i don't know where the walue I'm looking for is indexed in the array and I want to find out what the indexnumber is, how would that be possable ? function array_index($array,$needle) { /* written by [EMAIL PROTECTED] */ foreach($array as $key=$value) if($value==$needle) return $key; return -1; } $array=array(23, 56, 45, 47); echo array_index($array,45); echo array_index($array,48); if(array_index($array,48)0)echo 'not found'; else echo 'found'; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_search in 2-d arrays
I've a 2 D array and would like to search for vals. in the first dimension only i.e. myArray[0][0] myArray[1][0] myArray[2][0] myArray[3][0] . . . and not in the elements myArray[0][1] CODE: for($l = 0; $l count($layer); $l++) { $key = array_search($layer[$l], $layerDes); // $layerDes is 2-D $layer[$l] = $layerNames[$key]; } My $key is undefined after execution, is it because the elements are in $layerDes[i][0]? Using $key === also didn't help? Any ideas, thanks. -Pushkar S. Pradhan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search in 2-d arrays
Regarding my recent post, I am sorry $key is not undefined it is just 0 or FALSE. I've a 2 D array and would like to search for vals. in the first dimension only i.e. myArray[0][0] myArray[1][0] myArray[2][0] myArray[3][0] . . . and not in the elements myArray[0][1] CODE: for($l = 0; $l count($layer); $l++) { $key = array_search($layer[$l], $layerDes); // $layerDes is 2-D $layer[$l] = $layerNames[$key]; } My $key is undefined after execution, is it because the elements are in $layerDes[i][0]? Using $key === also didn't help? Any ideas, thanks. -Pushkar S. Pradhan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -Pushkar S. Pradhan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_search
thats why you do not have a key which is not found. Try to replace $findme withthis: $findme = array('banana', 'cranberry', 'not in there'); Will return: Key () was found for value not in there So there is a bug.. but well hidden :-( Von: Kevin Stone [EMAIL PROTECTED] An: PHP-general [EMAIL PROTECTED] Betreff: Re: [PHP] how to make array_search start from 0? Datum: Donnerstag, 25. April 2002 20:07 Well this seems to work for me just fine. ? $fruit = array('apple','banana','cranberry'); $findme = array('banana', 'cranberry', 'apple'); for ($i=0; $icount($findme); $i++) { $result = array_search($findme[$i], $fruit); if ($result !== false) { print Key ($result) was found for value $findme[$i]br; } else { print Sorry, could not find $findme[$i].br; } } ? This code outputs: Key (1) was found for value banana Key (2) was found for value cranberry Key (0) was found for value apple -Kevin - Original Message - From: andy [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, April 25, 2002 11:39 AM Subject: Re: [PHP] how to make array_search start from 0? I can see what u are saying. Its all about the === Anyhow.. this thing does not work in a loop. I guess this is my prob. There might be something wrong with the boolean. I tryed it with different settings, but still wrong: Here is what I mean: $fruit = array('apple','banana','cranberry'); $findme = array('apple', 'notlisted'); foreach($findme AS $value){ if (($key = array_search($value, $fruit)) !== false) { print Key ($key) was found from value $valuebr; } else { print Sorry, $value was not found in array \$fruitbr; } } Replys: Key (0) was found from value apple Key () was found from value notlisted So there must be still something wron in th stmt. Thanx for your help, Andy Philip Olson [EMAIL PROTECTED] schrieb im Newsbeitrag Pine.BSF.4.10.10204251646060.3971-10@localhost">news:Pine.BSF.4.10.10204251646060.3971-10@localhost... If the key is 0, array_search will return 0, it does not start at 1. $arr = array('apple','banana','cranberry'); $key = array_search('apple', $arr); print $key; // 0 If 'apple' was not found, $key would then equal to boolean false. Be sure to use === false to check failure because 0 == false. For example: $fruit = array('apple','banana','cranberry'); $findme = 'apple'; if (($key = array_search($findme, $fruit)) !== false) { print Key ($key) was found from value $findme; } else { print Sorry, $findme was not found in array \$fruit; } Again, remember, 0 == false. == !=, === !==. So, 0 !== false. Wow that sounds confusing. :) Also consider the sexy array_keys() function. Regards, Philip Olson p.s. http://uk.php.net/manual/en/language.operators.comparison.php p.s.s. also take into account extra whitespace (trim), and potential issues with case sensitivity (strtolower). On Thu, 25 Apr 2002, andy wrote: Hi there, I am passing an array through the URL with a ',' inbetween: var=php,mysql,super Parsing is done with: explode (',',$var). This gives me an array starting with 0 Later on I have to search for lets say php with array_search. Unfortunatelly array_search requires an array starting with 1. So php is not found. Does anybody know a workaround for this? Thanx, Andy -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_search
Hi, Can anybody explain why the output of this script is not found Found! Here is the script... $legalfields = array(reasonForRepair); if (array_search(reasonForRepair, $legalfields) == TRUE) { echo Found!br; } else { echo not foundbr; } $legalfields = array(foo, reasonForRepair); if (array_search(reasonForRepair, $legalfields) == TRUE) { echo Found!; } else { echo not found; } ? Thanks in advance, Roger -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search
On Tue, 2002-02-26 at 01:29, Roger Keays wrote: Hi, Can anybody explain why the output of this script is not found Found! Yup. array_search() returns the key of the found object, as noted in the docs (http://www.php.net/array_search). Since the first test has the searched-for field at index 0, array_search() will return an int. You're doing a loose (==) test against TRUE (a boolean, not an int), so you get 'not found.'. In the second example, the searched-for item is at index 1, which does loosely evaluate to TRUE. I think the function you're looking for is in_array(): http://www.php.net/in_array Cheers, Torben Here is the script... $legalfields = array(reasonForRepair); if (array_search(reasonForRepair, $legalfields) == TRUE) { echo Found!br; } else { echo not foundbr; } $legalfields = array(foo, reasonForRepair); if (array_search(reasonForRepair, $legalfields) == TRUE) { echo Found!; } else { echo not found; } ? Thanks in advance, Roger -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Torben Wilson [EMAIL PROTECTED] http://www.thebuttlesschaps.com http://www.hybrid17.com http://www.inflatableeye.com +1.604.709.0506 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_search
array_search returns the key-value of the searched value that is found... in the first case... the returned key is 0 because it's the first element... however... 0 is also the same as FALSE... you should use === (three ='s) ... it's used to compare both value and type... (and 0 is not the same type as FALSE)... Greets, Edward - Original Message - From: Roger Keays [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Tuesday, February 26, 2002 10:29 AM Subject: [PHP] array_search Hi, Can anybody explain why the output of this script is not found Found! Here is the script... $legalfields = array(reasonForRepair); if (array_search(reasonForRepair, $legalfields) == TRUE) { echo Found!br; } else { echo not foundbr; } $legalfields = array(foo, reasonForRepair); if (array_search(reasonForRepair, $legalfields) == TRUE) { echo Found!; } else { echo not found; } ? Thanks in advance, Roger -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_search()
I'm trying to figure out how am I supposed to write this snippet properly: if (($index = array_search($search, array_keys($pages))) !== false) { echo index: $index\n; } else { echo Not found\n; } When I search for something that I know exists, it returns the $index just fine. However, when I search for something that does not exist, it never tells me 'Not found', instead it just displays a blank index (it looks like $index = , not false). Should I be checking it against 'false', or against ? Using PHP 4.0.7-dev. -- W | I haven't lost my mind; it's backed up on tape somewhere. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / WebSmith . 800.441.3873 x130 Photo Craft Laboratories, Inc.. 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] array_search()
I'm trying to figure out how am I supposed to write this snippet properly: if (($index = array_search($search, array_keys($pages))) !== false) { echo index: $index\n; } else { echo Not found\n; } When I search for something that I know exists, it returns the $index just fine. However, when I search for something that does not exist, it never tells me 'Not found', instead it just displays a blank index (it looks like $index = , not false). Should I be checking it against 'false', or against ? Using PHP 4.0.7-dev. -- W | I haven't lost my mind; it's backed up on tape somewhere. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / WebSmith . 800.441.3873 x130 Photo Craft Laboratories, Inc.. 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] array_search
Hi How to figure out of a value is found using array_search ? eg: $myarray = array(val1,val2,val3); if(array_search(val2,$myarray)) { print Value found; } the above will work, but... if(array_search(val1,$myarray)) { print Value found; } the above will not work as val1 is key 0, how can I solve this ? Thanks in advance Steen