Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help: array_search problem!

Status
Not open for further replies.

JediDan

Programmer
May 15, 2002
128
US
Can anyone explain why the last statement in the code below doesn't print a 5? I'm lost here.

Code:
<?
	$test = array();

	for($i = 0; $i < 10; $i++){
		for($j = 0; $j < 10; $j++){
			$test[$i][$j] = $i.$j;
		}
	}

	$cell = array_search(&quot;55&quot;,$test[5]);
	print '<BR>loc: '.$cell;
?>

I've tried the
Code:
array_search
with and without the third type-matching parameter, and the searched-for number in both quotes, and without.

Any help is appreciated.
 
I get nothing!

Well, it prints:
Code:
loc:

But no value for
Code:
$cell
shows up.
 
It may be some strange side-effect of your PHP configuration and version. What version of PHP are you running. I tested your code against version 4.3.0.

Have you inspected your array before you perform the search?

Add:
print '<pre>';
print_r ($test);

to your script. Does the array look like it should on your system? Want the best answers? Ask the best questions: TANSTAAFL!
 
Here's the array contained in array element 5, it's in there:
Code:
[5] => Array
        (
            [0] => 50
            [1] => 51
            [2] => 52
            [3] => 53
            [4] => 54
            [5] => 55
            [6] => 56
            [7] => 57
            [8] => 58
            [9] => 59
        )

Using version 4.2.2.
 
I'm grasping at straws here.

Have you tried printing $test[5][5] inside of some delimiter to verify the value?

print '&quot;' . $test[5][5] . '&quot;';

Alternatively, have you explicitly tested the value?

if ($test[5][5] == &quot;55&quot;).... Want the best answers? Ask the best questions: TANSTAAFL!
 
Code:
print '&quot;' . $test[5][5] . '&quot;';
returned &quot;55&quot;.

Code:
if($test[5][5] == &quot;55&quot;) ...
did nothing.

Very strange.
 
Just to ask the dumb question...

When inserted my suggested code into your script, you didn't use that second fragment verbatim, right? You inserted some action that the if-statement could perform but did not, like 'print &quot;foo!&quot;' or something, right? Want the best answers? Ask the best questions: TANSTAAFL!
 
Yes, I did have a print statement.

But I found my problem, and I realize that I'm an idiot.

What I posted was a fragment of part of real file, and I was actually adding HTML tags before the data so that they would show up a certain way in a table. So really, if I was searching for &quot;55&quot;, the data returned false because the actual array element contained &quot;<TD align=center bgcolor=FF0000>55&quot;.

Thanks for your help. Sorry to have wasted your time!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top