so i have a bit difficulty in combining arrays in php. So let say i have these 2 array
array:4 [▼
20 => "University"
21 => "Polic Station"
22 => "Ambulance"
1 => "Zoo"
]
array:4 [▼
20 => "abc"
21 => "def"
22 => "ghi"
1 => "jkl"
]
How do i actually combine this to this
array:4 [▼
20 => "abc University"
21 => "def Polic Station"
22 => "ghi Ambulance"
1 => "jkl Zoo"
]
Here's the result:
$arr = array(
20=>'University',
21=>'Polic Station',
22=>'Ambulance',
1=>'Zoo');
$arr2= array(
20=>'abc',
21=>'def',
22=>'ghi',
1=>'jkl');
$arr_out = array();
foreach($arr as $key=>$el) {
$arr_out[$key] = $arr2[$key] ." ".$el;
}
var_dump($arr_out);
Obviously, you'll need to keep in mind to check whether the key exists in the second array so you do not get an error when accessing the value.
Related
i am attempting to make an array:
foreach ($cats as $cat) {
$catsList[$cat->id] = [$cat->info => $cat->info];
}
What i get is a resulting array that only contains the last $cat info.
array:2 [▼
10 => array:1 [▼
23 => 23
]
9 => array:1 [▼
11 => 11
]
]
What i expect to get is:
array:2 [▼
10 => array:1 [▼
23 => 23
15 => 15
12 => 12
]
9 => array:1 [▼
11 => 11
24 => 24
]
]
I guess the syntax is wrong when i'm trying to add a new member and it just overwrites the old one?
Can you try this:
foreach ($cats as $cat) {
$catsList[$cat->id][$cat->info] = $cat->info;
}
I have a 3 level category. I need to gain businesses on level 3 category.
I write this code:
$main_cat = Category::where(['slug' => $url])->first();
$lev_cat2 = Category::where(['parent_id' => $main_cat->id, 'status' => '1'])->get();
foreach ($lev_cat2 as $subCategory) {
$cat_ids_lv2[] = $subCategory->id . ',';
}
foreach ($lev_cat2 as $subCat) {
$lev_cat3[] = Category::where(['parent_id' => $subCat->id, 'status' => '1'])->pluck('id')->toArray();
}
dd($lev_cat3);
Then I got this array which is correct:
array:5 [▼
0 => array:3 [▼
0 => 145
1 => 146
2 => 147
]
1 => array:3 [▼
0 => 148
1 => 149
2 => 150
]
2 => array:3 [▼
0 => 151
1 => 152
2 => 153
]
3 => array:3 [▼
0 => 154
1 => 155
2 => 156
]
4 => []
]
now I dont know how can I get values like 145,146,147,148,149,... to pass theme to
Business::where(['category_id'=> [145,146,147,148,...]]->get();
of course dynamic.
You can use laravel collection helpers :
Business::whereIn('category_id', collect($lev_cat3)->flatten()->all())->get();
Since PHP 5.5.0 there is a built-in function array_column which does exactly this.
You can use it to Converting php array of arrays into single array then use it like this
$category_ids = array_column($lev_cat3);
Business::where(['category_id'=> $category_ids]]->get();
My current array that is being looped dumps this structure
0 => array:11 [▼
"category_code" => "123"
"category_name" => "Testing"
"category_description" => "This is a test category"
19738 => array:5 [▼
"identifier" => "720368842943"
"description" => Test Description One
"count" => 4
"details" => array:2 [▼
0 => array:3 [▼
"detail_code" => "2751"
"detail_code2" => "43"
"detail_specifier" => "Detail One"
]
1 => array:3 [▼
"detail_code" => "2681"
"detail_code2" => "9"
"detail_specifier" => "Detail Two"
]
]
"prices" => array:1 [▼
"01" => "1129.00"
]
]
19739 => array:5 [▼
"identifier" => "720368844121"
"description" => "Test Description Two"
"count" => 4
"details" => array:2 [▼
0 => array:3 [▼
"detail_code" => "2751"
"detail_code2" => "43"
"detail_specifier" => "Detail One"
]
1 => array:3 [▼
"detail_code" => "2681"
"detail_code2" => "9"
"detail_specifier" => "Detail Two"
]
]
"prices" => array:1 [▼
"01" => "1490.00"
]
]
But when I export to excel it only shows the three top level attributes
123 | Testing | This is a test category
I'm trying to export this in a way so that those top 3 values are one row (like a header) and all related products are listed under it like so:
123 | Testing | This is a test category
====================================================================================================================
19738 | 720368842943 | Test Description One | 4 | 2751 | 43 | Detail One | 2681 | 9 | Detail Two | 1129.00
19739 | 720368844121 | Test Description Two | 4 | 2751 | 43 | Detail One | 2681 | 9 | Detail Two | 1490.00
I'm using Laravel Excel by maatwebsite which is just a wrapper for PHPExcel in laravel, but all i want to do is simply take the category info as a row with the subsequent product info as rows below it.
Here's the excel code with the array I'm using, which is dumped above (item Code is the 19738,19739 values)
$allCategoryResult= array();
foreach($prices->categories as $category){
$categoryItem = array();
$categoryItem["category_code"] = $category->category_code;
$categoryItem["category_name"] = $category->category_name;
$categoryItem["category_desc"] = $category->category_desc;
foreach($category->skus as $sku){
$skuItem = array();
$skuItem["identifier"] = $sku->sku_info->identifier;
$skuItem["description"] = $sku->sku_info->item->description;
$skuItem["count"] = $sku->sku_info->item->item_type->count;
$skuItem["details"] = array();
foreach ($sku->sku_info->details as $details) {
$detailsItem = array();
$detailsItem["detail_code"] = $details->detail_code;
$detailsItem["detail_code2"] = $details->detail_code2;
$detailsItem["detail_specifier"] = $details->detail_specifier;
$skuItem["details"][] = $detailsItem;
}
$skuItem["prices"] = get_object_vars($sku->prices);
$itemCode = $sku->sku_info->item->item_code;
$categoryItem[$itemCode] = $skuItem;
}
$allCategoryResult[] = $categoryItem;
}
$name = 'Test Export';
$build = Excel::create($name, function ($excel) use ($allCategoryResult) {
$excel->setTitle('Test Export');
$excel->sheet('Test Export', function ($sheet) use ($allCategoryResult) {
$sheet->fromArray($allCategoryResult);
})->download('xlsx');
The methodfromArray() expects a 2D array
$data=(
array(2) (
[0] => array(3) (
[0] => 19738
[1] => ...
[2] => ...
)
[1] => array(4) (
[0] => 19739
[1] => ...
[2] => ...
[3] => ...
Each element of the array $data is a row. Each sub element is the value of a column. Restructure the creation of your array to fit this structure and you will be in business.
This code is untested, just trying to give an example. I'm not sure what you're doing with the get_object_vars($sku->prices);. I'm sure this will have to change.
$excelRows = [];
foreach($prices->categories as $category){
$excelRows[] = [
$category->category_code,
$category->category_name,
$category->category_desc
]
foreach($category->skus as $sku){
$row = [
$sku->sku_info->identifier,
$sku->sku_info->item->description,
$sku->sku_info->item->item_type->count
]
foreach ($sku->sku_info->details as $details) {
$row[] = $details->detail_code;
$row[] = $details->detail_code2;
$row[] = $details->detail_specifier;
}
$row[] = get_object_vars($sku->prices);
$row[] = $sku->sku_info->item->item_code;
$excelRows[] = $row;
}
}
I want to remove same array between 2 array
1 ) $getAlldate
array:8 [▼
0 => "2018-08-17"
1 => "2018-08-20"
2 => "2018-08-21"
3 => "2018-08-22"
4 => "2018-08-23"
5 => "2018-08-24"
6 => "2018-08-27"
7 => "2018-08-28"
]
2) $getHoliday
array:7 [▼
0 => "2019-1-1"
1 => "2019-3-6"
2 => "2019-2-28"
3 => "2019-5-2"
4 => "2019-4-25"
5 => "2018-8-27"
6 => "2018-8-28"
]
As you see in my array 2018-08-27,2018-08-28 are duplicate with $getholiday I try to use array_diff and It still didnt work
$a = array_diff($getAllDate, $getHoliday);
$b = array_unique($a);
How can I output like this $getalldate remove same value $getHoliday
array:8 [▼
0 => "2018-08-17"
1 => "2018-08-20"
2 => "2018-08-21"
3 => "2018-08-22"
4 => "2018-08-23"
5 => "2018-08-24"
]
The format of both the dates are diffrent.
Try:
PHP Code:
$getAlldate = [
"2018-08-17",
"2018-08-20",
"2018-08-21",
"2018-08-22",
"2018-08-23",
"2018-08-24",
"2018-08-27",
"2018-08-28"
];
$getHoliday=[
"2019-1-1",
"2019-3-6",
"2019-2-28",
"2019-5-2",
"2019-4-25",
"2018-8-27",
"2018-8-28"
];
$formattedHoliday = array_map(function ($holiday) {
$alldateFormat = 'Y-m-d';
$HolidayFormat = 'Y-n-d';
$date = DateTime::createFromFormat($HolidayFormat, $holiday);
return $date->format($alldateFormat);
}, $getHoliday);
var_dump(array_diff($getAlldate, $formattedHoliday));
Output: https://3v4l.org/rCLCk
Refer:
DateTime::createFromFormat: http://php.net/manual/en/datetime.createfromformat.php
DateTime::format: http://php.net/manual/en/datetime.format.php
Here is the array. What I want is I wanna sort the array on the basis on 'income_difference' in ascending order. If possible, I just want 3 data with least income difference.
array:5 [▼
0 => array:4 [▼
"month" => "11"
"income_times" => 2
"income_amount" => 52300
"income_difference" => 49000
]
1 => array:4 [▼
"month" => "10"
"income_times" => 1
"income_amount" => 50000
"income_difference" => 46700
]
2 => array:4 [▼
"month" => "09"
"income_times" => 1
"income_amount" => 5000000
"income_difference" => 4996700
]
3 => array:4 [▼
"month" => "08"
"income_times" => 1
"income_amount" => 50000
"income_difference" => 46700
]
4 => array:4 [▼
"month" => "06"
"income_times" => 1
"income_amount" => 5200
"income_difference" => 1900
]
]
Use usort:
usort($data, function($a, $b) {
return $a['income_difference'] - $b['income_difference'];
});
If you don't want to maintain the index(0,1,2,3) you can use usort
e.g
function cmp($a, $b)
{
if ($a['income_difference'] == $b['income_difference']) {
return 0;
}
return ($a['income_difference'] < $b['income_difference']) ? -1 : 1;
}
usort($yourArray,'cmp')
If you want to maintain the index (0,1,2,3) you can use uasort instead of usort. Other syntax remains the same.
For more details on functionality please refer usort