Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Accumulate strings and array values into an array as a class property via method

I have a class with method add() that accepts strings and arrays. I need to have an array with all users, but I cannot seem to get it. All I get is multiple arrays with all users. How could I merge those arrays into one?

class Users {

    function add($stringOrArray) {
        $arr = array();
        if(is_array($stringOrArray)) {
            $arr = $stringOrArray;
            
        } else if(is_string($stringOrArray)) {
            $arr[] = $stringOrArray;
            
        } else {
          echo('errrrror');
        }
        print_r($arr);
        
    }

When I use this test:

public function testOne() {
    $users = new Users();
    $users->add('Terrell Irving');
    $users->add('Magdalen Sara Tanner');
    $users->add('Chad Niles');
    $users->add(['Mervin Spearing', 'Dean Willoughby', 'David Prescott']);

This is what I get, multiple arrays but I need one array.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Array
(
    [0] => Terrell Irving
)
Array
(
    [0] => Magdalen Sara Tanner
)
Array
(
    [0] => Chad Niles
)
Array
(
    [0] => Mervin Spearing
    [1] => Dean Willoughby
    [2] => David Prescott
)

>Solution :

All you need is to store the added users in a class property, for example $listOfUsers.

If adding the array you use the array_merge() function otherwise just add new user at the end of indexed array.

<?php 

class Users {

  // here will be all the users stored
  public $listOfUsers = array();

    function add($stringOrArray) {
        //$arr = array();
        if(is_array($stringOrArray)) {
            // merge two arrays - could create  duplicate records
            $this->listOfUsers = array_merge($this->listOfUsers, $stringOrArray);
            
        } else if(is_string($stringOrArray)) {
            // simply add new item into the array
            $this->listOfUsers[] = $stringOrArray;
            
        } else {
          echo('errrrror');
        }
        print_r($this->listOfUsers);
    }
}

In your example you are storing the data locally within the method add() and it is not kept for future usage. This behavior is corrected using the class property $listOfUsers that can be accesed using $this->listOfUsers within the class object and if needed outside of the class.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading