design pattern for iteration php [closed]
Clash 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();
php design-patterns
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
add a comment |Â
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();
php design-patterns
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
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 changethis->contact
tothis->contacts
â Holger
Jun 25 at 9:11
add a comment |Â
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();
php design-patterns
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();
php design-patterns
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
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
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 changethis->contact
tothis->contacts
â Holger
Jun 25 at 9:11
add a comment |Â
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 changethis->contact
tothis->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
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
tothis->contacts
â Holger
Jun 25 at 9:11