I have the following multidimensional array :
meeting[$loop->index][person]are checkboxes
meeting[$loop->index][date] are input-fields
array:9 [▼
0 => array:1 [▼
"date" => null
]
1 => array:2 [▼
"person" => "Max Example"
"date" => "10.05"
]
2 => array:1 [▼
"date" => null
]
3 => array:1 [▼
"date" => null
]
4 => array:1 [▼
"date" => null
]
5 => array:1 [▼
"date" => null
]
6 => array:1 [▼
"date" => null
]
7 => array:1 [▼
"date" => null
]
8 => array:1 [▼
"date" => null
]
]
person in this case is a checkbox, so it's only there when it's checked.
Now I want to know, how many persons are 'invited' to the meeting.
So I need to count the amount of person (check how often person exists).
When only one person is invited I want to display something else than when more people are invited.
I tried it with this:
#if ($counts = array_count_values(array_flip(array_column($ticketDaten['hefte'], 'heft'))) == 1)
[...]
but if I var_dump it I only get bool(false) in both cases (only one person or more).
You should be able to simply
echo count(array_column($meeting, 'person'));
Demo on 3v4l.org
Related
Hello Geeks i am stuck here. I am beginner and want to know how to save and update array data in database.
this is laravel form requested data
I want to save data like this :
mysql database
here is my form request, i want to save data in mysql database, with new row :
"_token" => "FoyoYvFXmNAggbQuMq6PN243QS43MkY99Nq3UAni"
"description" => array:2 [▼
0 => "<h3>Shared Hosting Plan</h3>"
1 => "<h3>Cloud Hosting Plan</h3>"
]
"site_id" => array:2 [▼
0 => "5361"
1 => "5361"
]
"category_id" => array:2 [▼
0 => "1"
1 => "3"
]
"field_1" => array:2 [▶]
"field_2" => array:2 [▶]
"field_3" => array:2 [▶]
"field_4" => array:2 [▶]
"field_5" => array:1 [▶]
"price" => array:2 [▶]
"update_plan" => "Save Plans"
]```
Try like this
$save_data=[];
foreach($data['description'] as $key=>$desc){
$save_data[]=[
'description'=>$desc,
'site_id'=>$data['site_id'][$key],
'category_id'=>$data['category_id'][$key],
'feature_field_1'=>$data['field_1'][$key],
'feature_field_2'=>$data['field_2'][$key],
'feature_field_3'=>$data['field_3'][$key],
'feature_field_4'=>$data['field_4'][$key],
'feature_field_5'=>$data['field_5'][$key],
'price'=>$data['price'][$key]
]
}
\DB::table('table')->insert($save_data);
You can insert like this
Eloquent approach
Model::insert($data);
Query Builder approach
DB::table('table')->insert($data);
I Am trying to get from the candidate with the highest votes and his name also, all have tried runs false. I just dd() this below, after getting all the presidential candidates.
Illuminate\Database\Eloquent\Collection {#288 ▼
#items: array:4 [▼
0 => App\Candidate {#289 ▼
#connection: "mysql"
#table: "candidates"
#primaryKey: "id"
#keyType: "int"
#attributes: array:9 [▼
"id" => 1
"name" => "Brian"
"seat" => "president"
"regno" => "DIT-C004-0536\2013"
"votes" => 0 // this is the first candidate record, others are below.
"user_id" => 2
"created_at" => "2017-05-12 08:43:58"
"updated_at" => "2017-05-12 08:43:58"
"image" => ""
]
}
1 => App\Candidate {#290 ▼
#attributes: array:9 [▼
"id" => 5
"name" => "Juma"
"seat" => "president"
"regno" => "DIT-C004-0516\2013"
"votes" => 3
"user_id" => 7
"created_at" => "2017-05-12 09:03:03"
"updated_at" => "2017-05-12 14:48:54"
"image" => ""
]
}
2 => App\Candidate {#291 ▼
#attributes: array:9 [▼
"id" => 9
"name" => "Stephen"
"seat" => "president"
"regno" => "0001"
"votes" => 4 //trying to get the highest votes with the name.
"user_id" => 12
"created_at" => "2020-03-17 15:27:47"
"updated_at" => "2020-03-21 19:18:39"
"image" => ""
]
}
3 => App\Candidate {#292 ▼
#attributes: array:9 [▼
"id" => 10
"name" => "Gobby"
"seat" => "president"
"regno" => "DIT-C004-0436\2014"
"votes" => 0
"user_id" => 11
"created_at" => "2020-03-17 15:27:59"
"updated_at" => "2020-03-17 15:27:59"
"image" => ""
]
}
I have been searching for solutions all around but couldn't find one, Thanks in Advance.
This is my controller:
{
public function highestvote(){
$getpres = Candidate::where('seat','president')->get();
dd($getpres);
foreach($getpres as $getpresvote){
for($i = 0; $i < $getpresvote; $i++){
$highv = $getpresvote[$i];
}
}
return view('election.winners')->with('getpresvotes',$getpresvotes);
}
}
I die dump the code before looping, cause when I loop it keeps on giving me 0 or sometimes tells me stuff like array can't be converted int.
Your Controller Code should be like this in this case:
$seat = 'president';
$name= 'gobby';
$data = Candidate::where('seat', $seat)
->where('name', $name)
->orderBy('votes','desc')
->first()
print_r($data);
It will give you the maximum votes data with the seat 'president' and name 'gobby'
I have Project and Country Model. There is a many to many relations. I get projects with countries. Result is below
array:5 [▼
0 => array:5 [▼
"id" => 2
"account_id" => 1
"start_date" => "Jul 2012"
"end_date" => "Aug 2013"
"countries" => array:1 [▼
0 => array:2 [▼
"id" => 148
"pivot" => array:2 [▶]
]
]
]
1 => array:5 [▶]
2 => array:5 [▶]
3 => array:5 [▶]
4 => array:5 [▶]
]
I wont to get all unique countries count. In the now I do it with this way
$projects->pluck('countries')->collapse()->pluck('id')->unique()->count()
Question. Can I use pluck with nested relation column and has any more shortly and good solution?? for example like this
$projects->pluck('countries.id')->count();
You can use this:
$projects->pluck('countries.*.id')->flatten()->unique()->count()
You can't use:
$projects->pluck('countries.id')...
Because countries is an array of arrays.
But you can use the 'countries.*.id' on those cases
Or the other way round...
Country::whereHas('projects', function ($query) {
// $query->where(); if you want to limit the projects
})->count();
You get unique countries since your fetching from the countries table
I find this solution
$projects->groupBy('countries.*.id')->count();
I've created a collection like this :
Collection {#651 ▼
#items: array:3 [▼
0 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#656 ▶}
"foods" => array:2 [▶]
]
1 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#661 ▶}
"foods" => array:2 [▶]
]
2 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#665 ▶}
"foods" => array:2 [▶]
]
]
}
I demand to achieve collection like this (with Laravel collection):
Collection {#651 ▼
#items: array:3 [▼
0 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#656 ▶}
"foods" => array:6 [▶]
]
]
}
Because orderId and orderCreated are all the same in arrays. I need to make single array that collect orderId and orderCreated with all foods.
Any suggestion?
You can take the data from the first one and just combine all of the food items. For instance like this:
$new = collect([
'orderId' => $old->first()->orderId,
'orderCreated' => $old->first()->orderCreated,
'foods' => $old->pluck('foods')->flatten(1),
]);
The exact implementation will depend on how you built your initial collection.
So I'm trying to make a function that searches a string in a whole array. But it give me anything... So my array looks like this :
array:1 [▼
"list" => array:2 [▼
"pagination" => array:5 [▶]
"entries" => array:11 [▼
0 => array:1 [▼
"entry" => array:8 [▼
"firstName" => "Doctor"
"lastName" => "Who"
"emailNotificationsEnabled" => true
"telephone" => "0123456789"
"company" => []
"id" => "DW"
"enabled" => true
"email" => "doctorwho#time.lord"
]
]
1 => array:1 [▶]
2 => array:1 [▶]
3 => array:1 [▶]
4 => array:1 [▶]
5 => array:1 [▶]
6 => array:1 [▶]
7 => array:1 [▶]
8 => array:1 [▶]
9 => array:1 [▶]
10 => array:1 [▶]
]
]
]
So for exemple at first I did $key = array_search("doctor", $users); but this gives me nothing. So I thought it was because I have a multidimensional array. So I reduced it to just one array (and I would search in the rest of the original array with a for loop), so now I'm working with this array that I got with $users['list']['entries'][0]
array:1 [▼
"entry" => array:8 [▼
"firstName" => "Doctor"
"lastName" => "Who"
"emailNotificationsEnabled" => true
"telephone" => "0123456789"
"company" => []
"id" => "DW"
"enabled" => true
"email" => "doctorwho#time.lord"
]
]
But $key = array_search("doctor", $users['list']['entries'][0]); still doesn't give me anything (but false).
Does anyone know where is my mistake ? Because I couldn't find a solution of my problem yet and it's been a pretty long time I'm on it... I'm still a beginner in php so maybe I've missed something obvious and i'm sorry if I did.
Thank you in advance !
you are searching for doctor,but value stored in your array is as Doctor. So either search for Doctor as
$key=array_search('Doctor', $users['list']['entries'][0]));
or for case in-sensetive search use
$key=array_search(strtolower('Doctor'), array_map('strtolower', $users['list']['entries'][0]));
Note: as your array is multidimensional, so use loop to search your value