By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,935 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,935 IT Pros & Developers. It's quick & easy.

Find array value?

P: 77
Hi people!

I'm using
$key = in_array('setname', $Sname);
echo $key;

To try and echo nr 2 in the array here:
Because I need a script which can give me the position in the array of a specified object, like 'setname'.

Expand|Select|Wrap|Line Numbers
  1. Array ( 
  2. [0] => /edit-user 
  3. [1] => lone 
  4. [2] => setname 
  5. [3] => lone2 
But it doesnt work, nothing is echoed. Why is that?
The array is generated from explode(), I hope that doesnt affect it.

Thanks for insight.
May 4 '10 #1
Share this Question
Share on Google+
9 Replies

Expert Mod 5K+
P: 8,639
looks like a job for array_search().
May 5 '10 #2

P: 77
Well by using

Expand|Select|Wrap|Line Numbers
  1. $search=array_search('setname', $Sname);
  2. echo "search $search";
It doesn't echo any value except for 'search'.


But if Im testing the manual example quickly:

Expand|Select|Wrap|Line Numbers
  1. $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
  3. $key = array_search('setname', $SnameA);
  4. echo $key;
  5. // gives 2
It works fine.
Could it be because of the array $Sname is generated by an explode?
May 5 '10 #3

Expert Mod 5K+
P: 8,639
then "setname" is not in your array.
May 5 '10 #4

P: 77
Lol I tried changing 'setname' in the search to any of the other posted values, and it worked. But only on 'setname' it wont give the array position.
There is no hidden characters so I have no clue why it doesnt echo it.
The array is still as posted up top.

Edit: Heehee well it is a hidden character.

The output is formatted as

So there is a line break before setname.
Still wont show though, even if I use \r\n, \n or nl2br :(

Edit 2:

Well well, I guess I will survive if I dont use line breaks for each input, this way it works.
However with line breaks it would be easier to use what im making :(

Thanks though, using array search and it works!
May 5 '10 #5

Expert Mod 5K+
P: 8,639
so the line break is part of the string?
May 5 '10 #6

P: 77
well everything is sent from one textarea, and i would like the script to accept line breaks. so that commands could be formatted as:


So yeah, the line break seems to be a part of it when I look into the array, there's a line break before 'setname'.
explode() seperates the : parts.
May 5 '10 #7

Expert 5K+
P: 6,050
trim() your data :)
May 5 '10 #8

P: 77
Ive used this before... I can't believe I didnt remember it lol.

Thanks !
May 5 '10 #9

Expert 5K+
P: 6,050
Hehe. You're welcome.
May 5 '10 #10

Post your reply

Sign in to post your reply or Sign up for a free account.