0
array(4) { 

    [0]=> array(1) 
        { ["perm_desc"]=> string(10) "Can Delete" } 
    [1]=> array(1) 
        { ["perm_desc"]=> string(8) "Can Edit" } 
    [2]=> array(1) 
        { ["perm_desc"]=> string(10) "Can Create" } 
    [3]=> array(1) 
        { ["perm_desc"]=> string(16) "Can Manage Roles" } 
}

I am struggling to find the right way to loop over these arrays. so basically i have pulling all permissions from the database and i want it in 1 single array so i can manipulate late when needed. but the database is spittiing this data out in multiply arrays.

$results = static::customQuery($sql, ["role_id" => $role_id]);
if ($results) {
        foreach ($results as $r) {
            var_dump($r);
            die();
}

I get this back just 1 result from the original array which contains 4 array

array(1) { ["perm_desc"]=> string(10) "Can Delete" }

but really i want something like this

['Can Edit', 'Can Manage', 'Can Create', 'Can Manage Roles']

and idea would be really helpful

3
  • Are you using a framework as there may be methods which can do this for you. Commented Dec 14, 2019 at 13:26
  • With PDO - you can check out stackoverflow.com/questions/6047724/… Commented Dec 14, 2019 at 13:36
  • I am using a CMS which I am building myself I am not far off finishing it. This is for my personal projects Commented Dec 14, 2019 at 13:42

2 Answers 2

1

array_column was made for this:

$results = array_column($results, 'perm_desc');
Sign up to request clarification or add additional context in comments.

1 Comment

Sweet that is more easier than i thought 1 line of code thanks for that. think i am going to use your suggestion has its much better than writing a foreach loop
0

I manage to fix the issue

static function results()
{
    if (static::exec()) {
        $results = static::$stmt->fetchAll();
        return $results;
   }
}

this is a part of my code base I change it to

static function results()
{
    if (static::exec()) {
        $results = static::$stmt->fetchAll(PDO::FETCH_ASSOC);
        return $results;
   }
}

by adding PDO::FETCH_ASSOC

did my looping in my Roles class

$results = static::customQuery($sql, ["role_id" => $role_id]);
    if ($results) {
        foreach ($results as $result) {
            $role->permissions[$result["perm_desc"]] = true;
        }

    }
    var_dump($role);
    die();

and now i am getting back the desired results

    object(Core\Layers\Roles)#14 (1) { 
["permissions":protected]=> 
array(8) { 
["Can Delete"]=> bool(true) 
["Can Edit"]=> bool(true) 
["Can Create"]=> bool(true) 
["Can Manage Roles"]=> bool(true) 
["Can Backup"]=> bool(true) 
["Can Download"]=> bool(true) 
["Create Menu"]=> bool(true) 
["test"]=> bool(true) 
} 
}

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.