How to group array with the same ids in new array?

Asked

Viewed 71 times

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 in foreach, a simple example in php would be welcome, but not necessary to answer the question.

  • I believe you can give an array_push. Following documentation: https://www.php.net/manual/en/function.array-push.php

  • 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 :)

1 answer

0


With the code below it is possible to solve the problem in the array by recreating the array based on the id, thus taking into account what is requested here.

Code:

$GA_Domain = "GA Domain";
$Mailbox_Name = "Mailbox Name";
$GA_FirstName = "GA FirstName";
$GA_LastName = "GA LastName";

$out = array();
$arrayobj = $decodedata;
foreach($arrayobj as $data => $value) {
    $arr_id = $arrayobj[$data]["id"];
    $arr_value = $arrayobj[$data]["value"] ?? '';

    if(strpos($arrayobj[$data]["fieldname"], $GA_Domain) !== false)
    {
        $out[$arr_id]['id'] = $arr_id;
        $out[$arr_id]["$GA_Domain"] = $arr_value;
    }
    if(strpos($arrayobj[$data]["fieldname"], $Mailbox_Name) !== false)
    {
        $out[$arr_id]['id'] = $arr_id;
        $out[$arr_id]["$Mailbox_Name"] = $arr_value;
    }
    if(strpos($arrayobj[$data]["fieldname"], $GA_FirstName) !== false)
    {
        $out[$arr_id]['id'] = $arr_id;
        $out[$arr_id]["$GA_FirstName"] = $arr_value;
    }
    if(strpos($arrayobj[$data]["fieldname"], $GA_LastName) !== false)
    {
        $out[$arr_id]['id'] = $arr_id;
        $out[$arr_id]["$GA_LastName"] = $arr_value;
    }
}

return array("ProdCustomFields" => $out); 

To call in the whmcs template, just use the code below with the foreach.

Code:

{foreach item=field from=$ProdCustomFields}
    {if $field.id eq $service.id}
        {$GSUsername=$field['Mailbox Name']}
        ...
    {/if}
{/foreach}

This would allow you to display and manipulate the data in an organized manner according to the $service.id

Code reference in whmcs: Display product custom Fields on clientareaproducts.tpl page

By adapting the code you can manipulate any custom Filed, according to your needs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.