3

I have two arrays, one indexed and one associative. What my question boils down to is, how can I pass the associative array's reference to the edit class. This way when there are more books and movies, I can loop through, clean all the isbn's and not touch movie. The problem I'm having is passing the reference inside the for loop.

$i = new intro();

class intro{
  public function __construct(){
    $index = array(array("book", "regex"), array("movie", "regex"));
    $assoc = array(array("book"=>"freeBSD", "isbn"=>"01-2345-6789"), 
                   array("movie"=>"batman", "date"=>"10-10-1995");

    for($x = 0; $x < count($index); $x++){
      if($index[$x]["book"] == key($assoc)){
        edit::modify(current($assoc)); //I WANT TO PASS THE REFERENCE NOT VALUE
      }                                //current(&$assoc) DOES NOT WORK 
      next($assoc);
    }
  }
}

class edit{
  public function modify(&$isbn){
    $pattern = "/[^0-9]*/";
    $isbn = preg_replace($pattern, "", $isbn);
  }
}
1
  • 1
    how about &$assoc[key($assoc)] ? Commented Sep 22, 2011 at 6:01

1 Answer 1

5

Posting it here as reference since this was solved in the comments

doing &$assoc[key($assoc)] will solve the problem.

for($x = 0; $x < count($index); $x++){
  if($index[$x]["book"] == key($assoc)){
    edit::modify(&$assoc[key($assoc)]);
  }                                
  next($assoc);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.