Laravel Collection Array of objects to simple Array method - laravel

I have $result variable assigned with collection of array objects like this :
// below is the $result value i.e. output of print_r($result);
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [email] => test#test.com ) [1] => stdClass Object ( [email] => test#test.com ) [2] => stdClass Object ( [email] => test#test.com ) [3] => stdClass Object ( [email] => test#test.com ) [4] => stdClass Object ( [email] => test#test.com ) [5] => stdClass Object ( [email] => test#test.com ) ) )
In order to covert above to simple array I am using loop like below :
print_r($result);
$user_emails = array();
foreach ($result as $key => $value) {
array_push($user_emails,$value->email);
}
print_r($user_emails);
and I get $user_emails value as below :
Array ( [0] => test#test.com [1] => test#test.com [2] => test#test.com [3] => test#test.com [4] => test#test.com [5] => test#test.com )
I want to know if there is better and faster way to achieve what I have done above, using any of the laravel collection method.

You can try that:
$result->pluck('email')->all();

finally this worked for me :
$result->pluck('email')->toArray();

Related

How do I get array format instead of object array returned?

I have Laravel 6 code below displays the array with the object format. So I like to display instead of object-based array, just array as I have mentioned below.
$ins_affiliations = DB::table('ins_affiliations as af')
->select('af.id','af.name')
->where(['af.ins_category_id' => $ins_category_type[0]->category_id])
->get()->toArray();
my code shows
Array
(
[0] => stdClass Object
(
[id] => 12
[name] => NSDC
)
[1] => stdClass Object
(
[id] => 13
[name] => NCVT
)
[2] => stdClass Object
(
[id] => 14
[name] => AICTE
)
[3] => stdClass Object
(
[id] => 15
[name] => Others
)
)
what I need
Array
(
[12] => NSDC
[5] => NCVT
[8] => AICTE
[11] => Others
)
How can I achieve this?
You can remove ->toArray() when you retrieve data and then use mapWithKeys which is collection method
$ins_affiliations->mapWithKeys(function ($item) { return [$item->id => $item->name]; })
Then you can use ->all() if you want to get plain array

Laravel Array output [0] => 1 [1] => 2 [2] => 3 [3] => 4

I displayed the data from database using the following code
$mani = DB::select('select id from employee');
Output :
Array (
[0] => stdClass Object (
[id] => 1
)
[1] => stdClass Object (
[id] => 2
)
[2] => stdClass Object (
[id] => 3
)
[3] => stdClass Object (
[id] => 4
)
)
I need the following Exact output :
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Please share the solution. Thank you
Change your code to use pluck() instead, so change
$mani = DB::select('select id from employee');
to
$mani = DB::table('employee')->pluck('id');
If you are using laravel-5.2, then change
$mani = DB::table('employee')->lists('id');
But if you are using laravel 5.2 and greater version
$mani = DB::table('employee')->plunk('id');

how sort this type of associate array by "department_id"

Array
(
[0] => stdClass Object
(
[id] => 16
[department_id] => 4
[employee_status] => 1
)
[1] => stdClass Object
(
[id] => 17
[department_id] => 8
[employee_status] => 1
)
)
i found a solution that sort object array
function cmp($a, $b) {
return $b->department_id - $a->department_id;
}
$sirtedArray=usort($employeelist, "cmp");

converting array to stdClass

I am getting the result below. i want to print the question and cat_id but don't know how??? Actually i'm new in codeigniter. Any help will be appreciated .
Array
(
[data] => Array
(
[0] => Array
(
[records] => Array
(
[0] => stdClass Object
(
[qst_id] => 1
[question] => Describe a major change that occurred in a job that you held. How did you adapt to this change?
[cat_id] => 1
)
[1] => stdClass Object
(
[qst_id] => 2
[question] => Tell about a situation in which you had to adjust to changes over which you had no control. How did you handle it?
[cat_id] => 1
)
)
[cat_id] => 1
)
[1] => Array
(
[records] => Array
(
)
[cat_id] => 4
)
[2] => Array
(
[records] => Array
(
[0] => stdClass Object
(
[qst_id] => 3
[question] => Give an example of a situation where others were intense but you were able to maintain your composure.
[cat_id] => 6
)
)
[cat_id] => 6
)
)
)

multi-level comments error when deleting items from database

I have implemented a multi-level comment system where comments and their replies are pulled out of a SQL database and stored (via a PHP routine) in an array. Here is an example of the data structure of a comment which has 2 nested replies (main comment -> [child] reply -> [child] reply to reply):
Array ( [0] => stdClass Object ( **[self]** => stdClass Object ( [id] => 1 [page_id] => 47iNetBet [parent_id] => 0 [name] => adam 1 [email] => web#web.de [url] => [comment] => main comment [dt] => 2012-02-23 13:05:13 [ip] => 127.0.0.1 ) **[childs]** => Array ( [2] => stdClass Object ( [self] => stdClass Object ( [id] => 2 [page_id] => 47iNetBet [parent_id] => 1 [name] => adam 2 [email] => web#web.de [url] => [comment] => 1st reply [dt] => 2012-02-23 13:05:25 [ip] => 127.0.0.1 ) **[childs]** => Array ( [3] => stdClass Object ( [self] => stdClass Object ( [id] => 3 [page_id] => 47iNetBet [parent_id] => 2 [name] => adam 3 [email] => web#web.de [url] => [comment] => 2nd reply [dt] => 2012-02-23 13:05:35 [ip] => 127.0.0.1 ) **[childs]** => Array ( ) ) ) ) ) ) )
Each comment's parent_id refers to the id of its parent. If parent_id is zero then it's a main comment. This works fine - so far I have been able to add as many nested comments as I want without any problems. But it seems that as soon as I delete something from my database or even delete all the comments and then start adding new comments (even the exact same comments ids, parent_ids etc.) at some point the structure gets messed up. Here is the same example after deleting comments from database and repopulating database with the same comments data:
Array ( [0] => stdClass Object ( **[self]** => stdClass Object ( [id] => 1 [page_id] => 47iNetBet [parent_id] => 0 [name] => adam 1 [email] => web#web.de [url] => [comment] => main comment [dt] => 2012-02-23 13:05:13 [ip] => 127.0.0.1 ) **[childs]** => Array ( [2] => stdClass Object ( [self] => stdClass Object ( [id] => 2 [page_id] => 47iNetBet [parent_id] => 1 [name] => adam 2 [email] => web#web.de [url] => [comment] => 1st reply [dt] => 2012-02-23 13:14:40 [ip] => 127.0.0.1 ) **[childs] => Array (EMPTY???) ) ) )** [1] => stdClass Object ( [childs] => Array ( [3] => stdClass Object ( [self] => stdClass Object ( [id] => 3 [page_id] => 47iNetBet [parent_id] => 2 [name] => adam 3 [email] => web#web.de [url] => [comment] => 2nd reply [dt] => 2012-02-23 13:14:47 [ip] => 127.0.0.1 ) [childs] => Array ( ) ) ) ) )
As you can see in the latter example, the second child (main comment -> reply -> reply to reply) is an empty array. What should be in this array is now put at the end as some sort of independent/incomplete comment which breaks the flow of the display. If I drop the "comments" table and start a new one everything seems to work fine again until I start deleting stuff again.
I dont know how to explain, but this is weird. I cant imagine that there is something wrong with the code as everything seems to work fine otherwise. Could this have to do anything with the database itself? Maybe someone had a similar problem? Maybe someone could point me in the right direction. I dont even know where to start looking.
Any help, suggestions would be much appreciated.
Problem solved. It was indeed an error within the code. The records in the database need to be fetched in a certain order (sorted by parent_id in ascending order). It was as simple as that.

Resources