I have a array list, How can I get with PHP the highest key value pair in an array?
array:4 [
0 => array:2 [
"free_shipping" => true
"coupon_amt" => 20
]
1 => array:2 [
"coupon_amt" => 120
"free_shipping" => false
]
2 => array:2 [
"free_shipping" => false
"coupon_amt" => 100
]
3 => array:2 [
"coupon_amt" => 200
"free_shipping" => true
]
]
Any ideas or suggestions on how I can or should do this?
array:4 [
0 => array:2 [
"free_shipping" => true
"coupon_amt" => 200
]
]
A simple loop seems the best option to me.
$max = null;
foreach ($array as $part) {
if ($max === null || $part['coupon_amt'] > $max['coupon_amt']) {
$max = $part;
}
}
Could you try this:
First Convert the array to collection
$collection = collect($array);
And then sort it
$sorted = $collection->sortByDesc('coupon_amt');
then if you want it to be array again just do this
$array = $sorted->toArray();
then just get the first array
$first = $array[0];
Hope it helps!
Use the collections' available methods such as max and where.
<?php
$collection = collect($array);
print_r($collection->where('coupon_amt',$collection->max('coupon_amt'))->all());
$collection = collect($array);
print_r($collection->where('coupon_amt',$collection->max('coupon_amt'))->all());
Related
This question already has answers here:
Merge the rows of two arrays (appending row data from one array to a row in another array)
(4 answers)
Closed 2 months ago.
I have two arrays:
$array1 =
[
0 =>
[
'data1' => value1
],
1 =>
[
'data1' => value2
]
];
$array2 =
[
0 =>
[
'data2' => value1
],
1 =>
[
'data2' => value2
]
];
Only i want create this:
$arrayFinish =
[
0 =>
[
'data1' => value1,
'data2' => value1
],
1 =>
[
'data1' => value2
'data2' => value2
]
];
I have done this:
foreach ($data1 as $key=>$val)
{
$arrayFinish[] =
[
$val,
];
foreach ($data2[$key] as $key2=>$val2)
{
array_push($arrayFinish[$key],['data2'=>$val2]);
}
}
My actually result:
array:2 [▼
0 => array:2 [▼
0 => {#1546 ▼
+"data1": "BWQHCLJCH"
}
1 => {#1547 ▼
+"data2": "00308F000825"
}
]
1 => array:2 [▼
0 => {#1548 ▼
+"data1": "OTGSAVJIU"
}
1 => {#1549 ▼
+"data2": "00308F000946"
}
]
]
I am trying to do it using PHP. I am blocked right now, i am sure that that the solution is using two loops, but i am doing any bad. If you can see my actually result is not similar to my wish result.
thank u for the help.
Thank u to all that tried to help me. I find a solution.
I am tried to clear the array
foreach ($array1 as $key=>$val)
{
$arrayFinish[$key] = $val;
foreach ($array2[$key] as $key2=>$val2)
{
array_push($arrayFinish[$key],["data2"=>$val2]);
}
}
Using a for cycle i can have access using one iteration to the information. This is not the great solution that i tried to find but working.
Best regards
if your both arrays would have same no of indexes then you can do using below code
foreach ($array1 as $key=>$val) {
$arrayFinish [] = [ $array1[$key], $array2[$key] ];
}
I have the array array1
array:5 [
0 => array:1 [
2 => array:2 [
"type" => 1
"score" => 10
]
]
1 => array:1 [
2 => array:2 [
"type" => 2
"score" => 5
]
]
2 => array:1 [
2 => array:2 [
"type" => 3
"score" => 4
]
]
3 => array:1 [
2 => array:2 [
"type" => 4
"score" => 60
]
]
4 => array:1 [
1 => array:2 [
"type" => 1
"score" => 5
]
]
]
I have the second array2 which is one dimensional
array:2 [ 0 => 2, 1 => 1]
I want to get the sum of all the score values in array1 where the array1.key = array2.val which will result to:
array:5 [
0 => array:1 [
2 => array:2 [
"score" => 79
]
1 => array:2 [
1 => array:2 [
"score" => 5
]
]
]
This is what i've tried
// getResult (result,remarks,psychomotor) based on periodid
$result = Result::join('subject__class_groups', 'results.subjectid', 'subject__class_groups.id')
->join('term__session__years', 'results.academic_periodid', 'term__session__years.id')
->join('result_types', 'results.restult_typeid', 'result_types.id')
->join('terms', 'term__session__years.termid', 'terms.id')
->join('academic_sessions', 'term__session__years.sessionid', 'academic_sessions.id')
->join('academic_years', 'term__session__years.yearid', 'academic_years.id')
->join('subjects', 'subject__class_groups.subjectid', 'subjects.id')
->join('class_groups', 'subject__class_groups.classgroupid', 'class_groups.id')
->where('results.studentid', $studentid)
->where('results.academic_periodid', $periodid)->get();
$allSubjects = [2,1];
$subs = [];
foreach($result as $res){
// solve for everysubjectid
$subject = array_push($allSubjects, $res->subjectid);
$returnValue = array_unique($allSubjects);
$getScores = array_push($subs, [$res->subjectid=>[
'type'=>$res->restult_typeid,
'score'=>$res->score_obtained,
]]);
}
$check = array_intersect_key($returnValue, $subs);
dd($check);
I'm stuck here cos i don't know what exactly to do
so i'll appreciate a bit of help guys... compliments of the seasons
thank you in anticipation
try this:
foreach( $array1 as $array1x ){
$key=key($array1x);
if( in_array($key, $array2) ){
$this_score = $array1x[$key]['score'];
$cum_score = isset($newarray[$key]['score']) ? $newarray[$key]['score'] : 0;
$newarray[$key]['score'] = $cum_score + $this_score;
}
}
print_r($newarray);
DEMO: https://3v4l.org/iKU8j
I'm pulling my hair on a proble which seems very simple, but I can't find a solution.
I have a simple $row array here :
array:2 [▼
"reference" => "ABCDEF"
"quantity" => "10"
]
I'm trying to parse it and retrieve quantities per reference using :
$line = [
'ref' => strtoupper($row["reference"]),
'quantity' => $row["quantity"]
];
I'm looping through array of lines using this code:
foreach ($rows as $row) {
$line = [
'ref' => strtoupper($row['reference']),
'quantity' => $row['quantity']
];
}
As a test, my main array $rows has 2 lines :
^ array:3 [▼
0 => array:2 [▼
0 => "ABCDEF"
1 => "10"
]
1 => array:2 [▼
0 => "WXCVBN"
1 => "3"
]
2 => array:1 [▼
0 => null
]
]
However, I'm getting the following error :
Undefined index: reference
Strangely enough, if I comment out the
'ref' => strtoupper($row["reference"]),
line, I can get the "quantity" value with no issue...
I know the key is in there, since the debug of the $row object gives the above result.
It must be dead simple... but I can't find the solution...
If anyone could please help ?
So apparently the $row variable a row from a bigger array that is used in a foreach loop.
This might be the solution to your problem.
$array = [
[ "reference" => "ABC", "quantity" => "10"],
[ "reference" => "ABC", "quantity" => "10"],
[ "reference" => "ABC", "quantity" => "10"],
];
$line[] = '';
foreach($array as $row)
{
$line['ref'] = $row['reference'];
$line['quantity'] = $row['quantity'];
}
In this example $array is your bigger array, i use it to test the example.
After that i create a empty array $line to append the "new" data.
Could you try this?
Edit:
After taking a look at your loop and the array i noticed that your array doesnt have a reference key. Can you try strtoupper(row[0])?
Viewing your code seems that you convert $row array to $line without rewrite key in new array.
your code
foreach ($rows as $row) {
$line = [
'ref' => strtoupper($row['reference']),
'quantity' => $row['quantity']
];
}
With your rewrite you can access $line data by index, not by key
array:3 [▼
0 => array:2 [▼
0 => "ABCDEF"
1 => "10"
]
...
]
My solution
If you want to access your $line data by key, you need to rewrite your loop as:
foreach($rows as $rowData) {
foreach($rowData as $rowKey => $rowValue) {
$data = [
'ref' => $rowKey => strtoupper$($rowValue),
'quantity' => $row['quantity']
];
}
$line[] = $data;
}
Basically, I am building html form inputs that have dynamic inputs For example In below example if user want to increase the unit_type then user can add the new input from front end and send it to store
Note: If user will add the unit_type then all others keys will automatically added. Like if user try to increase the unit_type then unit_address and all other inputs increase accordingly.
Right Now I have the array like this
array:7 [
"unit_type" => array:2 [
0 => null
1 => null
]
"unit_address" => array:2 [
0 => null
1 => null
]
"unit_phone" => array:2 [
0 => null
1 => null
]
"fax" => array:2 [
0 => null
1 => null
]
"installed_capacity" => array:2 [
0 => null
1 => null
]
"production_capacity" => array:2 [
0 => null
1 => null
]
"unit_email" => array:2 [
0 => null
1 => null
]
]
Expected Result
[
[
//Here all keys contain the first values of all arrays
'unit_type'=>'first_value',
'unit_address'=>'first_value',
'unit_phone'=>'first_value',
'fax'=>'first_value',
'installed_capacity'=>'first_value',
'production_capacity'=>'first_value',
'unit_email'=>'first_value'
],
[
//Here all keys contain the second values of all arrays
'unit_type'=>'second_value',
'unit_address'=>'second_value',
'unit_phone'=>'second_value',
'fax'=>'second_value',
'installed_capacity'=>'second_value',
'production_capacity'=>'second_value',
'unit_email'=>'second_value'
]
]
Loop through the existing return data and build the new array like so:
$input_array = []; //your data received from the front end
$return_array = []; //structured data return
foreach($input_array as $field => $fieldData){
foreach ($fieldData as $key => $data){
$return_array[$key][$field] = $data;
}
}
I am trying to add 2 associative arrays, but the new array only comes with an index for the second array.
This is what I am doing
$array = ['name' => 'address', 'value' => 'us'];
$arr = ['name' => 'joe', 'value' => 'doe'];
$arr[] = $array;
This is the result
array:3 [▼
"name" => "joe"
"value" => "doe"
0 => array:2 [▶]
]
I am expecting something like this
array:2 [▼
0 => array:2 [▶]
1 => array:2 [▶]
]
As you can see, there is no index for the first array, thus making the count 3 instead of 2. Please how do I fix this?
Just create another array and add the 2 arrays to it:
$newAr = [];
$newAr[] = $array;
$newAr[] = $arr;
var_dump($newAr);