Im having trouble iterating thru this array of product information with the desired result of echoing google ecommerce tracking code for each item. How do I reduce the dimension by one. In short -
How do turn this:
Array (
[array] => Array (
[0] => Array (
[product_id] => 7
[prod_count] => 1
[price] => 19.99
)
[1] => Array (
[product_id] => 6
[prod_count] => 3
[price] => 19.99
)
[2] => Array (
[product_id] => 5
[prod_count] => 2
[price] => 19.99
)
[3] => Array (
[product_id] => 4
[prod_count] => 4
[price] => 14.99
)
[4] => Array (
[product_id] => 3
[prod_count] => 5
[price] => 19.99
)
)
)
into this:
Array (
[0] => Array (
[product_id] => 7
[prod_count] => 1
[price] => 19.99
)
[1] => Array (
[product_id] => 6
[prod_count] => 3
[price] => 19.99
)
[2] => Array (
[product_id] => 5
[prod_count] => 2
[price] => 19.99
)
[3] => Array (
[product_id] => 4
[prod_count] => 4
[price] => 14.99
)
[4] => Array (
[product_id] => 3
[prod_count] => 5
[price] => 19.99
)
)
The obvious answer for the example would be:
$array = $array['array'];
However, assuming there are multiple arrays as level one:
$array = call_user_func_array('array_merge',$array);
$arr = array(
"withinArray" => array(
"withinMoreArray" => array(
"andEvenMoreArray" => array(
)
)
)
);
$arr = current($arr);
// OR
$arr = $arr['withinArray'];
<?php
// Let's say this is your big array:
/*Array (
[array] => Array (
[0] => Array (
[product_id] => 7
[prod_count] => 1
[price] => 19.99
)
[1] => Array (
[product_id] => 6
[prod_count] => 3
[price] => 19.99
)
[2] => Array (
[product_id] => 5
[prod_count] => 2
[price] => 19.99
)
[3] => Array (
[product_id] => 4
[prod_count] => 4
[price] => 14.99
)
[4] => Array (
[product_id] => 3
[prod_count] => 5
[price] => 19.99
)
)
)*/
$littlearray = $bigarray['array'];
?>
Related
I am using an array that I get from a database and I would like to create an associative array from that one.
Here is the code I am using:
print_r($data_sites);
// Format for easy use
$data_sites_formatted = array();
foreach ($data_sites as $key => $row) {
if (empty($data_formatted[$row->project_loe_id])) {
$data_sites_formatted[$row->project_loe_id] = array();
}
$data_sites_formatted[$row->project_loe_id][$row->name] = array();
$data_sites_formatted[$row->project_loe_id][$row->name]['id'] = $row->id;
$data_sites_formatted[$row->project_loe_id][$row->name]['quantity'] = $row->quantity;
$data_sites_formatted[$row->project_loe_id][$row->name]['loe_per_quantity'] = $row->loe_per_quantity;
}
print_r($data_sites_formatted);
Here is the information I receive from the database:
Illuminate\Support\CollectionObject ( [items:protected] => Array (
[0] => stdClass Object ( [id] => 1 [project_loe_id] => 1 [name] => site [quantity] => 20 [loe_per_quantity] => 1 )
[1] => stdClass Object ( [id] => 2 [project_loe_id] => 1 [name] => switches [quantity] => 5 [loe_per_quantity] => 3 )
[2] => stdClass Object ( [id] => 3 [project_loe_id] => 2 [name] => site [quantity] => 20 [loe_per_quantity] => 1 )
[3] => stdClass Object ( [id] => 4 [project_loe_id] => 2 [name] => ap [quantity] => 5 [loe_per_quantity] => 3 )
[4] => stdClass Object ( [id] => 5 [project_loe_id] => 2 [name] => wireless [quantity] => 5 [loe_per_quantity] => 3 ) ) )
And when I do the transformation, here is the result:
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) )
[2] => Array ( [wireless] => Array ( [id] => 5 [quantity] => 5 [loe_per_quantity] => 3 ) ) )
I lost 3 lines and I really have no idea why. I can see when I look into details step by step that it deletes some lines from 1 iteration to the next:
Iteration:0
Array (
[1] => Array ( [site] => Array ( [id] => 1 [quantity] => 20 [loe_per_quantity] => 1 ) ) )
Iteration: 1
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) ) )
Iteration: 2
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) )
[2] => Array ( [site] => Array ( [id] => 3 [quantity] => 20 [loe_per_quantity] => 1 ) ) )
Iteration: 3
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) )
[2] => Array ( [ap] => Array ( [id] => 4 [quantity] => 5 [loe_per_quantity] => 3 ) ) )
Iteration: 4
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) )
[2] => Array ( [wireless] => Array ( [id] => 5 [quantity] => 5 [loe_per_quantity] => 3 ) ) )
You can see from iteration 0 to iteration 1, it deleted the line from iteration 0 and in iteration 1 I should have 2 lines.
You have a typo here:
if (empty($data_formatted[$row->project_loe_id])) {
$data_sites_formatted[$row->project_loe_id] = array();
}
$data_formatted is never defined (you meant to write $data_sites_formatted) so empty($data_formatted[$row->project_loe_id]) is always true, and always runs the next line, replacing anything previously added to $data_sites_formatted[$row->project_loe_id] with an empty array. So all you end up with is the last item to be added in each group.
I want key rating sum common slug key value. Please help me out.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 6
[slug] => scenario
[avis_id] => 2
[rating] => 0
)
[1] => Array
(
[id] => 7
[slug] => jeu_d_acteur
[avis_id] => 2
[rating] => 9
)
[2] => Array
(
[id] => 8
[slug] => effets_speciaux
[avis_id] => 2
[rating] => 0
)
[3] => Array
(
[id] => 9
[slug] => prises_de_vues
[avis_id] => 2
[rating] => 0
)
[4] => Array
(
[id] => 10
[slug] => bande_son
[avis_id] => 2
[rating] => 6.8
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[slug] => scenario
[avis_id] => 1
[rating] => 8
)
[1] => Array
(
[id] => 2
[slug] => jeu_d_acteur
[avis_id] => 1
[rating] => 5
)
[2] => Array
(
[id] => 3
[slug] => effets_speciaux
[avis_id] => 1
[rating] => 5
)
[3] => Array
(
[id] => 4
[slug] => prises_de_vues
[avis_id] => 1
[rating] => 6
)
[4] => Array
(
[id] => 5
[slug] => bande_son
[avis_id] => 1
[rating] => 8
)
)
)
output:
Array
(
[slug] => scenario
[rating] => 8
)
[1] => Array
(
[slug] => jeu_d_acteur
[rating] => 14
)
[2] => Array
(
[slug] => effets_speciaux
[rating] => 3
)
[3] => Array
(
[slug] => prises_de_vues
[rating] => 6
)
[4] => Array
(
[slug] => bande_son
[rating] => 14.8
)
)
You can merge them by using ... splat operator and loop through to group by slug and sum rating
$merged = array_merge(...$a);
$r = [];
foreach($merged as $v){
isset($r[$v['slug']])
? ($r[$v['slug']]['rating'] += $v['rating'] )
: ($r[$v['slug']] = ['slug'=>$v['slug'],'rating'=>$v['rating']]);
}
Working example :- https://3v4l.org/odCl6
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);
Hi guys i have this array when i do print_r($p)
Array
(
[0] => Array
(
[product] => Array
(
[title] => test
[id] => 9
[created_at] => 2015-08-11 19:32:05
[isNew] =>
[type] => simple
[status] => publish
[price] => 10.00
[regular_price] => 10.00
[sale_price] => 6.00
[stock_quantity] => 19999985
[featured] => 1
[on_sale] =>
[description] =>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
)
[variations] =>
)
)
[1] => Array
(
[product] => Array
(
[title] => test222222
[id] => 97
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 1999974
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[2] => Array
(
[product] => Array
(
[title] => test222222
[id] => 98
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 199969
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[3] => Array
(
[product] => Array
(
[title] => test222222
[id] => 76
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variable
[status] => publish
[price] => 0.00
[regular_price] => 0.00
[sale_price] => 0.00
[stock_quantity] => 50000
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
)
)
[featured_src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
)
null
i get this with this function
public function test(){
global $wpdb;
global $Pproduct;
global $woocommerce;
$productIds = 9_97_98_76;
$pId = explode("_", $productIds);
foreach($pId as $productID){
$product[] = $Pproduct->get_product($productID, $fields);
$p = $product;
}
print_r($p);
how do i do a foreach loop again to get variation attributes? in given product id 9_97_98_76, product id 97 & 98 are variation products of 76.
I want to get the title of the product and variable attributes, how do i code foreach so that the result returns as the following array : test_test222222 white_test222222 black_test222222 ???
try this.. Hope you are getting the product object for the productID using the get_product() function. Then try array_push to insert all filtered product objects in 1 array.
$product = Array();
foreach($pId as $productID){
array_push($product, $Pproduct->get_product($productID, $fields));
}
now try the following
foreach($product as $temp) {
echo $temp[variations];
}
Try this will get your title
$pId = explode("_", $products);
foreach($pId as $id){
$product = $Pproduct->get_product($id, $fields);
$title = $product['product']['title'];
echo $title."</br>";
}
I have the following array:
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
[2] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 3
[category_id] => 1
[amount] => 15
[cost] => 2000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:58
)
[0] => Array
(
[total_sell] => 6
)
)
[3] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 4
[category_id] => 1
[amount] => 50
[cost] => 8000
[paid] => 0
[comment] =>
[created] => 2015-06-23 01:10:10
)
[0] => Array
(
[total_sell] => 6
)
)
)
I want to remove duplicate entry of [Import][product_id]. So my expected result is :
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
)
Would you write a function to filter this type of array and produce expected result. I have been googling for 2 days but no luck.
This is a handy one liner that should do the trick:
$unique= array_map("unserialize", array_unique(array_map("serialize", $original)));
If the underlying arrays are not identical, that won't work, in which case I think you could do:
$unique = array_intersect_key($original ,
array_unique(
array_map(function($item) {
return $item['Import']['product_id'];
}, $original)
)
);
Tested: http://sandbox.onlinephpfunctions.com/code/8aee5cbd614e0ddd1a03dfaa7e98c72fbbe7d68d
Here is a quick stable sort and reduce which runs in linearithmic time. First-encountered product Id's are kept, and entries with duplicate product Id's are ignored.
// Stable sort
sort($in);
// Reduce
$out = array_reduce($in, function(&$acc, &$item){
if($item['Import']['product_id'] !== #$acc[sizeof($acc)-1]['Import']['product_id']) {
$acc[] = $item;
}
return $acc;
}, []);
Demo: http://ideone.com/BP0eUJ
Update: Here is an even better linear-time algorithm that does the same as above using a fast "hash table" lookup. Again, the first-encountered product Id is kept and subsequent ones of the same Id are ignored.
$out = [];
$hashTable = [];
foreach($in as $item) {
$pid = $item['Import']['product_id'];
if(!isset($hashTable[$pid])) {
$out[] = $item;
$hashTable[$pid] = true;
}
}
Demo: http://ideone.com/5RF0og