Add a dynamic key => value options in 3 level array option - php

I have an array as main options in my code.
In case, I want to add dynamic key => values into specific options array key.
This is my main options array:
$configarray1 = array(
"name" => "Addon",
"description" => "module for whmcs",
"version" => "1.1",
"author" => "Me",
"language" => "english",
"fields" => array(
"sender" => array (
"FriendlyName" => "Sender",
"Type" => "dropdown",
"Options" => strtolower($GatewaysIM),
"Description" => $getBalance,
"Default" => $Defaultsender,
),
"validateday" => array (
"FriendlyName" => "Days for Re-validation",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "90",
),
)
);
I want to add this sender array options in configarray1 fields key:
if($sender == 'sender1'){
$configarray2['fields'] = array(
"username" => array (
"FriendlyName" => "username",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
),
"password" => array (
"FriendlyName" => "password",
"Type" => "password",
"Size" => "25",
"Description" => "",
"Default" => "",
)
);
} elseif($sender == 'sender2'){
$configarray2['fields'] = array(
"line" => array (
"FriendlyName" => "line",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
)
);
}
Output array must be like this below when sender is sender1:
$configarray = array(
"name" => "Addon",
"description" => "module for whmcs",
"version" => "1.1",
"author" => "Me",
"language" => "english",
"fields" => array(
"sender" => array (
"FriendlyName" => "Sender",
"Type" => "dropdown",
"Options" => strtolower($GatewaysIM),
"Description" => $getBalance,
"Default" => $Defaultsender,
),
"username" => array (
"FriendlyName" => "username",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
),
"password" => array (
"FriendlyName" => "password",
"Type" => "password",
"Size" => "25",
"Description" => "",
"Default" => "",
),
"validateday" => array (
"FriendlyName" => "Days for Re-validation",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "90",
),
)
);
I tested array push but this adds a key in arrays first place and not in 'fields' key, my code was this $configarray = array_push($configarray1,$configarray2); but this not works !
I also tested sum of two arrays ($configarray = $configarray1 + $configarray2) but this is the same as array_push and returns wrong output for me.
How can i resolve this problem ?!

You should just declare it. You don't need a new variable.
Assuming you have the original array named as $configarray1, just:
if($sender == 'sender1'){
$configarray1['fields']['username'] = array(
"FriendlyName" => "username",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
);
$configarray1['fields']['password'] => array(
"FriendlyName" => "password",
"Type" => "password",
"Size" => "25",
"Description" => "",
"Default" => "",
);
} elseif($sender == 'sender2'){
$configarray1['fields']['line'] = array (
"FriendlyName" => "line",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
);
}

Related

multi-dimensional array hierarchy

I like to know how can I get the hierarchy by the identifier.
This is an example :
$inputArray = array(
array(
"text" => "Dir1",
"parent_id" => "",
"id" => "1",
"filesize" => "109"
),array(
"text" => "dir2",
"parent_id" => "",
"id" => "2",
"filesize" => "88",
"children" => array(
"text" => "Dir3",
"parent_id" => "2",
"id" => "3",
"filesize" => "",
"children" => array(
"text" => "dir4",
"parent_id" => "3",
"id" => "4",
"filesize" => "",
"children" => array(
"text" => "dir5",
"parent_id" => "4",
"id" => "4",
"filesize" => ""
)
)
)
));
looking for this example :
dir3/dir4/dir5
function getText($array) {
$save[] = $array['text'];
if (isset($array['children'])) {
$save = array_merge($save, getText($array['children']));
}
return $save;
}
foreach($inputArray as $x) {
echo implode('/', getText($x)) . "\n";
}
result
Dir1
dir2/Dir3/dir4/dir5
demo

Array to repeat

$a = array(
array(
"name" => "jack",
"data" => "123",
"link_uid" => "1",
),
array(
"name" => "jack",
"data" => "134",
"link_uid" => "2",
),
array(
"name" => "tom",
"data" => "567",
"link_uid" => "3",
),
array(
"name" => "tom",
"data" => "098",
"link_uid" => "4",
)
);
to
$a = array(
array(
"name" => "jack",
"data" => "123",
"link_uid" => "1",
),
array(
"name" => " ",
"data" => "134",
"link_uid" => "2",
),
array(
"name" => "tom",
"data" => "567",
"link_uid" => "3",
),
array(
"name" => " ",
"data" => "098",
"link_uid" => "4",
)
);
Do you want just repeated name to blank
<?php
$temp = array();
foreach($a as $key=>$value){
if(in_array($value["name"],$temp)){
$a[$key]["name"] = "";
}else {
$temp[] = $value["name"];
}
}
print_r($a);
?>
Live demo : https://eval.in/855975

How to consolidate duplicate elements of this array in PHP?

I have an array like this:
$array = array(
0 => array("ordernumber" => "1", "name" => "John", "product" => "laptop", "component" => "memory"),
1 => array("ordernumber" => "1", "name" => "John", "product" => "laptop", "component" => "cpu"),
2 => array("ordernumber" => "1", "name" => "John", "product" => "desktop", "component" => "cpu"),
3 => array("ordernumber" => "2", "name" => "Pete", "product" => "monitor", "component" => "")
);
It contains data from different orders, but as you can see an order can contain multiple purchased products, and each product can contain different 'components'. There's alot of duplicate data in this array, so I would like to turn it into this:
$array = array(
0 => array(
"order" => array(
"ordernumber" => "1", "name" => "John"
),
"products" => array(
0 => array(
"name" => "laptop",
"components" => array("memory", "cpu")
),
1 => array(
"name" => "desktop",
"components" => array("cpu")
)
)
),
1 => array(
"order" => array(
"ordernumber" => "2", "name" => "Pete"
),
"products" => array(
0 => array(
"name" => "monitor",
"components" => array()
)
)
)
);
What would be a good way to do this?
Please use below code to make the solution what you want
<?php
$array = array(
0 => array("ordernumber" => "1", "name" => "John", "product" => "laptop", "component" => "memory"),
1 => array("ordernumber" => "1", "name" => "John", "product" => "laptop", "component" => "cpu"),
2 => array("ordernumber" => "1", "name" => "John", "product" => "desktop", "component" => "cpu"),
3 => array("ordernumber" => "2", "name" => "Pete", "product" => "monitor", "component" => "")
);
$final_array = [];
foreach($array as $k=>$v){
$final_array[$v['ordernumber']]['order']['ordernumber'] = $v['ordernumber'];
$final_array[$v['ordernumber']]['order']['name'] = $v['name'];
$final_array[$v['ordernumber']]['products'][$v['product']]['name'] = $v['product'];
$final_array[$v['ordernumber']]['products'][$v['product']]['components'][] = $v['component'];
}
// You can skip this foreach if there will not metter of KEY of an array in your code!
$final_array = array_values($final_array);
foreach($final_array as $k=>$v){
$final_array[$k]['products'] = array_values($final_array[$k]['products']);
}
echo "<pre>";
print_r($final_array);
?>
its should work!!

Dolibarr soap web service usage with php

I am trying to use a Dolibarr soap web service with php
Dolibarr web service
this is what i got for the moment:
$url = "http://localhost/seko/dollibar/dolibarr-3.7.1/htdocs/webservices/server_invoice.php?wsdl";
$client = new SoapClient($url);
$authentication = array(
"dolibarrkey" => "xxxxx",
"sourceapplication" => "",
"login" => "xxxx",
"password" => "xxxxxx",
"entity" => "1"
);
$line = array(
"id" => "57",
"type" => 0,
"desc" => "SEKO",
"vat_rate" => 16.000,
"qty" => 03,
"unitprice" => 10500.00000000,
"total_net" => 10500.0000000,
"total_vat" => 1680.00000000,
"total" => 12180.0000000,
"date_start" => "",
"date_end" => "",
"payment_mode_id" => "efectivo",
"product_id" => 1,
"product_ref" => "",
"product_label" => "",
"product_desc" => ""
);
$invoice = array(
"id" => "57",
"ref" => "0007",
"ref_ext" => "test",
"thirdparty_id" => 3,
"fk_user_author" => "1",
"fk_user_valid" => "1",
"date" => date("Y-m-d"),
"date_due" => date("Y-m-d"),
"date_creation" => date("Y-m-d h:i:sa"),
"date_validation" => date("Y-m-d h:i:sa"),
"date_modification" => "",
"type" => 0,
"total_net" => 10500.00000000,
"total_vat" => 1680.00000000,
"total" => 12180.0000000,
"note_private" => "",
"note_public" => "",
"status" => 2,
"close_code" => "",
"close_note" => "",
"project_id" => "",
"lines" => $lines
);
$res = $client->createInvoice($authentication, $invoice);
var_dump($res);
I get the following error:
An uncaught Exception was encountered
Type: SoapFault
Message: looks like we got no XML document
When I use the getInvoice method of the service it works fine. But not the creatInvoice method. I am sure the problem is with my $line array but I do not know how to fix it.
According to XML documentation (open this http://localhost/seko/dollibar/dolibarr-3.7.1/htdocs/webservices/server_invoice.php?wsdl in your webbrowser)
please note that you need to remove the "payment_mode_id" field from the $line array and move it to your $invoice array
Like this :
$line = array(
"id" => "57",
"type" => 0,
"desc" => "SEKO",
"vat_rate" => 16.000,
"qty" => 03,
"unitprice" => 10500.00000000,
"total_net" => 10500.0000000,
"total_vat" => 1680.00000000,
"total" => 12180.0000000,
"date_start" => "",
"date_end" => "",
"product_id" => 1,
"product_ref" => "",
"product_label" => "",
"product_desc" => ""
);
$invoice = array(
"id" => "57",
"ref" => "0007",
"ref_ext" => "test",
"thirdparty_id" => 3,
"fk_user_author" => "1",
"fk_user_valid" => "1",
"date" => date("Y-m-d"),
"date_due" => date("Y-m-d"),
"date_creation" => date("Y-m-d h:i:sa"),
"date_validation" => date("Y-m-d h:i:sa"),
"date_modification" => "",
"payment_mode_id" => "efectivo",
"type" => 0,
"total_net" => 10500.00000000,
"total_vat" => 1680.00000000,
"total" => 12180.0000000,
"note_private" => "",
"note_public" => "",
"status" => 2,
"close_code" => "",
"close_note" => "",
"project_id" => "",
"lines" => $lines
);
Hoping help
Regards

Rearranging multidimensional array in PHP [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have an array that I need to rearange a bit.
I am building a voice server status widget for pyrocms.
I use for querying the voice server -> https://github.com/Austinb/GameQ
I use for the template -> https://github.com/BorisMoore/jsrender
whit a static table i haven created this !
www.i.stack.imgur.com/4a3Am.png
I get an array return and it looks like this.:
Array (
"Ventrilo" => Array (
"name" => "The Evil Teddys",
"phonetic" => "The Evil Teddys",
"comment" => "",
"auth" => "1",
"maxclients" => "20",
"voicecodec" => "0,GSM 6.10",
"voiceformat" => "3,44 KHz, 16 bit",
"uptime" => "25799604",
"platform" => "Linux-i386",
"version" => "3.0.3",
"channelcount" => "7",
"teams" => Array (
"0" => Array (
"cid" => "846",
"pid" => "0",
"prot" => "0",
"name" => "<TET>",
"comm" => ""
),
"1" => Array (
"cid" => "847",
"pid" => "0",
"prot" => "0",
"name" => "#ISOLATIECEL",
"comm" => ""
),
"2" => Array (
"cid" => "848",
"pid" => "0",
"prot" => "0",
"name" => "Relax Take It Easy",
"comm" => ""
),
"3" => Array (
"cid" => "849",
"pid" => "846",
"prot" => "0",
"name" => "Foodfightkitchen",
"comm" => ""
),
"4" => Array (
"cid" => "850",
"pid" => "846",
"prot" => "0",
"name" => "WOW",
"comm" => ""
),
"5" => Array (
"cid" => "851",
"pid" => "849",
"prot" => "0",
"name" => "Bad Company",
"comm" => ""
),
"6" => Array(
"cid" => "852",
"pid" => "850",
"prot" => "0",
"name" => "The G Channel",
"comm" => ""
)
),
"clientcount" => "3",
"players" => Array (
"0" => Array (
"admin" => "1",
"cid" => "846",
"phan" => "0",
"ping" => "18",
"sec" => "345345",
"name" => "Shorty*",
"comm" => ""
),
"1" => Array (
"admin" => "1",
"cid" => "851",
"phan" => "0",
"ping" => "20",
"sec" => "11988",
"name" => "Swifty",
"comm" => ""
),
"2" => Array (
"admin" => "1",
"cid" => "846",
"phan" => "0",
"ping" => "30",
"sec" => "678674",
"name" => "The1one12",
"comm" => ""
)
),
"gq_online" => "0",
"gq_address" => "172.0.0.1",
"gq_port" => "3812",
"gq_prot" => "ventrilo",
"gq_type" => "ventrilo"
)
and I want my end result to be like this:
cid = Channel ID
pid = Parent ID
Array (
"Ventrilo" => Array (
"name" => "The Evil Teddys",
"phonetic" => "The Evil Teddys",
"comment" => "",
"auth" => "1",
"maxclients" => "20",
"voicecodec" => "0,GSM 6.10",
"voiceformat" => "3,44 KHz, 16 bit",
"uptime" => "25799604",
"platform" => "Linux-i386",
"version" => "3.0.3",
"channelcount" => "7",
"teams" => Array (
"0" => Array (
"cid" => "846",
"pid" => "0",
"prot" => "0",
"name" => "<TET>",
"comm" => "",
"players" => Array (
"0" => Array (
"admin" => "1",
"cid" => "846",
"phan" => "0",
"ping" => "18",
"sec" => "345345",
"name" => "Shorty*",
"comm" => "vet verwacht je niet"
),
"1" => Array (
"admin" => "1",
"cid" => "846",
"phan" => "0",
"ping" => "30",
"sec" => "678674",
"name" => "The1one12",
"comm" => "grappig !"
)
),
"teams" => Array(
"0" => Array (
"cid" => "849",
"pid" => "846",
"prot" => "0",
"name" => "Foodfightkitchen",
"comm" => "",
"players" => Array (),
"teams" => Array(
"0" => Array (
"cid" => "851",
"pid" => "849",
"prot" => "0",
"name" => "Bad Company",
"comm" => "",
"players" => Array (
"0" => Array (
"admin" => "1",
"cid" => "851",
"phan" => "0",
"ping" => "20",
"sec" => "11988",
"name" => "Swifty",
"comm" => "nu nog Dynamisch"
)
),
"teams" => Array(
)
)
)
),
"1" => Array (
"cid" => "850",
"pid" => "846",
"prot" => "0",
"name" => "WOW",
"comm" => "",
"players" => Array (),
"teams" => Array(
"0" => Array(
"cid" => "852",
"pid" => "850",
"prot" => "0",
"name" => "The G Channel",
"comm" => "",
"players" => Array (),
"teams" => Array(
)
)
)
)
)
),
"1" => Array (
"cid" => "847",
"pid" => "0",
"prot" => "0",
"name" => "#ISOLATIECEL",
"players" => Array (),
"teams" => Array(
)
),
"2" => Array (
"cid" => "848",
"pid" => "0",
"prot" => "0",
"name" => "Relax Take It Easy",
"comm" => "",
"players" => Array (),
"teams" => Array(
)
)
),
"clientcount" => "3",
"gq_online" => "1",
"gq_address" => "213.163.76.130",
"gq_port" => "3812",
"gq_prot" => "ventrilo",
"gq_type" => "ventrilo"
)
How can I achieve this ? I have tried many things but nothing with this end result.
what i have now.
$teamArrayConverted['teams'] = convertTeamArray($serverstatus['teams']);
unset($serverstatus['players'], $serverstatus['teams']);
$results = array_merge_recursive($serverstatus, $teamArrayConverted);
function convertTeamArray ($array) {
// First, convert the array so that the keys match the ids
$reKeyed = array();
foreach ($array as $item) {
$reKeyed[(int) $item['cid']] = $item;
}
//print_r($reKeyed);
// Next, use references to associate children with parents
foreach ($reKeyed as $id => $item) {
if (isset($item['pid'], $reKeyed[(int) $item['pid']])) {
$reKeyed[(int) $item['pid']]['teams'][] =& $reKeyed[$id];
}
}
//print_r($reKeyed);
// Finally, go through and remove children from the outer level
foreach ($reKeyed as $id => $item) {
if ($item['pid'] != '0') {
//print_r($reKeyed[$id]);
unset($reKeyed[$id]);
}
}
return array_values($reKeyed);
}
It works in principle like this:
You make each node identifiable by it's numeric id and store it in a hash so it's accessible by it's id.
You take a new hash and insert all elements incl. their children as needed.
In detail discussion of the code is here: Converting an array from one to multi-dimensional based on parent ID values:
// key the array by id
$keyed = array();
foreach($array as &$value)
{
$keyed[$value['id']] = &$value;
}
unset($value);
$array = $keyed;
unset($keyed);
// tree it
$tree = array();
foreach($array as &$value)
{
if ($parent = $value['parent_id'])
$array[$parent]['children'][] = &$value;
else
$tree[] = &$value;
}
unset($value);
$array = $tree;
unset($tree);
var_dump($array); # your result
Replace the names with what you have as variable names and keys.
Create an array based on the pid from the current array which you can use to loop through the children based on the id.
$arr = array(...definition from your post....);
foreach($arr["teams"] as $team)
$teamchildren[$team[pid]][]=$team;
print_r($teamchildren);
Now you can loop through the $teamchildren[0] array and call a function recursively to build the nested team structure under each of the teams
The same concept could be applied to the players but changing the parent in this case to be the cid
(above code is not tested)

Categories