Array to repeat - php

$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

Related

I want to add up the corresponding prices

I want to reassemble the following array for each management_code, class_type.
$varArrayA = array(
array(
"manegement_code" => "1",
"class_type" => array('A','B','C')
),
array(
"manegement_code" => "2",
"class_type" => array('A','B','C')
),
array(
"manegement_code" => "3",
"class_type" => array('A','B','C')
)
);
$varArrayB = array(
array(
"manegement_code" => "1",
"class_type" => "A",
"price" => "200"
),
array(
"manegement_code" => "1",
"class_type" => "B",
"price" => "300"
),
array(
"manegement_code" => "1",
"class_type" => "B",
"price" => "300"
),
array(
"manegement_code" => "2",
"class_type" => "B",
"price" => "300"
)
);
I want this result
$varArrayB = array(
array(
"manegement_code" => "1",
"class_type" => "A",
"price" => "200"
),
array(
"manegement_code" => "1",
"class_type" => "B",
"price" => "600"
),
array(
"manegement_code" => "2",
"class_type" => "B",
"price" => "300"
)
);
I made it as follows.
I'm confused about incorporating conditions in class_type.
I would appreciate it if you could be help me.
foreach ($varArrayA as $i => $varValueA) {
foreach ($varArrayB as $rowow ){
if($rowow['manegement_code']==$varValueA['manegement_code']){
$varArrayC[$i]['manegement_code'] = $rowow['manegement_code'];
$varArrayC[$i]['class_type'] = $rowow['class_type'];
$num = $num+$rowow['price'];
$varArrayC[$i]['price'] = $num;
}
}
}
if(($rowow['manegement_code']==$varValueA['manegement_code']) && ($rowow['class_type']==$varValueA['class_type'])){
$varArrayC[$i]['manegement_code'] = $rowow['manegement_code'];
$varArrayC[$i]['class_type'] = $rowow['class_type'];
$num = $num+$rowow['price'];
$varArrayC[$i]['price'] = $num;
}
Just ask if the class_type is the same as well.

Correct Logic for PHP Array

I need to produce an output like this:
{
"properties": [
{
"name": "name",
"value": "A company name"
},
{
"name": "description",
"value": "A company description"
}
]
}
And i've written the logic as such:
$data = array(
"properties" => array(
"name" => "name",
"value" => $companyname,
"name" => "address",
"value" => $AddressLine1,
"name" => "address2",
"value" => $AddressLine2,
"name" => "address3",
"value" => $AddressLine3,
"name" => "locality",
"value" => $Locality,
"name" => "city",
"value" => $Town,
"name" => "state",
"value" => $County,
"name" => "zip",
"value" => $PostCode,
"name" => "country",
"value" => $Country,
"name" => "phone",
"value" => $Telephone,
"name" => "domain",
"value" => $Website,
"name" => "lead_forensics_business_id",
"value" => $hubspotcompanyid
)
);
Can someone highlight the correct way to produce the above output?
Each name and value pair should be their own arrays. Like this:
$data = [
"properties" => [
[
"name" => "name",
"value" => $companyname,
],
[
"name" => "address",
"value" => $AddressLine1,
],
// And so on
]
];
(I've opted for the shorthand version of creating arrays [] instead of array() since it's, in my opinion, easier to read).
This should do the trick if i remember correctly:
$data = array(
"properties" => array(
array(
"name" => "name",
"value" => $companyname
),
array(
"name" => "address",
"value" => $AddressLine1
),
...
)
);
PHP automatically builds an {} or [] depending on the contents.
So u just use the array() function to build the entire structure.
Something like this ...
$data = array();
array_push($data, array('name' => 'name', 'value' => $companyname));
array_push($data, array('name' => 'address', 'value' => $AddressLine1));
array_push($data, array('name' => 'address2', 'value' => $AddressLine2));
$output = array();
$output['properties'] = $data;
echo json_encode($output);

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

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

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" => "",
);
}

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!!

Categories