0
I’m using a php function to get custom Fields data from whmcs sql, but the operation returns the data in an array, separating each id by products.
That is, it does not return the product with all the fieldname
, returns multiple arrays to the single product.
I need these Ids(fieldname
) get together in an array.
My array logic is below.
Code:
Array(
[0] => Array([id] => 3[fieldname] => GA Domain[value] => teste.net)
[1] => Array([id] => 4[fieldname] => GA Domain[value] => gbr.com)
[2] => Array([id] => 5[fieldname] => GA Domain[value] => )
[3] => Array([id] => 3[fieldname] => GA PurchaseOrderID[value] => )
[4] => Array([id] => 4[fieldname] => GA PurchaseOrderID[value] => whmcs_account_4)
[5] => Array([id] => 5[fieldname] => GA PurchaseOrderID[value] => )
[6] => Array([id] => 3[fieldname] => GA AlternateEmail[value] => [email protected])
[7] => Array([id] => 4[fieldname] => GA AlternateEmail[value] => galternate.com)
[8] => Array([id] => 5[fieldname] => GA AlternateEmail[value] => [email protected])
[9] => Array([id] => 3[fieldname] => Mailbox Name[value] => testeuser1)
[10] => Array([id] => 4[fieldname] => Mailbox Name[value] => meltay)
[11] => Array([id] => 5[fieldname] => Mailbox Name[value] => meunome2)
[12] => Array([id] => 3[fieldname] => GA FirstName[value] => First Name)
[13] => Array([id] => 4[fieldname] => GA FirstName[value] => Mel)
[14] => Array([id] => 3[fieldname] => GA LastName[value] => Last Name)
[15] => Array([id] => 4[fieldname] => GA LastName[value] => Tay)
)
I need the array to look like it’s below.
Code:
Array(
[3] => Array([id] => "3",
["GA Domain"] => "teste.net",
["GA PurchaseOrderID"] => "",
["GA AlternateEmail"] => "[email protected]",
["Mailbox"] => "testeuser1",
["GA FirstName"] => "First Name",
["GA LastName"] => "Last Name"),
[4] => Array([id] => "4",
["GA Domain"] => "gbr.com",
["GA PurchaseOrderID"] => "whmcs_account_4",
["GA AlternateEmail"] => "galternate.com",
["Mailbox"] => "meltay",
["GA FirstName"] => "Mel",
["GA LastName"] => "Tay"),
[5] => Array([id] => "5",
["GA Domain"] => "",
["GA PurchaseOrderID"] => "",
["GA AlternateEmail"] => "[email protected]",
["Mailbox"] => "meunome2",
["GA FirstName"] => "",
["GA LastName"] => "")
)
Below is the php code used in whmcs to generate the array: Code:
function products_list_hook($vars) {
$client = Menu::context('client');
$productcf = Capsule::table('tblcustomfields')
->join('tblcustomfieldsvalues','tblcustomfieldsvalues.fieldid','=','tblcustomfields.id')
->join('tblhosting','tblhosting.id','=','tblcustomfieldsvalues.relid')
->where('tblhosting.userid',$client->id)
->where('tblcustomfields.type','product')
->where('tblcustomfields.fieldtype','text')
->select('tblhosting.id','tblcustomfields.fieldname','tblcustomfieldsvalues.value')
->get();
$encodedata = json_encode($productcf);
$decodedata = json_decode($encodedata, true);
return array("ProdCustomFields" => $decodedata);
}
add_hook("ClientAreaPageProductsServices", 1, "products_list_hook");
This is generating several arrays, and the foreach
below which I am using in the whmcs template {foreach item=field from=$ProdCustomFields} {if $field.id eq $service.id}
You don’t know how to handle them properly.
How could I change this array, so that it generates a single ID for all Keys and related values as demonstrated, in order to get one foreach
more precise?
I tried to make a
array_merge
, but it didn’t work, as I need to use it inforeach
, a simple example inphp
would be welcome, but not necessary to answer the question.– Florida
I believe you can give an array_push. Following documentation: https://www.php.net/manual/en/function.array-push.php
– Nicolas Pereira
Thanks to the suggestion @Nicolaspereira I solved differently, I leave the question open for a few days if you have other options with the same result. I answered with my solution, because I did not find how to do this for the case of custom Fields, I hope it helps others not to go through :)
– Florida