I have this array:
Array (
[0] => stdClass Object ( [id] => 123 [name] => Alex )
[1] => stdClass Object ( [id] => 124 [name] => John )
[2] => stdClass Object ( [id] => 123 [name] => Alex )
[3] => stdClass Object ( [id] => 124 [name] => John)
[4] => stdClass Object ( [id] => 126 [name] => Paul )
)
And I want to output like the following:
Array (
[0] => Array ( [id] => 123 [name] => Alex [count] = 2 )
[1] => Array ( [id] => 124 [name] => John [count] = 2 )
[2] => Array ( [id] => 126 [name] => Paul [count] = 1 )
)
I tried using array_count_values($array), but it doesn't work.
Any ideas how to solve this?
You can simply use array_map along with simple foreach like as
foreach(array_map("get_object_vars",$arr) as $val){
$hash = $val['id'];
if(isset($result[$hash])){
$result[$hash]['count'] += $result[$hash]['count'];
}else{
$result[$hash] = $val;
$result[$hash]['count'] = 1;
}
}
print_r(array_values($result));
Output:
Array
(
[0] => Array
(
[id] => 123
[name] => Alex
[count] => 2
)
[1] => Array
(
[id] => 124
[name] => John
[count] => 2
)
[2] => Array
(
[id] => 126
[name] => Paul
[count] => 1
)
)
Related
I have product options that came from database and I want to merge all the values by same product_id .
I'm using a foreachloop.
stdClass Object(
[id] => 22
[product_id] => 48
[values] => Array (
[0] => stdClass Object (
[id] => 81
[option_id] => 22
[name] => Black
[value] => black
)
)
),
stdClass Object(
[id] => 10
[product_id] => 50
[values] => Array (
[0] => stdClass Object (
[id] => 33
[option_id] => 10
[name] => L
[value] => l
),
[1] => stdClass Object (
[id] => 34
[option_id] => 10
[name] => M
[value] => m
)
)
),
stdClass Object(
[id] => 24
[product_id] => 48
[values] => Array (
[0] => stdClass Object (
[id] => 98
[option_id] => 11
[name] => X
[value] => x
),
[1] => stdClass Object (
[id] => 99
[option_id] => 11
[name] => XL
[value] => xl
),
[2] => stdClass Object (
[id] => 100
[option_id] => 11
[name] => XLL
[value] => xll
)
)
)
I want to combine these array by same product_id and get output like :
stdClass Object(
[id] => 22
[product_id] => 48
[values] => Array (
[0] => stdClass Object (
[id] => 81
[option_id] => 22
[name] => Black
[value] => black
),
[1] => stdClass Object (
[id] => 98
[option_id] => 11
[name] => X
[value] => x
)
[2] => stdClass Object (
[id] => 99
[option_id] => 11
[name] => XL
[value] => xl
),
[3] => stdClass Object (
[id] => 100
[option_id] => 11
[name] => XLL
[value] => xll
)
),
stdClass Object(
[id] => 10
[product_id] => 50
[values] => Array (
[0] => stdClass Object (
[id] => 33
[option_id] => 10
[name] => L
[value] => l
),
[1] => stdClass Object (
[id] => 34
[option_id] => 10
[name] => M
[value] => m
)
)
)
I'm using foreach loop , How to do this inside foreach loop ?
I want to merge values of same product_id. What is the best way to do this ?
My code is:
foreach($data['products'] as $pro){
foreach($pro->options as $opt){
debug($opt);
}
}
You can try something like this :
1/ Your data
$array = /* array with all your product obj */;
// Create your new result array
$result = array();
2/ The foreach loop
// Loop throught all your object
foreach ($array as $obj) {
// Your current product id
$current_product_id = $obj->product_id;
// The values of this product
$current_product_values = $obj->values;
// Loop through all the value of the product
foreach ($current_product_values as $value) {
// Add those value to your result array for the current product id
$result[$current_product_id][] = $value;
}
}
Is it what you are looking for?
Hii I need help in grouping array where i have this sort of array :
Array
(
[0] => Array
(
[name] => rose
[price] => 1
)
[1] => Array
(
[name] => daisy
[price] => 3
)
[2] => Array
(
[name] => orchid
[price] => 1
)
[3] => Array
(
[name] => rose
[price] => 2
)
[4] => Array
(
[name] => daisy
[price] => 3
)
[5] => Array
(
[name] => orchid
[price] => 1
)
[6] => Array
(
[name] => rose
[price] => 2
)
[7] => Array
(
[name] => daisy
[price] => 3
)
[8] => Array
(
[name] => orchid
[price] => 2
)
)
and i want it to be like :
Array
(
[0] => Array
(
[0] => Array
(
[name] => rose
[price] => 1
)
[1] => Array
(
[name] => daisy
[price] => 1
)
[2] => Array
(
[name] => orchid
[price] => 1
)
)
[1] => Array
(
[0] => Array
(
[name] => rose
[price] => 2
)
[1] => Array
(
[name] => daisy
[price] => 2
)
[2] => Array
(
[name] => orchid
[price] => 2
)
)
[2] => Array
(
[0] => Array
(
[name] => rose
[price] => 3
)
[1] => Array
(
[name] => daisy
[price] => 3
)
[2] => Array
(
[name] => orchid
[price] => 3
)
)
)
I mean want to group them where same "price" value occurs . You can better understand them from given arrays .
$group = array();
foreach ( $array as $value ) {
$group[$value['price']][] = $value;
}
var_dump($group);
Update: I have a solution - please see below for details.
I have an array where the keys are levels (in a navigation tree for example) - something like
Array
(
[0] => Array
(
[100] => Array
(
[name] => foo100
[slug] => foo100
[id] => 100
[parent] => 0
[level] => 0
)
[101] => Array
(
[name] => foo101
[slug] => foo101
[id] => 101
[parent] => 0
[level] => 0
)
)
[1] => Array
(
[200] => Array
(
[name] => foo200
[slug] => foo200
[id] => 200
[parent] => 100
[level] => 1
)
[201] => Array
(
[name] => foo201
[slug] => foo201
[id] => 201
[parent] => 101
[level] => 1
)
)
[2] => Array
(
[300] => Array
(
[name] => foo300
[slug] => foo300
[id] => 300
[parent] => 200
[level] => 2
)
[301] => Array
(
[name] => foo301
[slug] => foo301
[id] => 301
[parent] => 201
[level] => 2
)
)
[3] => Array
(
[400] => Array
(
[name] => foo400
[slug] => foo400
[id] => 400
[parent] => 300
[level] => 3
)
)
[4] => Array
(
[500] => Array
(
[name] => foo500
[slug] => foo500
[id] => 500
[parent] => 400
[level] => 4
)
)
)
I need to create an array from this which iterates from the top most level and creates an array with the key being the slug of that level - to produce the following:
Array
(
[foo500] => Array
(
[4] => Array
(
[name] => foo500
)
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo400] => Array
(
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo300] => Array
(
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo301] => Array
(
[2] => Array
(
[name] => foo301
)
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo200] => Array
(
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo201] => Array
(
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo100] => Array
(
[0] => Array
(
[name] => foo100
)
)
[foo101] => Array
(
[0] => Array
(
[name] => foo101
)
)
)
I hope this explains the issue - struggling to get this right! Any help much appreciated!
Update - solution.
For this I removed the first level of keys to leave
Array
(
[107] => Array
(
[id] => 107
[name] => About Us
[indexID] => about
[level] => 0
[parent] => 0
)
[109] => Array
(
[id] => 109
[name] => Home
[indexID] => index
[level] => 0
[parent] => 0
)
}
etc etc
Assuming $data is the above array I went with:
foreach ($data as $k => $v) {
if ($v['parent'] == 0) {
$bc[$v['indexID']][0]['name'] = $v['name'];
$bc[$v['indexID']][0]['indexID'] = $v['indexID'];
}
else {
$nextParent = $v['parent'];
$currentIndexID = $v['indexID'];
$currentName = $v['name'];
$bc[$v['indexID']][0]['name'] = $currentName;
$bc[$v['indexID']][0]['indexID'] = $currentIndexID;
for($i=1;$i<=$level;$i++) {
foreach ($data as $a => $b) {
if ($a == $nextParent) {
$nextParent = $b['parent'];
$bc[$v['indexID']][$i]['name'] = $b['name'];
$bc[$v['indexID']][$i]['indexID'] = $b['indexID'];
}
}
}
}
}
I get this array in response from webservice, so How do I read it in a foreach cycle? or some easy way to read it. The [group] they are more than 12 [id]
there is.
Array
( [response] => Array (
[single] => Array ( [parameters] => Array ( [0] => Array ( [name] => msgCode [value] => 0101 )
[1] => Array ( [name] => msgDesc [value] => OK )
[2] => Array ( [name] => status [value] => 1)
[3] => Array ( [name] => message [value] => Normal) ) )
[group] => Array ( [id] => N4BD767 [parameters] => Array ( [0] => Array ( [name] => idFee [value] => 000 ) echo
[1] => Array ( [name] => typeFee [value] => Cuota)
[2] => Array ( [name] => entryDate [value] => 2014-12-17T14:06:47-03:00 )
[3] => Array ( [name] => expirationDate [value] => 2015-12-05T00:00:00-03:00)
[4] => Array ( [name] => amountOrigin [value] => 221980)
[5] => Array ( [name] => surcharges [value] => 1856)
[6] => Array ( [name] => entity [value] => ONLINE)
[7] => Array ( [name] => feeStatus [value] => inicial )
[8] => Array ( [name] => tranNumber [value] => 27) ) ) ) )
Okay guys, this is what I did and it work fine. With no foreach.
$output = array();
array_walk_recursive($result, function($item,$key) use (&$output){
array_push($output,$key,$item);
});
echo var_dump($output);
I have one array in php like this :
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] => People
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] => People
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] => Facility
)
)
I want to replace the duplicate type with blank. but the first one should have that type name and others should have blank. so the result array should be like this :
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] =>
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] =>
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] =>
)
)
I want array in this format only. and i am using PHP zend.
I search for this but most of them showing to remove that element from array. but i don't want to remove it. i want to replace it with blank but want to show the first one.
Tried Code
$result = array();
$result1 = array();
$result2 = array();
$y = array();
$y1 = array();
foreach ($data as $entry) {
$type= $entry["type"];
if (!isset($y[$type])) {
$y[$type] = array();
unset($entry["type"]);
$result[$type][] = $entry;
}
}
can anyone tell me how to do that ?
Code
$i = 0;
$found = 0;
foreach($data as $key=>&$val) {
if($i != 0) {
if($data[$i]['Type'] == $data[$found]['Type']) {
$data[$i]['Type'] = "";
}
else {
$found = $i;
}
}
$i++;
}
echo "<pre>";
print_r($data);
Output
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] =>
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] =>
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] =>
)
)
Example