SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot virginiamonkeys's Avatar
    Join Date
    Jun 2003
    Location
    Stafford, Virginia, USA
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Find String in Array Value and Move Position. How?

    I have a multidimensional array with values as such:

    Code:
    array(4) {
      [0]=>
      array(7) {
        [0]=>
        string(xx) "sample *catchword* sample"
        [1]=>
        string(xx) "test *catchword*"
        [2]=>
        string(xx) "text here catchword *catchword* text here"
        [3]=>
        string(xx) "just regular text"
        [4]=>
        string(xx) "*catchword* and text"
        [5]=>
        string(xx) ""
        [6]=>
        string(xx) "more *catchword* text"
      [1]=>
      array(7) {
        [0]=>
        string(xx) "sample *catchword* sample"
        [1]=>
        string(xx) "test *catchword*"
        [2]=>
        string(xx) "text here catchword *catchword* text here"
        [3]=>
        string(xx) "just regular text"
        [4]=>
        string(xx) "*catchword* and text"
        [5]=>
        string(xx) ""
        [6]=>
        string(xx) "more *catchword* text"
      [2]=>
      array(7) {
        [0]=>
        string(xx) "sample *catchword* sample"
        [1]=>
        string(xx) "test *catchword*"
        [2]=>
        string(xx) "text here catchword *catchword* text here"
        [3]=>
        string(xx) "just regular text"
        [4]=>
        string(xx) "*catchword* and text"
        [5]=>
        string(xx) ""
        [6]=>
        string(xx) "more *catchword* text"
      [3]=>
      array(7) {
        [0]=>
        string(xx) "sample *catchword* sample"
        [1]=>
        string(xx) "test *catchword*"
        [2]=>
        string(xx) "text here catchword *catchword* text here"
        [3]=>
        string(xx) "just regular text"
        [4]=>
        string(xx) "*catchword* and text"
        [5]=>
        string(xx) ""
        [6]=>
        string(xx) "more *catchword* text"
    I want to take every instance of *catchword* and move it to the end of each string. For instance, the 1st result would reposition as "sample sample *catchword*".

    I don't know where to start looking.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    First write a function that'll move the instance to the end of a string.
    Then apply that function to the array using array_walk_recursive

  3. #3
    SitePoint Zealot virginiamonkeys's Avatar
    Join Date
    Jun 2003
    Location
    Stafford, Virginia, USA
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I plan but I can't think of the function(s) in the php manual I need to look up to find the string, then to move it.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could use explode() to seperate the string into words
    then use array_search() to find if the string exists in one of the array elements. If so, unset() it and then append it to the end of the array.
    implode() it back together.

  5. #5
    SitePoint Zealot virginiamonkeys's Avatar
    Join Date
    Jun 2003
    Location
    Stafford, Virginia, USA
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know what's wrong with me today.

    I several things to find recursively. This is the latest version of my code:

    PHP Code:
    function find_phrase(&$item$key)
    {
     
    $found = (array_keys($found"catchphrase"));
    }
    array_walk_recursive($array'find_phrase');

    var_dump($found); 
    I'm getting nowhere.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simplify your problem and take it in steps. Write a function that works for a single string only, and make it work real good. Then, worry about dealing with arrays.

  7. #7
    SitePoint Zealot virginiamonkeys's Avatar
    Join Date
    Jun 2003
    Location
    Stafford, Virginia, USA
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I've been trying to do for the last 2 days. I'm getting slightly frustrated. This just isn't working at all for me.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •