0

My Problem

Trying to create similar arrays so I can compare them.

I am creating a distribution list for files, the adding of the distribution list work fine, I list user surname, user forename and user department, these are selected and posted, foreach user I retrieve the users.user_id and store this in a distribution table (under distro_lis)t like so.

distro_id (AI, PK) | distro_list | policy_id
---------------------------------------------
        1          | 1 23 21 34  |     13
        2          | 10 22 21 34 |     15
        3          | 1 27 26 40  |     34

Now I working on the editing of the distribution list, so for a row, lets say row 1, I fetch the distro_list and parse it to get each user ID (from distro_list) using.

$policyID is received in the method argument

$db = $this->dbh->prepare('SELECT distro_list FROM distro WHERE policy_id = :policy_id');
        $db->bindParam(':policy_id', $policyID);
        $db->execute();
        $row = $db->fetch(PDO::FETCH_ASSOC);

$ids = explode(' ',$row);
foreach ($ids as $user) {
            $db = $this->dbh->prepare('SELECT user_forename, user_surname, user_dept FROM users WHERE user_id = :user_id ORDER BY user_surname ASC');
        $db->bindParam(':user_id', $user);
        $db->execute();
        $row = $db->fetch(PDO::FETCH_ASSOC);
        var_dump($row);
        }

the var_dump($row) returns an array of arrays (objects.. not sure of the terminology here) which looks like so (print_r).

Array
(
    [user_forename] => fname1
    [user_surname] => sname1
    [user_dept] => dept1
)
Array
(
    [user_forename] => fname2
    [user_surname] => sname2
    [user_dept] => dept2
)
Array
(
    [user_forename] => fname3
    [user_surname] => sname3
    [user_dept] => dept3
)

the array that I want to compare it to looks like so (print_r).

array
(
    [0] => Array
        (
            [user_forename] => fname1
            [user_surname] => sname1
            [user_dept] => dept1
        )

    [1] => Array
        (
            [user_forename] => fname2
            [user_surname] => sname2
            [user_dept] => dept2
        )

    [2] => Array
        (
            [user_forename] => fname3
            [user_surname] => sname3
            [user_dept] => dept3
        )
)

I understand why the first array looks like that, because im var_dump'ing' after each iteration. How can I get the first array (array of arrays or objects) into a single array so I can compare it to the second array?

1 Answer 1

2

Try this

$list_users = array()
foreach ($ids as $user) {
    //...
    $list_users[] = $row;
}

var_dump($list_users);
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.