Laravel compare database and api - php

I have api base data like:
array:21 [▼
0 => "jne"
1 => "pos"
2 => "tiki"
3 => "esl"
4 => "pcp"
5 => "rpx"
6 => "cahaya"
7 => "dse"
8 => "first"
9 => "indah"
10 => "jet"
11 => "jnt"
12 => "ncs"
13 => "pahala"
14 => "pandu"
15 => "sap"
16 => "sicepat"
17 => "slis"
18 => "star"
19 => "nss"
20 => "wahana"
]
which i get with this code:
$cori = $rajaongkir->courier('all');
$cori = array_keys($cori);
I also have database table named couriers where i saved same names as you see in my dd above + status and dd of my table is:
Collection {#825 ▼
#items: array:19 [▼
0 => "jne"
1 => "pos"
2 => "tiki"
3 => "esl"
4 => "rpx"
5 => "cahaya"
6 => "dse"
7 => "first"
8 => "indah"
9 => "jet"
10 => "jnt"
11 => "ncs"
12 => "pahala"
13 => "pandu"
14 => "sap"
15 => "sicepat"
16 => "slis"
17 => "star"
18 => "nss"
]
}
this is how i get that:
$selectedcouriers = Courier::where('active', '=', '1')->pluck('courier');
PS: as you see my table dd has 2 names less than my api dd that's
because those 2 names active status is 0 (deactivated)
Problem
what I try to do here is compare between my API courier names and my Database courier names (which are the same by the way) and then return only those couriers with status of 1 in my database (ignore the rest of courier names in my api)
how can i do this compare?
My full code is:
$cori = $rajaongkir->courier('all'); // get all couriers from api
$cori = array_keys($cori); // retrieve only their names
$selectedcouriers = Courier::where('active', '=', '1')->pluck('courier'); // get my DB couriers with active status
//comparing code...?

Simple, use array_intersect()
$result = array_intersect($selectedcourier, $cori);

Related

I want to remove the common elements in two arrays using PHP [duplicate]

This question already has answers here:
How to remove an array value from another array using PHP question
(3 answers)
Closed 2 years ago.
I tried the following code and it should be working, but not getting the required result. What's wrong with the code? I have two arrays and I want to remove the common elements in both arrays so I wore the following code.
<?php
$aMgaMembersList= array (
0 => '9194962',
1 => '9197448',
2 => '9174039',
3 => '9199473',
4 => '9175598',
5 => '9197474',
6 => '9195444',
7 => '9195268',
8 => '9189438',
9 => '9175103',
10 => '9199619',
11 => '9195267',
12 => '9194463',
13 => '9196333',
14 => '9197471',
15 => '9198479',
16 => '9197472',
17 => '9185479',
18 => '9197452',
19 => '9197442',
20 => '9180861',
21 => '9194950',
22 => '9198464',
23 => '9199613',
24 => '9175939',
25 => '9195442',
26 => '9190203',
27 => '9199613',
) ;
$aRocketMembersList = array (
0 => '9174039',
1 => '9175103',
2 => '9175598',
3 => '9175939',
4 => '9180861',
5 => '9185479',
6 => '9189438',
7 => '9190203',
8 => '9194463',
9 => '9194950',
10 => '9194962',
11 => '9195267',
12 => '9195268',
13 => '9195442',
14 => '9195444',
15 => '9196333',
16 => '9197442',
17 => '9197448',
18 => '9197452',
19 => '9197471',
20 => '9197472',
21 => '9197474',
22 => '9198464',
23 => '9198479',
24 => '9199473',
25 => '9199613',
26 => '9199619',
27 => 'arun',
) ;
if (is_array($aRocketMembersList)) {
foreach ($aRocketMembersList as $rocketUsername) {
if (in_array($rocketUsername, $aMgaMembersList)) {
unset($aMgaMembersList[array_search($rocketUsername, $aMgaMembersList)]);
unset($aRocketMembersList[array_search($rocketUsername, $aRocketMembersList)]);
}
}
}
print_r($aRocketMembersList);
print_r($aMgaMembersList);
The out put is
Array
(
[27] => arun
)
Array
(
[27] => 9199613
)
The element 9199613 shouldn't be there. Why it's happening? I ran the code in a different environment and the result is same.
Here's a different function that works regardless of the order of Arrays:
<?php
function different($array1, $array2){
$m = array_merge($array1, $array2); $x = array_intersect($array1, $array2); $a = array_diff($m, $x); $b = array_diff($x, $m); $a = array_merge($a, $b);
$r = [];
foreach($a as $v){
$o = new StdClass; $k = array_search($v, $array1);
if($k === false)$k = array_search($v, $array2);
$o->$k = [$array1[$k], $array2[$k]]; $r[] = $o;
}
return $r;
}
$aMgaMembersList = [
0 => '9194962',
1 => '9197448',
2 => '9174039',
3 => '9199473',
4 => '9175598',
5 => '9197474',
6 => '9195444',
7 => '9195268',
8 => '9189438',
9 => '9175103',
10 => '9199619',
11 => '9195267',
12 => '9194463',
13 => '9196333',
14 => '9197471',
15 => '9198479',
16 => '9197472',
17 => '9185479',
18 => '9197452',
19 => '9197442',
20 => '9180861',
21 => '9194950',
22 => '9198464',
23 => '9199613',
24 => '9175939',
25 => '9195442',
26 => '9190203',
27 => '9199613'
];
$aRocketMembersList = [
0 => '9174039',
1 => '9175103',
2 => '9175598',
3 => '9175939',
4 => '9180861',
5 => '9185479',
6 => '9189438',
7 => '9190203',
8 => '9194463',
9 => '9194950',
10 => '9194962',
11 => '9195267',
12 => '9195268',
13 => '9195442',
14 => '9195444',
15 => '9196333',
16 => '9197442',
17 => '9197448',
18 => '9197452',
19 => '9197471',
20 => '9197472',
21 => '9197474',
22 => '9198464',
23 => '9198479',
24 => '9199473',
25 => '9199613',
26 => '9199619',
27 => 'arun'
];
$diffArray = different($aMgaMembersList, $aRocketMembersList);
$test = json_encode($diffArray);
?>
$tmp1 = array_diff($aMgaMembersList,$aRocketMembersList);
$tmp2 = array_diff($aRocketMembersList,$aMgaMembersList);
$final = array_unqiue(array_merge($tmp1, $tmp2));
unset($tmp1);unset($tmp2);//and maybe original arrays?
There are probably better solutions, but that should work for you. If you had associative arrays instead of numeric values you could exclude array_unqiue
EDIT:
I originally assumed you wanted the results in one array, if that's unnecessary just use the array_diff function twice, and you can maintain your original array names as desired. Again there are probably better solutions (more memory/processor efficient), but in most practical cases this will be fine. If you're working with extremely large data sets... do more research ;)

PHP Calculate Unique combinations which total a specific value for multidimensional array

In an eCommerce store we have multiple transactions that relate to a specific order, e.g an order was made for 2 SKU's (1 transaction), 2 skus were added (another transaction), 1 was refunded (another transaction). The order itself is made up of multiple line items, each with a unique id.
Each of these transactions will have a numerical value, where together they will add up to the total of that order. What I'm trying to achieve is to take the line items and match their values to the transactions where there is no overlap (e.g a line item can only be part of one transaction).
A real world example would be as follows, there are 3 transactions -
Transaction 1 (6-671461867600) For 39.68
Transaction 2 (6-671717458000) For 31.98
Transaction 3 (6-671826772048) For -7.7
(these total 63.96)
And the line items which make up these transactions are as follows.
$lineItems = [
0 => [
"xId" => 96909066320
"price" => "-7.70"
"sku" => 'KB-291'
]
1 => [
"xId" => 4978455609424
"price" => "15.99"
"sku" => "261-R4-BE3B-CDWM"
]
2 => [
"xId" => 4978455642192
"price" => "15.99"
"sku" => "261-LV-5PT8-V6KA"
]
3 => [
"xId" => 4979504119888
"price" => "15.99"
"sku" => "261-UQ-3TEP-GJ8U"
]
4 => [
"xId" => 4979504152656
"price" => "15.99"
"sku" => "261-SO-58DE-0QKS"
]
5 => [
"xId" => 1887799246928
"price" => "7.70"
"sku" => 'KB-297'
]
];
What I'm trying to achieve is to specify which line items will make up each transaction, doing it manually I can find the follow work with no overlaps.
Transaction 1) 1887799246928, 4979504152656, 4979504119888
Transaction 2) 4978455642192, 4978455609424
Transaction 3) 96909066320
$array = [
"6-671826772048" => [
0 => "96909066320"
]
"6-671461867600" => [
0 => "1887799246928,4978455609424,4978455642192"
1 => "1887799246928,4978455609424,4979504119888"
2 => "1887799246928,4978455609424,4979504152656"
3 => "1887799246928,4978455642192,4979504119888"
4 => "1887799246928,4978455642192,4979504152656"
5 => "1887799246928,4979504119888,4979504152656"
]
"6-671717458000" => [
0 => "96909066320,1887799246928,4978455609424,4978455642192"
1 => "96909066320,1887799246928,4978455609424,4979504119888"
2 => "96909066320,1887799246928,4978455609424,4979504152656"
3 => "96909066320,1887799246928,4978455642192,4979504119888"
4 => "96909066320,1887799246928,4978455642192,4979504152656"
5 => "96909066320,1887799246928,4979504119888,4979504152656"
6 => "4978455609424,4978455642192"
7 => "4978455609424,4979504119888"
8 => "4978455609424,4979504152656"
9 => "4978455642192,4979504119888"
10 => "4978455642192,4979504152656"
11 => "4979504119888,4979504152656"
]
]
Where I've got to thus far is for every transaction, find what combination of line items would total the amount of the transaction - however I obviously need to ensure that there is no overlap,
e.g.
Transaction id 6-671826772048 can ONLY be calculated using Line Item 96909066320.
-Transaction id 6-671717458000 has an option of being calculated using 96909066320,1887799246928,4979504119888,4979504152656 - however, that would stop Transaction 1 from working.
To sumamrise, I want to take this array
$array = [
"6-671826772048" => [
0 => "96909066320"
]
"6-671461867600" => [
0 => "1887799246928,4978455609424,4978455642192"
1 => "1887799246928,4978455609424,4979504119888"
2 => "1887799246928,4978455609424,4979504152656"
3 => "1887799246928,4978455642192,4979504119888"
4 => "1887799246928,4978455642192,4979504152656"
5 => "1887799246928,4979504119888,4979504152656"
]
"6-671717458000" => [
0 => "96909066320,1887799246928,4978455609424,4978455642192"
1 => "96909066320,1887799246928,4978455609424,4979504119888"
2 => "96909066320,1887799246928,4978455609424,4979504152656"
3 => "96909066320,1887799246928,4978455642192,4979504119888"
4 => "96909066320,1887799246928,4978455642192,4979504152656"
5 => "96909066320,1887799246928,4979504119888,4979504152656"
6 => "4978455609424,4978455642192"
7 => "4978455609424,4979504119888"
8 => "4978455609424,4979504152656"
9 => "4978455642192,4979504119888"
10 => "4978455642192,4979504152656"
11 => "4979504119888,4979504152656"
]
]
and ONE result could be (although there are other correct possibilities)
$array = [
"6-671826772048" => "96909066320",
"6-671461867600 => "4978455642192, 4978455609424"
"6-671717458000" => "1887799246928, 4979504152656, 4979504119888"
];
If anyone has an idea on how to solve this it would be appreciated
Update - In the example I gave, whilst I presented one combination which solved the problem, there are many, there is no specific link between the transaction and line items, so as long as each transaction line items total the amount, and no transactions share line items, then the problem is considered solved
e.g the following would also be a valid solution
$array = [
"6-671826772048" => "96909066320",
"6-671461867600" => "4979504152656, 4979504119888"
"6-671717458000" => "4978455642192, 4978455609424, 1887799246928"
];

Laravel, How to store multiple open and close time per every day

I have array of days and their open time and close time as below, I need to store these days and the open and close time for a selected store in table, in the same time if the store have already entered in this table then update the current records so no duplicate will be found
I hope you can help me in this
many thanks
"store_id" => "625"
"day_id" => array:7 [
1 => "1"
2 => "2"
3 => "3"
4 => "4"
5 => "5"
6 => "6"
7 => "7"
]
"open_time" => array:7 [
1 => "13:20"
2 => "01:20"
3 => "13:20"
4 => "01:20"
5 => "15:50"
6 => "07:20"
7 => "23:20"
]
"close_time" => array:7 [
1 => "20:20"
2 => "08:20"
3 => "20:20"
4 => "21:20"
5 => "18:20"
6 => "00:20"
7 => "19:20"
]
I tried with this but need more
public function store(Request $request)
{
$store_id=$request->storeinfo_id;
foreach ($request->input('day_id') as $key => $val) {
$array = [
'day_id' => $val,
];
Storeday::where('storeinfo_id',$store_id)->create($array);
}
return response()->json(['data' => trans('message.success')]);
}

Properly getting the sorting order of this array in PHP

I have a 2-dimensional array which the values...
9999999999999999 3201 4584 4821 1628 1218 1786 4738 4897
3122 9999999999999999 1400 1638 1797 2756 3323 5310 5472
4523 1400 9999999999999999 237 3198 4156 4723 6711 6872
4760 1638 237 9999999999999999 3435 4394 4961 6948 7110
1324 1846 3247 3485 9999999999999999 958 1525 3931 4093
932 2854 4273 4510 1002 9999999999999999 567 4873 5034
1499 3422 4840 5078 1569 567 9999999999999999 5440 5602
5061 5359 6760 6998 4019 4959 5526 9999999999999999 161
5233 5531 6931 7169 4190 5130 5697 171 9999999999999999
Here's the same array in code:
array:9 [
0 => array:9 [
0 => 9999999999999999
1 => 3122
2 => 4523
3 => 4760
4 => 1324
5 => 932
6 => 1499
7 => 5061
8 => 5233
]
1 => array:9 [
0 => 3201
1 => 9999999999999999
2 => 1400
3 => 1638
4 => 1846
5 => 2854
6 => 3422
7 => 5359
8 => 5531
]
2 => array:9 [
0 => 4584
1 => 1400
2 => 9999999999999999
3 => 237
4 => 3247
5 => 4273
6 => 4840
7 => 6760
8 => 6931
]
3 => array:9 [
0 => 4821
1 => 1638
2 => 237
3 => 9999999999999999
4 => 3485
5 => 4510
6 => 5078
7 => 6998
8 => 7169
]
4 => array:9 [
0 => 1628
1 => 1797
2 => 3198
3 => 3435
4 => 9999999999999999
5 => 1002
6 => 1569
7 => 4019
8 => 4190
]
5 => array:9 [
0 => 1218
1 => 2756
2 => 4156
3 => 4394
4 => 958
5 => 9999999999999999
6 => 567
7 => 4959
8 => 5130
]
6 => array:9 [
0 => 1786
1 => 3323
2 => 4723
3 => 4961
4 => 1525
5 => 567
6 => 9999999999999999
7 => 5526
8 => 5697
]
7 => array:9 [
0 => 4738
1 => 5310
2 => 6711
3 => 6948
4 => 3931
5 => 4873
6 => 5440
7 => 9999999999999999
8 => 171
]
8 => array:9 [
0 => 4897
1 => 5472
2 => 6872
3 => 7110
4 => 4093
5 => 5034
6 => 5602
7 => 161
8 => 9999999999999999
]
]
What I'm trying to achieve is find the sorting order of this array in terms of closest. Each row contains the distance of a place to another place.
In other words, the array looks something like:
China India USA Japan
China 0 50 4000 2000
India 50 0 4100 2100
USA 4050 4120 2 3000
Japan 2010 1950 2997 0
As you can see, the value to self is 0, sometimes it's a small number like 2-10 (not really sure thou) because Google distance matrix API sometimes returns big values for same origin and destination too which is why I replaced it to 9999999999999999 throughout the diagonal in my original array above.
The goal is to get the sorting in terms of distance. The first entry is the starting point so in the case of the hypothetical array above it would be:
[0 1 3 2] i.e. From China to India, then India to Japan, and Japan to the USA. My goal is to use array_multisort, in the end, to order just the keys with the place names so that it's ordered properly.
The code I came up with isn't working as I hoped for:
$order = [0];
$i = 0;
$nextItemToProcessIndex = 0;
foreach($tempMatrix as $key => $entry) {
$tempMatrix[$key][$key] = PHP_INT_MAX;
}
while(!empty($tempMatrix)) {
$closestItemIndex = array_search(min($tempMatrix[$nextItemToProcessIndex]), $tempMatrix[$nextItemToProcessIndex]);
array_push($order, $closestItemIndex);
$this->pull_item($tempMatrix, $nextItemToProcessIndex);
$nextItemToProcessIndex = $closestItemIndex - 1;
$i++;
}
dd($order);
...
...
public function pull_item(&$array, $offset) {
array_walk($array, function (&$v) use ($offset) {
array_splice($v, $offset, 1);
});
$row = array_splice($array, $offset, 1);
return $row;
}
If altering matrix column labels is ok for you - you can do sorting like that:
$dist = [
"China" => ['China' => 0, 'India' => 50, 'USA' => 4000, 'Japan' => 2000],
"India" => ['China' => 50, 'India' => 0, 'USA' => 4100, 'Japan' => 2100],
"USA " => ['China' => 4050, 'India' => 4120, 'USA' => 2, 'Japan' => 3000],
"Japan" => ['China' => 2010, 'India' => 1950, 'USA' => 2997, 'Japan' => 0]
];
// sort columns by distance
foreach ($dist as $key => $value) {
asort($dist[$key]);
}
// display distances matrix
foreach ($dist as $row_c => $data) {
$row1 = "\t";
$row2 = $row_c . " : ";
foreach ($data as $col_c => $col_dist) {
$row1 .= $col_c . "\t";
$row2 .= $col_dist . "\t";
}
echo "{$row1}\n{$row2}\n\n";
}
Outputs:
China India Japan USA
China : 0 50 2000 4000
India China Japan USA
India : 0 50 2100 4100
USA Japan China India
USA : 2 3000 4050 4120
Japan India China USA
Japan : 0 1950 2010 2997
I was able to figure out a solution to this on my own. Posting here if anyone else comes across the same issue:
$nextItemToProcess = 0;
$order = [0];
foreach(range(0, count($tempMatrix) - 2) as $i) {
if(count(array_diff_key($tempMatrix[$nextItemToProcess], $order)) == 1) {
$order = array_merge($order, array_diff(range(0, count($tempMatrix)-1), $order));
break;
}
$diffArray = array_diff_key($tempMatrix[$nextItemToProcess], array_flip($order));
$closestItemIndex = array_search(min($diffArray), $tempMatrix[$nextItemToProcess]);
array_push($order, $closestItemIndex);
$nextItemToProcess = $closestItemIndex;
}
$ascOrigins = [];
foreach($order as $i) {
array_push($ascOrigins, $arrOrigins[$i]);
}
ascOrigins is the array in ascending order of place distances. I thought I could use php's array_multisort but it wasn't useful since here I'm sorting the keys of arr based on the values of order. (I know an array_flip could have done the trick but this works fine).

PHP Trader macd returns false

I want to use trader_macd but it returns always false.
I am using the default parameters:
$data = [
0 => "0.06945900",
1 => "0.06945200",
2 => "0.06948100",
3 => "0.06944100",
4 => "0.06939800",
5 => "0.06941800",
6 => "0.06942300",
7 => "0.06940000",
8 => "0.06937700",
9 => "0.06937200",
10 => "0.06940000",
11 => "0.06939800",
12 => "0.06941100",
13 => "0.06944500",
14 => "0.06940100",
15 => "0.06942600",
16 => "0.06941500",
17 => "0.06941400",
18 => "0.06939900",
19 => "0.06941400",
20 => "0.06940700",
21 => "0.06938100",
22 => "0.06940400",
23 => "0.06937400",
24 => "0.06937000",
25 => "0.06939700"]
$result = trader_macd($data, 12, 26, 9)
When I set the last parameter ($signalPeriod) then get an array with 0 values:
0 => array:1 [▼
24 => -0.0
]
1 => array:1 [▼
24 => -0.0
]
2 => array:1 [▼
24 => -0.0
]
When I am using other methods like trader_ema with same $data it works fine.
I also set trader.real_precision to 8.
ini_set('trader.real_precision', '8');
What I am doing wrong?
My Systems uses php 7.2.7 with trader 0.5.0.
You don't have enough data to calculate the signal line you chose (9 day EMA of MACD line). Add eight more entries to your data array, and you'll get a result. Or lower the signal line period

Categories