473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Find array value?

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
9 2135
Dormilich
8,658 Expert Mod 8TB
looks like a job for array_search().
May 5 '10 #2
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');
  2.  
  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
Dormilich
8,658 Expert Mod 8TB
then "setname" is not in your array.
May 5 '10 #4
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
/edit-user:lone:
setname:lone2:

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
Dormilich
8,658 Expert Mod 8TB
so the line break is part of the string?
May 5 '10 #6
well everything is sent from one textarea, and i would like the script to accept line breaks. so that commands could be formatted as:

/edit-user:Name:
setname:NewName:

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
Markus
6,050 Expert 4TB
trim() your data :)
May 5 '10 #8
Ive used this before... I can't believe I didnt remember it lol.

Thanks !
May 5 '10 #9
Markus
6,050 Expert 4TB
Hehe. You're welcome.
May 5 '10 #10

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

Similar topics

8
by: J. B. Moreno | last post by:
What's the best (i.e. fastest) way to find out if an array contains a given value? Other than looping, the only way I know to do it is to use an associative array/hash instead.... Is there a...
5
by: cai_rongxi | last post by:
Anyone has the solution to the question "Given an array a with value 1...N, find a repeted value" in C code? Thanks a lot
13
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number...
4
by: Arjen | last post by:
Hi, I need to add this inside an array: 1 3 2 4 3 2 4 5 5 1 I think of using this:
2
by: Ros | last post by:
Peeps, I need help with trying to find a value in array of arrays. Public Module myModule Private Site_Access() As Array Property Set_SA() Get Return Site_Access End Get
2
by: elissa | last post by:
hi guys i really wish if some one could help me with this problem , well i solved it but the output is always wrong so can some one help me: the Question is in this link:...
30
by: =?Utf-8?B?VGhvbWFzVDIy?= | last post by:
Hello, I have an array that holds 4 values ( they contain random numbers). I would like to find the lowest value (if there is a tie i would like to find one of the tie.) then remove that value....
2
choke
by: choke | last post by:
I need to write a function in devc++ that creates an array of n integers, each element is equal to n*n+i*i-n*i where i is from 0 to n-1 as the array index. Within the same function I need to find...
3
by: raylopez99 | last post by:
I suspect the answer to this question is that it's impossible, but how do I make the below code work, at the point where it breaks (marked below). See error CS0411 This is the complete code. ...
4
by: raylopez99 | last post by:
I would like to know if there's a quick "Linq" way to find the index of an array having a particular value. I can do this the long way by sequential iteration, but would like to know if there's a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.