My arrays result is like this one
Array
(
[0] => Array
(
[id] => Bank Transfer
[ec] => 1000
[accounts] => Array
(
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
)
)
[1] => Array
(
[id] => Wired
[ec] => 1001
[accounts] => Array
(
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
[2] => Array
(
[name] => Account ID
[value] =>
)
)
)
)
It's weird because 2nd array of accounts contains same value as first array.
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
How to prevent this duplicated so the 2nd array of accounts will only show
[0] => Array
(
[name] => Account ID
[value] =>
)
Here's my code
$arr = $arr_pay = array();
foreach($site_payment as $key => $value){
if($value['status'] && $value['ec']>=1000){
$payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
foreach ($payment_data_cust as $ke => $va) {
$arr[] = array("name"=>$va,"value"=>'');
}
$spc[] = array(
"id"=>$value['id'],
"ec"=>$value['ec'],
"accounts"=>$arr
);
}
}
Array of $site_payment contains
[Bank Transfer] => Array
(
[id] => Bank Transfer
[ec] => 1000
[status] => 1
[auto_wd_data] => IjZRcWp1aGtzNmZHbjVPZTlkeStGZVNPaWdPY0lrZ0UyQnd6eFhxQUZoR1VEeU82TzVJZkdMelJrZzJKS3lxXC9yTm5meFBndFRlUDQ9Ig==
)
[Dana] => Array
(
[id] => Wired
[ec] => 1001
[status] => 1
[auto_wd_data] => IkNDek9IY1BtelVEeFFxZEtMc0hvalBkbVBRdENEZEJWakZoaFBJWkNBUk09Ig==
)
I want to show the auto_wd_data of $site_payments with different array so it's became the result, but not duplicating in each array
Please help me to solve the problem
Duplication is due to the $arr is not being reset
$arr_pay = array();
foreach($site_payment as $key => $value){
$arr = array(); // Resetting
if($value['status'] && $value['ec']>=1000){
$payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
foreach ($payment_data_cust as $ke => $va) {
$arr[] = array("name"=>$va,"value"=>'');
}
$spc[] = array(
"id"=>$value['id'],
"ec"=>$value['ec'],
"accounts"=>$arr
);
}
}
I have two array as bellow :
First array :
Array
(
[0] => Array
(
[0] => Array
(
[name] => one
[number] => 051
)
[1] => Array
(
[name] => two
[number] => 052
)
[2] => Array
(
[name] => three
[number] => 053
)
)
[1] => Array
(
[0] => Array
(
[name] => four
[number] => 061
)
[1] => Array
(
[name] => five
[number] => 062
)
)
)
I want to make output from first array above
[0] => 051, 052, 053.
[1] => 061, 062.
Array
(
[0] => Array
(
[0] => Array
(
[name] => book
[number] => 41
)
[1] => Array
(
[name] => pencil
[number] => 42
)
)
[1] => Array
(
[name] => eraser
[number] => 71
)
)
I want to make output from second array above
[0] => 41, 42.
[1] => 71.
Please advise. Thank you.
You can make a try like this way with two foreach() loop.
$numbers = [];
foreach ($array as $k => $v) {
$num = [];
foreach ($v as $k2 => $v2) {
$num[] = $v2['number'];
}
$numbers[$k] = implode(',',$num).'.';
}
print_r($numbers);
DEMO: https://3v4l.org/mEeO7
you can try something like this
$arr = Array (
Array (
Array (
"name" => "one",
"number" => "051"
),
Array (
"name" => "two",
"number" => "052"
),
Array (
"name" => "three",
"number" => "053"
)
),
Array (
Array (
"name" => "four",
"number" => "061"
),
Array (
"name" => "five",
"number" => "062"
)
)
);
foreach ($arr as $k => $s_arr) {
echo "[" . $k . "] => ";
foreach ($s_arr as $k2 => $v2) {
echo $v2["number"] . " ";
}
echo "\n";
}
This question already has answers here:
Group subarrays by one column, make comma-separated values from other column within groups
(2 answers)
Closed last month.
here's how it looks in the PHP code:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
print_r($array);
?>
Example of print_r() function output:
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml
)
[1] => Array
(
[name] => filter_amount
[value] => 200-ml
)
[2] => Array
(
[name] => page_size
[value] => 7
)
)
I need to combine duplicates of filter_amount values from the array.
The values of these duplicates must be commas separated and the result should be the following code:
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml,200-ml
)
[1] => Array
(
[name] => page_size
[value] => 7
)
[2] => Array
(
[name] => orderby
[value] => rating
)
[3] => Array
(
[name] => paged
[value] => 1
)
)
Since you want value to be concatenated by a comma, you'll have to make a cycle of it
<?php
//Allow me to change this variable name, just to not create confusion
$content = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
//$content is your initial array
//$outputArray is the final worked-up array
$outputArray = [];
//Let's make a cycle going for every array inside $content
foreach ($content as $innerArray) {
//Does this $innerArray['name'] (filter_ammount) exist in $outputArray in an array
//consisting in key => value where the key is 'name' and equals
//what we look for that is(filter_ammount)?
$key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//If not, let's place this array in the $output array
if ($key === false) {
array_push($outputArray, $innerArray);
} else {
//If exists, then $key is the $key of the $outputArray and let's add to its value
//our current value, that is in our $innerArray, concatenated with a comma
$outputArray[$key]['value'] .= ",". $innerArray['value'];
}
}
//Boom, magic
print_r($outputArray);
//Note: This is going to affect every duplicate it finds, as in:
//If you got 3 arrays with name 'filter_ammount' and 2 arrays with name
//'page_size', it's going to concatenate the filter_ammount and the 'page_size'.
//If you specifically just want filter_ammount,
//replace this -> $key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//with this -> $key = array_search('filter_ammount', array_column($outputArray , 'name'));
?>
References
http://php.net/manual/en/function.array-search.php
http://php.net/manual/en/function.array-column.php
How to combine two items by 2 duplicate columns?
[root#localhost TEST]# php R00.php
Array
(
[0] => Array
(
[0] => S01
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 445
)
[1] => Array
(
[0] => S02
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[2] => Array
(
[0] => S03
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[3] => Array
(
[0] => S04
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 23
)
[4] => Array
(
[0] => S05
[1] => 100.100.100.100
[2] => 192.168.100.100
[3] => 22
)
[5] => Array
(
[0] => S06
[1] => 192.168.200.10
[2] => 192.168.100.100
[3] => 22
)
[6] => Array
(
[0] => S07
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[7] => Array
(
[0] => S08
[1] => 192.168.100.100
[2] => 10.10.100.106
[3] => 446
)
[8] => Array
(
[0] => S09
[1] => 172.16.20.223
[2] => 10.10.10.108
[3] => 447
)
[9] => Array
(
[0] => S10
[1] => 192.168.100.100
[2] => 10.10.10.109
[3] => 448
)
)
[root#localhost TEST]#
combine 1 or 2 items by 2 column duplicate below is result I need
Array
(
[0] => Array
(
[0] => S01 , S04
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 445 , 23
)
[1] => Array
(
[0] => S02 , S03 , S07
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[3] => Array
(
[0] => S05 , S06
[1] => 100.100.100.100 , 192.168.200.10
[2] => 192.168.100.100
[3] => 22
)
[4] => Array
(
[0] => S08
[1] => 192.168.100.100
[2] => 10.10.100.106
[3] => 446
)
[5] => Array
(
[0] => S09
[1] => 172.16.20.223
[2] => 10.10.10.108
[3] => 447
)
[6] => Array
(
[0] => S10
[1] => 192.168.100.100
[2] => 10.10.10.109
[3] => 448
)
)
Try this:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
$tmp = array();
foreach($array as $val) {
$tmp[$val['name']]['values'][] = $val['value'];
}
foreach($tmp as $k => $v) {
$item = implode(',', array_unique(explode(',', implode(',',$v['values']))));
$newArr[] = array('name' => $k, 'value' => $item);
}
echo '<pre>';
print_r($newArr);
echo '</pre>';
got it with the following crazy mess:
$name = array_column($array, 'name');
$value = array_column($array, 'value');
foreach($name as $nk=>$nv)
foreach($value as $vk=>$vv)
if($nk == $vk)
$a[$nv][] = $vv;
foreach($a as $k=>$v)
$b[$k] = implode(',', $v);
$z = 0;
foreach($b as $k=>$v)
{
$c[$z]['name'] = $k;
$c[$z]['value'] = $v;
$z++;
}
$c is the resulting array
Or using a medley of array functions:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
$names = array_column($array, 'name');
$values = array_column($array, 'value');
$result = [];
foreach (array_unique($names) as $k)
$result[$k] = implode(", ", array_filter($values,
function($v, $indx) use ($names, $k) {
return $names[$indx] == $k;
}, ARRAY_FILTER_USE_BOTH));
print_r($result);
$result2 = [];
foreach ($result as $k=>$v) $result2[] = ['name'=>$k, 'value'=>$v];
print_r($result2);
Results in:
Array
(
[filter_amount] => 100-ml, 200-ml
[page_size] => 7
)
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml, 200-ml
)
[1] => Array
(
[name] => page_size
[value] => 7
)
)
All of the other answers up to now are using two or more iterating techniques for this task. There only needs to be one loop.
Build an associative output array based on the name values as you iterate. If the associative key isn't set, then save the whole row. If it is set, then just append a comma then the new value data to the stored value element.
Using temporary keys allows isset() to swiftly check for existence. It will always outperform array_search() and in_array() because of how php treats arrays (as hash maps).
Remove the temporary keys when the loop is finished by calling array_values().
Code: (Demo)
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['name']])) {
$result[$row['name']] = $row;
} else {
$result[$row['name']]['value'] .= ',' . $row['value'];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'name' => 'filter_amount',
'value' => '100-ml,200-ml',
),
1 =>
array (
'name' => 'page_size',
'value' => '7',
),
)
I'm trying to arrange an array in levels. This is my array:
Array
(
[0] => Array(
[0] => Array(
[id] => 971249312[name] => Wolverine
)
[children] => Array(
[0] => Array(
[0] => Array(
[id] => 735327624[name] => Ciclop
)
[children] => Array()
)
)
)
[1] => Array(
[0] => Array(
[id] => 1926833684[name] => Gambit
)
[children] => Array()
)
[2] => Array(
[0] => Array(
[id] => 51194629[name] => Quicksilver
)
[children] => Array()
)
)
See that the first position of array has 3 elements - this must be the level 0. The first position of these elements must be the level 1. The children of these elements are the next level and so on.
I canĀ“t figure out how to arrange it.
the expected output:
Array
(
["level_1"] => Array
(
[0] => Array
(
[id] => 971249312
[name] => Wolverine
)
[1] => Array
(
[id] => 1926833684
[name] => Gambit
)
[2] => Array
(
[id] => 51194629
[name] => Quicksilver
)
)
["level_2"] => Array
(
[0] => Array
(
[id] => 735327624
[name] => Ciclop
)
)
)
Another recursive tree walk.
I scan the tree 'depth first' so I need to keep track of the current 'level'.
Demonstration at eval.in
Tree scan routine:
/**
* Recursive scan of the tree
*
* #node array Current Node to be processed
* #level integer Current depth of the tree
* output array reference to where to store the details
*
* #return void
*/
function scanNode($node, $level, &$output)
{
$outLevelIdx = 'level_'. $level;
foreach ($node as $idx => $info) {
$parent = current($info);
$output[$outLevelIdx][] = array('id' => $parent['id'], 'name' => $parent['name']);
if (!empty($info['children'])) { // go scan the children
scanNode($info['children'], $level + 1, $output);
}
}
}
Run the scan:
/*
* Output array in here - pass as a reference
*/
$output = array();
// scan the full tree
scanNode($source, 0, $output);
Sample output:
output
Array
(
[level_0] => Array
(
[0] => Array
(
[id] => 971249312
[name] => Wolverine
)
[1] => Array
(
[id] => 1926833684
[name] => Gambit
)
[2] => Array
(
[id] => 51194629
[name] => Quicksilver
)
)
[level_1] => Array
(
[0] => Array
(
[id] => 735327624
[name] => Ciclop
)
)
)
If your desired output is
Array
(
[0] => Array
(
[id] => 971249312
[name] => Wolverine
[children] => Array
(
)
)
[1] => Array
(
[id] => 971249312
[name] => Wolverine
[children] => Array
(
)
)
[2] => Array
(
[id] => 971249312
[name] => Wolverine
[children] => Array
(
)
)
)
Then your code should be
$newArray = [];
foreach ($givenArray as $key => $value) {
$newArray[$key]['id'] = $value[0]['id'];
$newArray[$key]['name'] = $value[0]['name'];
$newArray[$key]['children'] = $value['children'];
}
AS per your desired output
This function used to scan all the node and provide as per your requirment.
OUTPUT
$newArray = [];
myfunction($a, 0,$newArray);
function myfunction($loop, $level, &$newArray) {
$index = "level_".$level;
$i = 0;
foreach ($loop as $key => $value) {
foreach ($value as $key1 => $value1) {
if($key1 !== 'children'){
$newArray[$index][$i] = ['id' => $value1['id'], 'name' => $value1['name']];
$i++;
}
}
if (isset($value['children']) && !empty($value['children'])) {
myfunction($value['children'], $level + 1, $newArray);
}
}
}
print_r($newArray);exit;
I have an array that looks like this:
$cars = array (
array(
'a' => 'audi',
'b' => 'a4'),
array(
'a' => 'peugeot',
'b' => '306'),
array(
'a' => 'audi',
'b' => 'a4'),
array(
'a' => 'audi',
'b' => 'a5'),
array(
'a' => 'peugeot',
'b' => '106'),
array(
'a' => 'peugeot',
'b' => '106'),
);
I need to order arrays like this to (id is the same as name):
name => audi
id=> audi
data => a4 => 2
a5 => 1
name => peugeot
id=> peugeot
data => 306 => 1
106 => 2
So the car brands need to be grouped an the car types counted.
I already have this code; but that is only for the group part and the count part is missing.
function mergeAndOrder($data){
// set group arrays
$i = 0; $group1 = array();
// loop trough array
$array = array(); $array2 = array();
if($data != null){
foreach($data AS $row){
// search and order level1
$search = array_search($row->a,$group1);
// this object is not found
if(is_int($search) == false){
$group1[$i] = $row->a;
$array[$i]['id'] = $row->a;
$array[$i]['name'] = $row->a;
$array[$i]['data'] = array();
$i++;
}
}
}
return $array;
}
Does somebody know an solution for this case? Thanks!
--- INPUT (part of) ---
a = lease company in this case
Array
(
[0] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[1] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[2] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[3] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
--- OUTPUT (part of) ---
Array
(
[0] => Array
(
[id] => LPN
[name] => LPN
[data] => Array
(
)
)
[1] => Array
(
[id] => ARV
[name] => ARV
[data] => Array
(
)
)
[2] => Array
(
[id] => ARVB
[name] => ARVB
[data] => Array
(
)
)
[3] => Array
(
[id] => LPD
[name] => LPD
[data] => Array
(
)
)
)
Array
(
[0] => Array
(
[id] => LPN
[name] => LPN
[data] => Array
(
)
)
[1] => Array
(
[id] => ARV
[name] => ARV
[data] => Array
(
)
)
[2] => Array
(
[id] => ARVB
[name] => ARVB
[data] => Array
(
)
)
If I understand your question correctly, this should do what you want.
function mergeAndOrder ($data) {
$output = array();
foreach ($data as $item) {
$id = $item->a;
$value = $item->b;
if (!array_key_exists($id, $output)) {
$output[$id] = array('id' => $id, 'name' => $id, 'data' => array());
}
if (!array_key_exists($value, $output[$id]['data'])) {
$output[$id]['data'][$value] = 0;
}
$output[$id]['data'][$value]++;
}
// Order by name element
uasort($output, function ($a, $b) {
return strcasecmp($a['name'], $b['name']);
});
return $output;
}
Output:
Array
(
[audi] => Array
(
[id] => audi
[name] => audi
[data] => Array
(
[a4] => 2
[a5] => 1
)
)
[peugeot] => Array
(
[id] => peugeot
[name] => peugeot
[data] => Array
(
[306] => 1
[106] => 2
)
)
)