laravel 8 multiple array value by the index - arrays

i have 2 array, and all i want is multiple array value by index, example 0=> 1 * 0=>100000 , 1=> 2 * 1=>90000, etc. Is that possible ?
array 1 :
array:2 [▼
0 => 1
1 => 2
next value ...
]
array 2 :
array:2 [▼
0 => 100000
1 => 90000
next value ...
]
very grateful if someone helps.

$arr1 = [0 => 1, 1 => 2];
$arr2 = [0 => 100000, 1 => 90000];
foreach ($arr1 as $k => $val) {
$total[$k]=$val * $arr2[$k];
}
$total will return list of multiplied value, I assumed $arr2 and $arr1 has same key count.

Related

filtering change content values in all element array

i have value array like this
^ array:2 [▼
0 => "random value1<tr>random string1</tr>random endvalue1"
1 => "random value2<tr>random string2</tr>random endvalue2"
)
]
how do i change the value of the dimensional array above to be like below
^ array:2 [▼
0 => "random string1"
1 => "random string2"
)
]
im trying using
$array = array_map(function($key) {
$exp = explode('<tr>', $tes);
//$exp1 = explode('</tr>', exp[1]); -from this line3 my explode cant access array
return exp;
}, $myarray);
dd($array);
my array in line3 become like this
^ array:2 [▼
0 => array:2 [▼
0 => "random value1"
1 => "random string1</tr>random endvalue1"
]
1 => array:2 [▼
0 => "random value2"
1 => "random string2</tr>random endvalue2"
]
)
]
how to improve conditions like this, or have another way that is better ?
$data = [
"random value1<tr>random string1</tr>random endvalue1",
"random value2<tr>random string2</tr>random endvalue2",
];
$trim = collect($data)
->map(function (string $value) {
preg_match('/(?<=<tr>)(.*)(?=<\/tr>)/', $value, $match);
return empty($match) ? $value : $match[1];
})
->toArray();
Could be done this way, using the framework collect method to map through the values & extract the string you want.

How to get unique value in multidimensional array in Laravel?

I have the following multidimensional array.
"PM" => array:6 [▼
0 => "Zeb"
1 => "Pen"
2 => "Zeb"
3 => "Eds"
4 => "Fsa"
5 => "Zeb"
]
"OS" => array:3 [▼
0 => "Min"
1 => "Kep"
2 => "Min"
]
"IT" => array:8 [▶]
]
And I would like to remove the duplicate values from the nested arrays. In this case, have only one value Zeb in PM and Min in OS. Please keep in mind that, I don't know in which array there are duplicates so I need a way to check all the nested arrays for duplicates.
Thank you.
$arr = [
"PM" => ["Zeb", "Pen", "Zeb", "Eds", "Fsa", "Zeb"],
"OS" => ["Min", "Kep", "Min"],
];
$cleanArray = collect($arr)->map(fn($subarr) => array_unique($subarr))->toArray();
will produce:
[
"PM" => ["Zeb", "Pen", "Eds", "Fsa"],
"OS" => ["Min", "Kep"]
]

Cakephp collection how can I get array index?

This is my collection data
[
0 => object(App\Model\Entity\SummaryCoin) id:0 { 'user_id' => (int) 2
'total_sum_coin' => (float) 3 },
1 => object(App\Model\Entity\SummaryCoin) id:1 { 'user_id' => (int) 1
'total_sum_coin' => (float) 33 },
]
How can I get the index where user_id = 1
Using first match I am able to get user 1 new collection
$sumOfUserCoins = $collection->firstMatch([
'user_id' => 1
]);
How I will get the array index 1 that user have ?
There is no specific method for that, you'll have to be a little creative to obtain that information, for example match all instead so that you get a collection back, take everything away expect for the first entry, and convert it to an array that keeps the keys, from which you can then get that information:
$userCoins = $collection
->match(['user_id' => 1])
->take(1)
->toArray();
$index = key($userCoins);
$coin = current($userCoins);

Match an array value with another multidimensional array and then get the values from the multidimensional array

I want to Match Array 1 values with the value of array 2 key DATE and if it found matched value then print value of MIN_TIME
Array 1:
array:13 [
0 => 2020-02-13
1 => 2020-02-12
2 => 2020-02-11
3 => 2020-02-10
4 => 2020-02-09
5 => 2020-02-08
6 => 2020-02-07
7 => 2020-02-06
8 => 2020-02-05
9 => 2020-02-04
10 => 2020-02-03
11 => 2020-02-02
12 => 2020-02-01
]
Array 2:
array:8 [▼
0 => {#1192 ▼
+"USERID": "59"
+"DATE": "2020-02-13"
+"MAX_TIME": "17:11:14.0000000"
+"MIN_TIME": "10:01:55.0000000"
}
1 => {#1194 ▼
+"USERID": "59"
+"DATE": "2020-02-12"
+"MAX_TIME": "18:12:20.0000000"
+"MIN_TIME": "14:08:25.0000000"
}
2 => {#1195 ▼
+"USERID": "59"
+"DATE": "2020-02-11"
+"MAX_TIME": "18:22:09.0000000"
+"MIN_TIME": "15:40:56.0000000"
}
3 => {#1196 ▼
+"USERID": "59"
+"DATE": "2020-02-10"
+"MAX_TIME": "16:47:15.0000000"
+"MIN_TIME": "14:07:19.0000000"
}
4 => {#1197 ▼
+"USERID": "59"
+"DATE": "2020-02-07"
+"MAX_TIME": "16:29:40.0000000"
+"MIN_TIME": "08:39:38.0000000"
}
5 => {#1198 ▶}
6 => {#1199 ▶}
7 => {#1200 ▶}
]
My Expected output is:
Date Date From DB Time
2/13/2020 2/13/2020 10:01:55 AM
2/12/2020 2/12/2020 2:08:25 PM
2/11/2020 2/11/2020 3:40:56 PM
2/10/2020 2/10/2020 2:07:19 PM
2/9/2020 Not Found
2/8/2020 Not Found
2/7/2020 2/7/2020 8:39:38 AM
I searched from google and tried all solution but failed, Can you guys please help me.
You can use Laravel Collections to achieve this:
$result = collect($array1)
->mapWithKeys(function ($item) {
return [$item => null];
})
->merge(
collect($array2)->pluck('MIN_TIME', 'DATE')
)
->toArray();
/* Output:
[
2020-02-13 => 10:01:55.0000000
2020-02-12 => 14:08:25.0000000
2020-02-11 => 15:40:56.0000000
2020-02-10 => 14:07:19.0000000
2020-02-09 => null
2020-02-08 => null
2020-02-07 => 08:39:38.0000000
]
*/
You can use map or transform to get your expected result:
$result = collect($array1)
->mapWithKeys(function ($item) {
return [$item => null];
})
->merge(
collect($array2)->pluck('MIN_TIME', 'DATE')
)
->map(function ($item, $key) {
return [
'Date' => $key,
'Time' => $item
];
})
->toArray();
In below code we made for loop of first array element which you show in question.
And then match with your collection with first where.
And print your table
Documentation :
https://laravel.com/docs/5.7/collections#method-first-where
$array=[
0 => 2020-02-13
1 => 2020-02-12
2 => 2020-02-11
3 => 2020-02-10
4 => 2020-02-09
5 => 2020-02-08
6 => 2020-02-07
7 => 2020-02-06
8 => 2020-02-05
9 => 2020-02-04
10 => 2020-02-03
11 => 2020-02-02
12 => 2020-02-01
];
//$dbcollection is the laravel collection which you getting from the db.
// as per your second array printed we assume it will be same.
// assign that array in $dbcollection variable.
echo "<table>";
foreach($array as $value){
echo "<tr>";
echo "<td>{$value}</td>";
if ($dbrow=$dbcollection->firstWhere('DATE',$value)) {
echo "<td>{$dbrow->DATE}</td><td>{$dbrow->MIN_TIME}</td>";
} else {
echo "<td colspan='2'>Not Found</td>";
}
echo "</tr>";
}
echo "</table>";
Hope this will be work for you.

Issue while looping an array in laravel view

Hi i have 2 arrays returned from a controller.
$order
array (size=2)
0 =>
array (size=2)
'orderid' => int 1
'ostatus' => string 'Placed' (length=6)
1 =>
array (size=2)
'orderid' => int 4
'ostatus' => string 'Placed' (length=6)
$orderdetails
array (size=2)
1 =>
array (size=3)
0 =>
array (size=3)
'oid' => int 1
'img6_path' => string 'images/product-list/pro1-list.jpg' (length=33)
'brand_name' => string 'Puma' (length=4)
1 =>
array (size=3)
'oid' => int 1
'img6_path' => string 'images/product-list/pro2-list.jpg' (length=33)
'brand_name' => string 'DressBerry' (length=10)
2 =>
array (size=3)
'oid' => int 1
'img6_path' => string 'images/product-list/pro3-list.jpg' (length=33)
'brand_name' => string 'United Colors of Benetton' (length=25)
4 =>
array (size=1)
0 =>
array (size=2)
'oid' => int 4
'img6_path' => string 'images/product-list/pro4-list.jpg' (length=33)
'brand_name' => string 'Puma' (length=4)
I am trying to print it in this order:
This is what i have been doing:
foreach($order as $ord)
//printing order id
foreach($orderdetail as $od)
foreach($od as $x)
//printing the products
endforeach()
endforeach()
endforeach()
But this is printing
orderid 1 with 3 products
orderid 1 with 1 product (That belonged to orderid 4)
orderid 4 with 3 products (That belonged to orderid 1)
orderid 4 with 1 product
Can anyone tell me how should i loop through the array so that after printing the first 3 products in order1 the loop breaks out and then print the 1 product in order 4 (Like in the picture above). Thanks
#foreach($order as $ord)
//printing order id
#foreach($orderdetail[$ord->orderid] as $od)
#foreach($od as $x)
//printing the products
#endforeach
#endforeach
#endforeach
where #foreach($orderdetail[$ord->orderid] as $od) is the big change... I would give this a try...

Resources