Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

satheeshkumarj's avatar

Get Values from Array

I need to list id from following array with typr ="Blood Donate" and status "1"

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [type] => Blood Donate
                    [completion] => 10
                    [status] => 1
                )

            [1] => Array
                (
                    [type] => Food Donate
                    [completion] => 15
                    [status] => 1
                )

            [2] => Array
                (
                    [type] => Clothes Donate
                    [completion] => 0
                    [status] => 1
                )

            [id] => 3901ee9e-01bb-483a-9f74-8f7b76290cd5
        )

    [1] => Array
        (
            [0] => Array
                (
                    [type] => Blood Donate
                    [completion] => 10
                    [status] => 0
                )

            [1] => Array
                (
                    [type] => Food Donate
                    [completion] => 15
                    [status] => 1
                )

            [2] => Array
                (
                    [type] => Clothes Donate
                    [completion] => 0
                    [status] => 1
                )

            
            [id] => f2772366-4e7f-4257-90bd-8ea506dd8f84
        )

)
0 likes
17 replies
satheeshkumarj's avatar

@Tray2 I got the values from DataBase as Json sting. I done json_decode to get this array value

MohamedTammam's avatar
collect($yourArray)->filter(fn ($item) => $item['type'] == 'Blood Donate' && $item['status'] == 1)->pluck('id');

And as @tray2 mentioned, if you're getting that from database it's better to do the filtering there.

Sinnbeck's avatar

That looks like a weird mix of numeric keys and string keys (id)? How did you create such an array?

Sinnbeck's avatar

@satheeshkumarj yeah, but the 0, 1, 2 seems to be on the same level as id? Show the code for how the array is created

satheeshkumarj's avatar

In my users table "profile_setting" field is entered as json string.

[{"type":"Blood Donate","completion":"10","status":"1"},{"type":"Food Donate","completion":"15","status":"1"},{"type":"Clothes Donate","completion":"0","status":"1"},{"type":"Toy Donate","completion":"0","status":"0"},{"type":"Food Donate","completion":"0","status":"1"},{"type":"Organ Donate","completion":"0","status":"0"}]

I need to get users who have type "Blood Donation" with status 1.

$userSetting = DB::table('users')
            ->select('profile_setting', 'id')
            ->whereIn('id', explode(',',$UidList)) //array of user id
            ->where('profile_setting', '!=', null)
            ->get();

output is

Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [profile_setting] => [{"type":"Blood Donate","completion":"10","status":"1"},{"type":"Food Donate","completion":"15","status":"1"},{"type":"Clothes Donate","completion":"0","status":"1"},{"type":"Toy Donate","completion":"0","status":"0"},{"type":"Food Donate","completion":"0","status":"1"},{"type":"Organ Donate","completion":"0","status":"0"}] [id] => 3901ee9e-01bb-483a-9f74-8f7b76290cd5 ) [1] => stdClass Object ( [profile_setting] => [{"type":"Blood Donate","completion":"10","status":"0"},{"type":"Food Donate","completion":"15","status":"1"},{"type":"Clothes Donate","completion":"0","status":"1"},{"type":"Toy Donate","completion":"0","status":"0"},{"type":"Food Donate","completion":"0","status":"1"},{"type":"Organ Donate","completion":"0","status":"0"}] [id] => f2772366-4e7f-4257-90bd-8ea506dd8f84 ) [2] => stdClass Object ( [profile_setting] => [{"type":"Blood Donate","completion":"10","status":"1"},{"type":"Food Donate","completion":"15","status":"1"},{"type":"Clothes Donate","completion":"0","status":"1"},{"type":"Toy Donate","completion":"0","status":"0"},{"type":"Food Donate","completion":"0","status":"1"},{"type":"Organ Donate","completion":"0","status":"0"}] [id] => f8b20d31-ac80-4a98-b561-d255c79236fd ) [3] => stdClass Object ( [profile_setting] => [{"type":"Blood Donate","completion":"10","status":"1"},{"type":"Food Donate","completion":"15","status":"1"},{"type":"Clothes Donate","completion":"0","status":"1"},{"type":"Toy Donate","completion":"0","status":"0"},{"type":"Food Donate","completion":"0","status":"1"},{"type":"Organ Donate","completion":"0","status":"0"}] [id] => 15a31589-bba6-4e22-a5c2-1dcd13f43cfe ) ) [escapeWhenCastingToString:protected] => )

after that I have done for loop to store to single array

for($i=0;$i<count($userSetting);$i++){
                $settingsVal[] = json_decode($userSetting[$i]->profile_setting, true);
                $settingsVal[$i]['id'] = $userSetting[$i]->id;
            }
Sinnbeck's avatar

@satheeshkumarj ok then I hope you only ever need to get a few rows and filter those. If you ever need all rows that has blood donate, you might run into problems as you would need to get every single row. I will write an example of how to do it in a moment then

1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Ok. Here is my suggested solution then

First add a cast to the model, to have it automatically turn it into an array

protected $casts = [
    'profile_setting' => 'array',
];

Then use eloquent to get the data

$users = User::query
            ->select('profile_setting', 'id')
            ->whereIn('id', explode(',',$UidList)) //array of user id
            ->where('profile_setting', '!=', null)
            ->get();

and then filter it

$ids = $user->filter(function($user) {
      foreach ($user->profile_setting as $setting) {
          if ($setting['type'] == 'Blood Donate' && $setting['status'] == 1) {
             return true;
        }
       return false;
    }
})->pluck('id');
1 like
satheeshkumarj's avatar

@Sinnbeck Actually value 'Blood Donate' posted from drop down filed. Which may change. So I took

$category = $request->category;

I am getting values like Blood Donate, Organ Donate.. etc When I used variable category inside function . it shows undefined variable $category

$ids = $user->filter(function($user) {
            foreach ($user->profile_setting as $setting) {
                if ($setting['type'] == $category && $setting['status'] == 1) {
                    return true;
                }
                    return false;
            }
            })->pluck('id');

Error showing undefined variable $category

Sinnbeck's avatar

@satheeshkumarj Yes. That is due to scope. Your function cannot access valiables form the outsite.

You can add them with use() (first line)

$ids = $user->filter(function($user) use ($category) {
            foreach ($user->profile_setting as $setting) {
                if ($setting['type'] == $category && $setting['status'] == 1) {
                    return true;
                }
                    return false;
            }
            })->pluck('id');
1 like

Please or to participate in this conversation.