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
https://forums.phpfreaks.com/topic/73536-solved-array_search-problem/
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

Archived

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

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