Find array value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PHPstarter
    New Member
    • Jun 2009
    • 77

    Find array value?

    Hi people!

    I'm using
    $key = in_array('setna me', $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'.

    Code:
    Array ( 
    [0] => /edit-user 
    [1] => lone 
    [2] => setname 
    [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.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    looks like a job for array_search().

    Comment

    • PHPstarter
      New Member
      • Jun 2009
      • 77

      #3
      Well by using

      Code:
      $search=array_search('setname', $Sname);
      echo "search $search";
      It doesn't echo any value except for 'search'.

      :/

      But if Im testing the manual example quickly:

      Code:
      $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
      
      $key = array_search('setname', $SnameA);
      echo $key;
      // gives 2
      It works fine.
      Could it be because of the array $Sname is generated by an explode?
      Last edited by PHPstarter; May 5 '10, 10:17 AM.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        then "setname" is not in your array.

        Comment

        • PHPstarter
          New Member
          • Jun 2009
          • 77

          #5
          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!

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            so the line break is part of the string?

            Comment

            • PHPstarter
              New Member
              • Jun 2009
              • 77

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

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                trim() your data :)

                Comment

                • PHPstarter
                  New Member
                  • Jun 2009
                  • 77

                  #9
                  Ive used this before... I can't believe I didnt remember it lol.

                  Thanks !

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Hehe. You're welcome.

                    Comment

                    Working...