Jump to content

[SOLVED] array_search problem


weazle82

Recommended Posts

Hey....i have got a problem with the array_search. It might be quite simple...but i doesnt work at all.

I am trying to get the position of a String inside an array.

 

This is how one of my array looks like ($array)

Array ([0] => 1533 [1] => 4711 [2] => 3311 [3] => 1234)

 

snippet 1 (does not work)

$search = 4711;

$position = array_search($search , $array);
echo $position";

 

Result =

 

snippet 2 (works fine)

// $search = 4711;

$position = array_search(4711 , $array);
echo $position;

 

Result = 1

 

Why can't I use my $search inside the array_search?!

Does anyone know about this "feature" ?!

 

Regards from GER,

Chris

Link to comment
Share on other sites

Try changing your search variable to this:

 

$search = '4711';

 

It may be reading the number as a string inside the array, so making it a string in the variable may work.

 

Hi...i have already tried that. Its some kind of strange.

This part does not work:

 

<?php $result = mysql_query("SELECT * FROM preise_neu;") or die("Fehler: <br>". mysql_error()); 
while ($newarticles = mysql_fetch_assoc($result))  
{ 
	$update_datensatz = array($newarticles['artnr'], $newarticles['preisa'], $newarticles['preisb'], $newarticles['preisc']);

	$artikel = mysql_query("SELECT * FROM artikel;") or die("Fehler: <br>". mysql_error()); 
 	while ($wert = mysql_fetch_assoc($artikel))
	{

		$check = trim($wert['artnr']);
		$parts = explode("\n", $check);

		$search = $update_datensatz[0];

		print_r ($teile);

		$position = array_search($search, $parts);
		echo "Suchwert befindet sich an Stelle: " .  $position . "<br><br><br>";


	}	

}?>

 

But this part works fine

 

<?php

$array = explode("\n", "34
34
346
97
9789
78
678
3");

$search = $array[4];

print_r ($array);

echo "<br><br>" . "Count elements: " . count ($array)  . "<br>";

$position = array_search($search, $array);
echo "Element on position: " .  $position . "<br><br><br>";

?>

 

Hope I was able to make it a little comprehensible .... :-P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

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.