$response['inserted_data'] =
array:3 [
0 => array:16 [
"name" => "Khv"
"emails" => array:1 [
0 => array:1 [
"email" => "demo#yahoo.com"
]
]
]
1 => array:18 [
"name" => "Aesha"
"emails" => array:1 [
0 => array:1 [
"email" => "test2#gmail.com"
]
]
]
How to get email values demo#yahoo.com and test2#gmail.com using pluck - php laravel and then from that emails need to get _id from database and update that records.
Try the below code :
Assuming your data look likes below array i.e $arrays.
$arrays=[
["name" => "Khv","emails" =>
[0 => ["email" => "demo1#yahoo.com"]
]
],
["name" => "asy", "emails" =>
[0 => ["email" => "demo2#yahoo.com"]
]
]
];
$emails=collect();
foreach ($arrays as $array) {
$emails->push($array['emails'][0]['email']);
}
dd($emails);
Note : You can't use pluck on array, it can be only used on collection.
You get the value from the pluck like below way.
User::all()->pluck('email');
Related
I have an API request like this:
$postInformation =
(new postInformation)->handle(['field' => 'slug', 'value' => $this->slug]);
A dump of this response, besides other things, show some categories that are coming from this API request. And they are on this format where there is a 'categories' key with the top categories:
categories:
^ array:40 [
"id" => 2
...
"categories" => array:2 [
"data" => array:15 [
0 => array:3 [
"id" => 6
"name" => array:1 [
"en" => "General"
]
"on_request" => 0
]
1 => array:3 [
"id" => 14
"name" => array:1 [
"en" => "Tuts"
]
"on_request" => 0
]
2 => array:3 [
"id" => 3
"name" => array:1 [
"en" => "Laravel"
]
"on_request" => 0
]
...
]
...
]
I created a table 'post_top_categories' and a model PostTopCategory and I want to get the top categories from the API response above and store them into the 'post_top_categories' table. But I'm not understanding how to properly achieve this. Do you know how it can be achieved it? Thanks
foreach($yourArray['categories']['data'] as $topCategory)
{
$catId = $topCategory['id'];
$catname = $topCategory['name']['en'];
$catOnRequest = $topCategory['on_request'];
// Do what you want with those values now
}
I have an array like below :
array:3 [
"2021-08-07" => array:3 [
"id" => "1"
"date" => "2021-08-07"
"numbers" => array:2 [
0 => 1
1 => 2
]
]
"2021-08-08" => array:3 [
"id" => "1"
"date" => "2021-08-08"
"numbers" => array:2 [
0 => 1
1 => 2
]
]
]
What I want to do is simply to remote the parent 2021-08-08 items because I have the date inside the array already . Now, what I have tried so far is :
$result = array_map(function ($el){
return $el[0];
},$test);
dd($result);
But it gives me error of undefined index[0] . what I want this array to look like is like below :
array:3 [
"id" => "1"
"date" => "2021-08-07"
"numbers" => array:2 [
0 => 1
1 => 2
],
"id" => "1"
"date" => "2021-08-08"
"numbers" => array:2 [
0 => 1
1 => 2
]
]
Won't just array_values() do?
array_values($test);
$test = array_values(yourArray);
$test = (object)$yourArray;
I have an array(or collection) like this for my notifications. Each notification has an array field for all users who has seen the notification.
array:2 [
0 => array:8 [
"_id" => "604485ddf92e0000050079c6"
"title" => "Notification title"
"message" => "Notification message"
"type" => "success"
"expire_date" => "2021-05-09T19:30:00.000000Z"
"read_by_users" => array:2 [
0 => array:2 [
"user_id" => "601fd68d212200006f001c0b"
"seen_time" => 1615103466
]
1 => array:2 [
"user_id" => "60365402546900008a007c76"
"seen_time" => "1615103466"
]
]
"updated_at" => "2021-03-07T07:50:53.314000Z"
"created_at" => "2021-03-07T07:50:53.314000Z"
]
1 => array:8 [
"_id" => "604497d2f92e0000050079c7"
"title" => "Next Notification title"
"message" => "Next Notification message"
"type" => "success"
"expire_date" => "2021-05-09T19:30:00.000000Z"
"read_by_users" => array:1 [
0 => array:2 [
"user_id" => "601fd68d212200006f001c0b"
"seen_time" => 1615187929
]
]
"updated_at" => "2021-03-07T09:07:30.066000Z"
"created_at" => "2021-03-07T09:07:30.066000Z"
]
]
Now I want to get notifications where has not seen by current user (for example with this user_id 60365402546900008a007c76).
Any solution would be apreciated 🙏.
My project is in Laravel 8 and MongoDB.
If you want the result using array here is a solution
$userId = '60365402546900008a007c76'; $notifications = [];
foreach ($data as $notification) {
$userIds = array_column($notification['read_by_users'], 'user_id');
if (!in_array($userId, $userIds)) {
$notifications [] = $notification;
}
dd($notifications);
In my project with Symfony2, I'm using Doctrine createNativeQuery, and I would like to get an associative array.
This is my code
$rsm = new ResultSetMapping;
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('name', 'name');
$rsm->addScalarResult('phone_one', 'phoneOne');
$rsm->addScalarResult('phone_two', 'phoneTwo');
I have this result:
array:2 [
0 => array:4 [
"id" => "975"
"name" => "one name"
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
1 => array:4 [
0 => array:4 [
"id" => "976"
"name" => "two name"
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
]
It's posible this result?
array:2 [
0 => array:4 [
"id" => "975"
"name" => "one name"
"phones" => [
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
]
1 => array:4 [
0 => array:4 [
"id" => "976"
"name" => "two name"
"phones" => [
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
]
]
Thanks a lot
If that is the result you want, why not create a OneToMany for Users to Phones? I would highly suggest you do that.
I have 2 arrays $projectsIds = [1,2] and $usersIds = [1,2,3]
I have nasted foreach loop like this:
foreach($projectsIds as $projecID){
foreach($userIds as $userID){
$sync_array[$projecID] = array('user_id' => $userID);
}
}
dd($sync_array);
now I get result like this:
array:2 [ 1 => array:1 [ "user_id" => 1 ]
2 => array:1 [ "user_id" => 1 ]
]
Is possible to get result something like this:
array:2 [ 1 => array:1 [ "user_id" => 1 ]
1 => array:1 [ "user_id" => 2 ]
1 => array:1 [ "user_id" => 3 ]
2 => array:1 [ "user_id" => 1 ]
2 => array:1 [ "user_id" => 2 ]
2 => array:1 [ "user_id" => 3 ]
]
You wish to make an array in PHP having two or more of instances of the same key, which is not possible at all!
Keys are meant to be treated as unique entities