i'm kinda new with Laravel Framework. And now confused with data type like array and object. I got a data like this :
"data[0].detailItem" => array:2 [
0 => array:3 [
"itemNo" => "100011"
"unitPrice" => 0
"quantity" => 6
]
1 => array:3 [
"itemNo" => "102972"
"unitPrice" => 0
"quantity" => 3
]
]
is there a way I change it into :
"data[0].detailItem" => [
{
"itemNo" => "100011"
"unitPrice" => 0
"quantity" => 6
},
{
"itemNo" => "102972"
"unitPrice" => 0
"quantity" => 3
}
]
Edit :
Heres the code
public function apiStore(Request $request){
$detailReceiveItem = DB::table('t_receive_item_detail')->where('receive_item_id','=',$request->idReceiveItem)->get();
$detailItem = [];
foreach($detailReceiveItem as $key=>$receiveItem){
$temp = [
'itemNo' => $receiveItem->item_code,
'unitPrice' => 0,
'quantity' => $receiveItem->qty,
];
array_push($detailItem,$temp);
}
$input = [
'data[0].vendorNo' => $request->vendorNo,
'data[0].receiveNumber' => $request->receiveNumber,
'data[0].transDate' => date('d/m/Y',strtotime($request->transDate)),
'data[0].detailItem' => (object)$detailItem
];
$insertReceiveItem = Http::withHeaders([
'Authorization' => 'Bearer '.session()->get('access_token'),
'X-Session-ID' => session()->get('X-Session-ID'),
])->asForm()->post(session()->get('host').'api url here',$input);
return response()->json(['status'=>1, 'url'=>'/api-accurate/receive-item','message'=>'Receive Item Created Successfully!']);
}
That's my code. I need to convert $detailItem into a object.
Related
Trying to replace/update type A record name # using godaddy api in laravel with Guzzle
https://developer.godaddy.com/doc/endpoint/domains#/v1/recordReplaceTypeName
Valid Headers
$godaddy = Http::withHeaders([
'Authorization' => 'sso-key ' . config('godaddy.key') . ':' . config('godaddy.secret'),
]);
I try the get method and it works
$response = $godaddy->get('https://api.godaddy.com/v1/domains/example.com/records/A/#');
Method put
$response = $godaddy->put('https://api.godaddy.com/v1/domains/example.com/records/A/#', [
'records' => [
'data' => '127.0.0.1',
'priority' => 0,
'ttl' => 0,
'weight' => 0
],
]);
Response details
array:3 [▼
"code" => "INVALID_BODY"
"fields" => array:1 [▼
0 => array:3 [▼
"code" => "UNEXPECTED_TYPE"
"message" => "is not a array"
"path" => "records"
]
]
"message" => "Request body doesn't fulfill schema, see details in `fields`"
]
¿What i am doing bad?
Edited:
Try
$records = json_decode('[{"data": "127.0.0.1","name": "#","port": 65535,"priority": 0,"protocol": "string","service": "string","ttl": 0,"type": "A","weight": 0}]');
$response = $godaddy->put('https://api.godaddy.com/v1/domains/example.com/records', [
'records' => $records,
]);
$response = $godaddy->put('https://api.godaddy.com/v1/domains/example.com/records/A/#', [
'records' => [
[
'data' => '127.0.0.1',
'priority' => 0,
'ttl' => 0,
'weight' => 0
]
],
]);
I'm guessing records is supposed to be an array of arrays\objects:
$response = $godaddy->put('https://api.godaddy.com/v1/domains/example.com/records/A/#', [
'records' => [
[
'data' => '127.0.0.1',
'priority' => 0,
'ttl' => 0,
'weight' => 0
]
],
]);
I think you need to work with array of objects. You need to encoding with right way.
$result = [
[
"data" => "string",
"port" => 65535,
"priority" => 0,
"protocol" => "string",
"service" => "string",
"ttl" => 0,
"weight" => 0
]
];
$records = json_encode($result);
$response = $godaddy->put('https://api.godaddy.com/v1/domains/example.com/records/A/#', [
'records' => $records,
]);
I have this static code today that works fine, now i need to make the
order line dynamic. The code is from a e-commerce store.
$order = [
"purchase_country" => "SE",
"purchase_currency" => "SEK",
"locale" => "sv-SE",
"order_amount" => 10000,
"order_tax_amount" => 2000,
"order_lines" => [
[
"type" => "physical",
"reference" => "123050",
"name" => "Tomatoes",
"quantity" => 10,
"quantity_unit" => "kg",
"unit_price" => 600,
"tax_rate" => 2500,
"total_amount" => 6000,
"total_tax_amount" => 1200
],
[
"type" => "physical",
"reference" => "543670",
"name" => "Bananas",
"quantity" => 1,
"quantity_unit" => "bag",
"unit_price" => 5000,
"tax_rate" => 2500,
"total_amount" => 4000,
"total_discount_amount" => 1000,
"total_tax_amount" => 800
]
],
"merchant_urls" => [
"terms" => "https://www.example.com/villkor.php",
"cancellation_terms" => "https://www.example.com/terms/cancellation.html",
"checkout" => "https://www.example.com/_script/checkout.php",
"confirmation" => "https://www.example.com/_script/checkout.php",
// Callbacks
"push" => "https://www.example.com/api/push",
"validation" => "https://www.example.com/api/validation",
"shipping_option_update" => "https://www.example.com/api/shipment",
"address_update" => "https://www.example.com/api/address",
"notification" => "https://www.example.com/api/pending",
"country_change" => "https://www.example.com/api/country"
]
];
Now I need to make order_lines dynamic.
I have tried this code:
$order = [
"purchase_country" => "SE",
"purchase_currency" => "SEK",
"locale" => "sv-SE",
"order_amount" => 10000,
"order_tax_amount" => 2000,
"order_lines" => [
$cartsql="select a, b, c, d from lux_cart WHERE status = 0 ORDER BY a DESC";
if ($result=mysqli_query($mysqli,$cartsql)){
while ($cartrow=mysqli_fetch_row($result)){
$itemid = $cartrow[0];
$prodTitel = $cartrow[1];
$antalcart = $cartrow[2];
$prodPris = $cartrow[3];
[
"type" => "physical",
"reference" => "$itemid",
"name" => "$prodTitel",
"quantity" => $antalcart,
"unit_price" => $prodPris,
"tax_rate" => 2500,
"total_amount" => $prodPris,
"total_tax_amount" => 1200
],
}
}
"merchant_urls" => [
"terms" => "https://www.example.com/villkor.php",
"cancellation_terms" => "https://www.example.com/terms/cancellation.html",
"checkout" => "https://www.example.com/_script/checkout.php",
"confirmation" => "https://www.example.com/_script/checkout.php",
// Callbacks
"push" => "https://www.example.com/api/push",
"validation" => "https://www.example.com/api/validation",
"shipping_option_update" => "https://www.example.com/api/shipment",
"address_update" => "https://www.example.com/api/address",
"notification" => "https://www.example.com/api/pending",
"country_change" => "https://www.example.com/api/country"
]
];
But the page broken without any error message. I think I need to make some array ore something to resolve this issue. Can someone help me figure out what I am doing wrong here?
You can't execute PHP code inside an array definition like that. That's causing a parse error that's the reason your page is broken with no error. (You can change settings on your development server to show these errors as well.) Instead, define the array with an empty array for order_lines
$order = [
"purchase_country" => "SE",
"purchase_currency" => "SEK",
"locale" => "sv-SE",
"order_amount" => 10000,
"order_tax_amount" => 2000,
"order_lines" => [],
"merchant_urls" => [
"terms" => "https://www.example.com/villkor.php",
"cancellation_terms" => "https://www.example.com/terms/cancellation.html",
"checkout" => "https://www.example.com/_script/checkout.php",
"confirmation" => "https://www.example.com/_script/checkout.php",
// Callbacks
"push" => "https://www.example.com/api/push",
"validation" => "https://www.example.com/api/validation",
"shipping_option_update" => "https://www.example.com/api/shipment",
"address_update" => "https://www.example.com/api/address",
"notification" => "https://www.example.com/api/pending",
"country_change" => "https://www.example.com/api/country"
]
];
Then fill that key with values from your query.
$cartsql="select a, b, c, d from lux_cart WHERE status = 0 ORDER BY a DESC";
if ($result=mysqli_query($mysqli,$cartsql)){
while ($cartrow=mysqli_fetch_row($result)){
$itemid = $cartrow[0];
$prodTitel = $cartrow[1];
$antalcart = $cartrow[2];
$prodPris = $cartrow[3];
// Use [] to append the rows to the existing key
$order['order_lines'][] = [
"type" => "physical",
"reference" => "$itemid",
"name" => "$prodTitel",
"quantity" => $antalcart,
"unit_price" => $prodPris,
"tax_rate" => 2500,
"total_amount" => $prodPris,
"total_tax_amount" => 1200
];
}
}
I need a certain array format like this:
$data = [
1 => ['order' => 3],
2 => ['order' => 2],
3 => ['order' => 1]
];
So when I do:
$ids = $items->transform(function ($item) {
return [$item->id => ['order' => rand(1,10)]];
})->all();
Gives:
array:3 [
0 => array:1 [
1 => array:1 [
"order" => 8
]
]
1 => array:1 [
2 => array:1 [
"order" => 3
]
]
2 => array:1 [
3 => array:1 [
"order" => 10
]
]
]
How can I transform the array in a specific way I need above? TIA
you can use mapWithKeys, docs is here: https://laravel.com/docs/5.5/collections#method-mapwithkeys
$keyed = $items->mapWithKeys(function ($item) {
return [$item->id => ['order' => rand(1,10)]];
});
$ids = $keyed->all();
So I have array like this one
[
'custid' => [
'customer_number_1' => '20098374',
'customer_number_8' => '20098037',
'customer_number_15' => '20098297'
],
'destid' => [
'destination_numbers_1' => [
(int) 0 => '20024838',
(int) 1 => '20041339'
],
'destination_numbers_8' => [
(int) 0 => '20008293'
],
'destination_numbers_15' => [
(int) 0 => '20016969',
(int) 1 => '20022919',
(int) 2 => '20025815',
(int) 3 => '20026005',
(int) 4 => '20027083',
(int) 5 => '20045497'
]
]
]
Goal is to merge cust id with destid in pairs and should look like so
[
(int) 0 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098374',
'sap_destination_id' => '20024838'
],
(int) 1 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098374',
'sap_destination_id' => '20041339',
],
(int) 2 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098037',
'sap_destination_id' => '20008293,
],
(int) 3 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098297'
'sap_destination_id' => '20016969',
],
...
I have tried with code below, but I am getting destination_id number as array and duplicated customer numbers. Also I have tried with array walk but result is same.
$data = [];
foreach ($sap_data['custid'] as $custid) {
foreach ($sap_data['destid'] as $destid) {
$data[] = [
'user_id' => 1,
'sap_customer_id' => $custid,
'sap_destination_id' => $destid
];
}
}
Thx for helping!
You should make inner loop in other way
foreach($sap_data['custid'] as $k => $custid) {
// Make destination key
$dkey = str_replace('customer_number', 'destination_numbers', $k);
// And get array, for example, destination_numbers_1 for customer_number_1
foreach ($sap_data['destid'][$dkey] as $destid) {
$data[] = [
'user_id' => 1,
'sap_customer_id' => $custid,
'sap_destination_id' => $destid
];
}
}
demo on eval.in
I have 2 arrays, as you will see bellow, and I want theme to combine into a single one. First 2 arrays have sub-arrays with a sum and a month. The result array(3rd), must have the sum of first 2 arrays for the same month.
array:3 [
0 => array:2 [
"sum" => 179.0
"month" => "2016-01"
]
1 => array:2 [
"sum" => 34.0
"month" => "2016-02"
]
2 => array:2 [
"sum" => 67.0
"month" => "2016-03"
]
]
array:2 [
0 => array:2 [
"sum" => 143.25
"month" => "2016-01"
]
1 => array:2 [
"sum" => 479.0
"month" => "2016-03"
]
]
Total:
array:3 [
0 => array:3 [
"sum" => 313.25
"month" => "2016-01"
]
1 => array:2 [
"sum" => 34.0
"month" => "2016-02"
]
2 => array:2 [
"sum" => 546.0
"month" => "2016-03"
]
]
What I tried:
for($i=0;$i<count($com1);$i++){
for ($j=0; $j < count($com2); $j++) {
if($com1[$i]['month'] == $com2[$j]['month']){
$total = $com1[$i]['sum']+$com2[$j]['sum']
}
}
}
But it doesn't give the result I want
I am abit noob...I hope someone can help. Thanks
Try this:
$com1 = [
0 => [
'sum' => 179.0,
'month' => '2016-01'
],
1 => [
'sum' => 34.0,
'month' => '2016-02'
],
2 => [
'sum' => 67.0,
'month' => '2016-03'
]
];
$com2 = [
0 => [
'sum' => 143.25,
'month' => '2016-01'
],
1 => [
'sum' => 479.0,
'month' => '2016-03'
]
];
$total = [];
foreach ($com1 as $data) {
$total[$data['month']] = $data;
}
foreach ($com2 as $data) {
if (!isset($total[$data['month']])) {
$total[$data['month']] = [
'sum' => 0,
'month' => $data['month']
];
}
$total[$data['month']]['sum'] += $data['sum'];
}
print_r($total);
Here is a solution that generates a non-associative array (as you indicated in your question):
$month_idx = []; // helper to identify at which index a certain month is stored
$result = [];
foreach (array_merge($data1, $data2) as $idx => $row) {
$month = $row["month"];
if (!isset($month_idx[$month])) {
$month_idx[$month] = count($result);
$result[] = $row;
} else {
$result[$month_idx[$month]]["sum"] += $row["sum"];
}
}
var_export($result);
When $data1 and $data2 are:
$data1 = array (
array(
"sum" => 179.0,
"month" => "2016-01"
),
array(
"sum" => 34.0,
"month" => "2016-02"
),
array(
"sum" => 67.0,
"month" => "2016-03"
)
);
$data2 = array (
array(
"sum" => 143.25,
"month" => "2016-01"
),
array(
"sum" => 479.0,
"month" => "2016-03"
)
);
Then output is:
array (
0 =>
array (
'sum' => 322.25,
'month' => '2016-01',
),
1 =>
array (
'sum' => 34,
'month' => '2016-02',
),
2 =>
array (
'sum' => 546,
'month' => '2016-03',
),
)