design pattern for iteration php [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I have this class where I can perform two loops on the same associative array, is there a design pattern that would accomplish this but with only one loop?



I was thinking it would be Iterator class and then injecting functionality to run on each iteration?



class Contacts 

public __construct($contacts)

$this->contacts = $contacts;


public function changeEmailDomain($from, $to)

foreach($this->contacts as $key => $contact)
$this->contacts[$key]['email'] = str_replace($from, $to, $contact['email']);



public function splitName()

foreach($this->contacts as $key => $contact)
$names = explode(' ',$contact['name']);
$this->contact[$key]['firstname'] = $names[0];
$this->contact[$key]['surname'] = $names[1];




$contactData = [
[
"name" => "Peter Parker",
"email" => "peterparker@mail.com",
],
[
"name" => "Clark Kent",
"email" => "clarkkent@mail.com",
],
[
"name" => "Harry Potter",
"email" => "harrypotter@mail.com",
]];

$contacts = new Contacts($contactData);
$contacts->changeEmailDomain('mail.com', 'outlook.com');
$contacts->splitName();






share|improve this question













closed as off-topic by Toby Speight, Billal BEGUERADJ, Stephen Rauch, hjpotter92, Hosch250 Jun 22 at 14:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Billal BEGUERADJ, hjpotter92, Hosch250
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 2




    Nobody answered this at SO either. Is there a way I can get an answer somewhere? Any recommendations?
    – jim smith
    Jun 25 at 7:40










  • Put the line of changeEmailDomain() into the loop of splitNames() and change splitNames() to doAll($from, $to). And change this->contact to this->contacts
    – Holger
    Jun 25 at 9:11
















up vote
1
down vote

favorite












I have this class where I can perform two loops on the same associative array, is there a design pattern that would accomplish this but with only one loop?



I was thinking it would be Iterator class and then injecting functionality to run on each iteration?



class Contacts 

public __construct($contacts)

$this->contacts = $contacts;


public function changeEmailDomain($from, $to)

foreach($this->contacts as $key => $contact)
$this->contacts[$key]['email'] = str_replace($from, $to, $contact['email']);



public function splitName()

foreach($this->contacts as $key => $contact)
$names = explode(' ',$contact['name']);
$this->contact[$key]['firstname'] = $names[0];
$this->contact[$key]['surname'] = $names[1];




$contactData = [
[
"name" => "Peter Parker",
"email" => "peterparker@mail.com",
],
[
"name" => "Clark Kent",
"email" => "clarkkent@mail.com",
],
[
"name" => "Harry Potter",
"email" => "harrypotter@mail.com",
]];

$contacts = new Contacts($contactData);
$contacts->changeEmailDomain('mail.com', 'outlook.com');
$contacts->splitName();






share|improve this question













closed as off-topic by Toby Speight, Billal BEGUERADJ, Stephen Rauch, hjpotter92, Hosch250 Jun 22 at 14:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Billal BEGUERADJ, hjpotter92, Hosch250
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 2




    Nobody answered this at SO either. Is there a way I can get an answer somewhere? Any recommendations?
    – jim smith
    Jun 25 at 7:40










  • Put the line of changeEmailDomain() into the loop of splitNames() and change splitNames() to doAll($from, $to). And change this->contact to this->contacts
    – Holger
    Jun 25 at 9:11












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have this class where I can perform two loops on the same associative array, is there a design pattern that would accomplish this but with only one loop?



I was thinking it would be Iterator class and then injecting functionality to run on each iteration?



class Contacts 

public __construct($contacts)

$this->contacts = $contacts;


public function changeEmailDomain($from, $to)

foreach($this->contacts as $key => $contact)
$this->contacts[$key]['email'] = str_replace($from, $to, $contact['email']);



public function splitName()

foreach($this->contacts as $key => $contact)
$names = explode(' ',$contact['name']);
$this->contact[$key]['firstname'] = $names[0];
$this->contact[$key]['surname'] = $names[1];




$contactData = [
[
"name" => "Peter Parker",
"email" => "peterparker@mail.com",
],
[
"name" => "Clark Kent",
"email" => "clarkkent@mail.com",
],
[
"name" => "Harry Potter",
"email" => "harrypotter@mail.com",
]];

$contacts = new Contacts($contactData);
$contacts->changeEmailDomain('mail.com', 'outlook.com');
$contacts->splitName();






share|improve this question













I have this class where I can perform two loops on the same associative array, is there a design pattern that would accomplish this but with only one loop?



I was thinking it would be Iterator class and then injecting functionality to run on each iteration?



class Contacts 

public __construct($contacts)

$this->contacts = $contacts;


public function changeEmailDomain($from, $to)

foreach($this->contacts as $key => $contact)
$this->contacts[$key]['email'] = str_replace($from, $to, $contact['email']);



public function splitName()

foreach($this->contacts as $key => $contact)
$names = explode(' ',$contact['name']);
$this->contact[$key]['firstname'] = $names[0];
$this->contact[$key]['surname'] = $names[1];




$contactData = [
[
"name" => "Peter Parker",
"email" => "peterparker@mail.com",
],
[
"name" => "Clark Kent",
"email" => "clarkkent@mail.com",
],
[
"name" => "Harry Potter",
"email" => "harrypotter@mail.com",
]];

$contacts = new Contacts($contactData);
$contacts->changeEmailDomain('mail.com', 'outlook.com');
$contacts->splitName();








share|improve this question












share|improve this question




share|improve this question








edited Jun 25 at 7:39
























asked Jun 22 at 12:29









jim smith

62




62




closed as off-topic by Toby Speight, Billal BEGUERADJ, Stephen Rauch, hjpotter92, Hosch250 Jun 22 at 14:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Billal BEGUERADJ, hjpotter92, Hosch250
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Toby Speight, Billal BEGUERADJ, Stephen Rauch, hjpotter92, Hosch250 Jun 22 at 14:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Billal BEGUERADJ, hjpotter92, Hosch250
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 2




    Nobody answered this at SO either. Is there a way I can get an answer somewhere? Any recommendations?
    – jim smith
    Jun 25 at 7:40










  • Put the line of changeEmailDomain() into the loop of splitNames() and change splitNames() to doAll($from, $to). And change this->contact to this->contacts
    – Holger
    Jun 25 at 9:11












  • 2




    Nobody answered this at SO either. Is there a way I can get an answer somewhere? Any recommendations?
    – jim smith
    Jun 25 at 7:40










  • Put the line of changeEmailDomain() into the loop of splitNames() and change splitNames() to doAll($from, $to). And change this->contact to this->contacts
    – Holger
    Jun 25 at 9:11







2




2




Nobody answered this at SO either. Is there a way I can get an answer somewhere? Any recommendations?
– jim smith
Jun 25 at 7:40




Nobody answered this at SO either. Is there a way I can get an answer somewhere? Any recommendations?
– jim smith
Jun 25 at 7:40












Put the line of changeEmailDomain() into the loop of splitNames() and change splitNames() to doAll($from, $to). And change this->contact to this->contacts
– Holger
Jun 25 at 9:11




Put the line of changeEmailDomain() into the loop of splitNames() and change splitNames() to doAll($from, $to). And change this->contact to this->contacts
– Holger
Jun 25 at 9:11















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation