I have an array which contains sub-elements that have several occurrences of the same element with different quantity:
Example array:
Array
(
[7] => Array
(
[id] => 11399
[groupid] => 18
[name] => Morphite
[quantity] => 150
)
[8] => Array
(
[id] => 16673
[groupid] => 429
[name] => Fernite Carbide
[quantity] => 44
)
[9] => Array
(
[id] => 16678
[groupid] => 429
[name] => Sylramic Fibers
[quantity] => 11
)
[10] => Array
(
[id] => 16673
[groupid] => 429
[name] => Fernite Carbide
[quantity] => 17
)
[11] => Array
(
[id] => 16680
[groupid] => 429
[name] => Phenolic Composites
[quantity] => 6
)
[12] => Array
(
[id] => 33361
[groupid] => 429
[name] => Plasmonic Matamaterials
[quantity] => 2
)
[13] => Array
(
[id] => 16681
[groupid] => 429
[name] => Nanotransistors
[quantity] => 2
)
[14] => Array
(
[id] => 16673
[groupid] => 429
[name] => Fernite Carbide
[quantity] => 22
)
)
What I want to do, is add all the elements with the same name / id together so that I'll be able to display one line with the right quantity.
Pretty simple to do with a loop.
//create new array to hold new array format
$new_array = [];
//loop through old array
foreach($old_array as $value) {
//check if $value['id'] key exists in `$new_array`
if(!array_key_exists($value['id'], $new_array)) {
//if key does not exist, create it
$new_array[$value['id']] = $value;
} else {
//if key does already exist, increment quantity
$new_array[$value['id']]['quantity'] += $value['quantity'];
}
}
Output from your example array:
Array
(
[11399] => Array
(
[id] => 11399
[groupid] => 18
[name] => Morphite
[quantity] => 150
)
[16673] => Array
(
[id] => 16673
[groupid] => 429
[name] => Fernite Carbide
[quantity] => 83
)
[16678] => Array
(
[id] => 16678
[groupid] => 429
[name] => Sylramic Fibers
[quantity] => 11
)
[16680] => Array
(
[id] => 16680
[groupid] => 429
[name] => Phenolic Composites
[quantity] => 6
)
[33361] => Array
(
[id] => 33361
[groupid] => 429
[name] => Plasmonic Matamaterials
[quantity] => 2
)
[16681] => Array
(
[id] => 16681
[groupid] => 429
[name] => Nanotransistors
[quantity] => 2
)
)
If you want the keys in this output array to be sequential numbers for some reason, you can achieve that by using array_values($new_array) after the loop.
Related
I have an array, which has been created from getting the pages from user levels. When I use print_r to print out the array you can see that there are arrays nested within arrays.
I am trying to access the 'ID' and 'name' values, which exists in each nested array. I have tried using a foreach and for loop, but have had no luck. Any help would be greatly appreciated.
Array ( [0] => Array ( [0] => Array ( [ID] => 2487 [name] => Student Analytics [_more_] => /levels/1531756603/pages/2487 )
[1] => Array ( [ID] => 1048 [name] => Member Profile [_more_] => /levels/1531756603/pages/1048 )
[2] => Array ( [ID] => 864 [name] => i.4 Course Practicum – Your Live Campaign [_more_] => /levels/1531756603/pages/864 )
[3] => Array ( [ID] => 817 [name] => i.5 Course Expectations – Contact Us If Needed [_more_] => /levels/1531756603/pages/817 )
[4] => Array ( [ID] => 815 [name] => i.3 Course Assessments – Why There Are Level Assessments [_more_] => /levels/1531756603/pages/815 )
[5] => Array ( [ID] => 813 [name] => i.2 Course Structure – How to Navigate the Web Platform [_more_] => /levels/1531756603/pages/813 )
[6] => Array ( [ID] => 807 [name] => Introduction to the Course [_more_] => /levels/1531756603/pages/807 )
[7] => Array ( [ID] => 659 [name] => INTRO LEVEL ASSESSMENT [_more_] => /levels/1531756603/pages/659 )
[8] => Array ( [ID] => 378 [name] => How This Course Will be Taught [_more_] => /levels/1531756603/pages/378 )
[9] => Array ( [ID] => 376 [name] => SCourse [_more_] => /levels/1531756603/pages/376 )
[10] => Array ( [ID] => 372 [name] => Dashboard [_more_] => /levels/1531756603/pages/372 ) )
[1] => Array ( [0] => Array ( [ID] => 2372 [name] => Profile Assignment [_more_] => /levels/1531866216/pages/2372 )
[1] => Array ( [ID] => 2110 [name] => Instructions [_more_] => /levels/1531866216/pages/2110 )
[2] => Array ( [ID] => 1619 [name] => Practicum [_more_] => /levels/1531866216/pages/1619 )
[3] => Array ( [ID] => 1048 [name] => Member Profile [_more_] => /levels/1531866216/pages/1048 )
[4] => Array ( [ID] => 571 [name] => LEVEL 1 [_more_] => /levels/1531866216/pages/571 )
[5] => Array ( [ID] => 506 [name] => 2.A ASSIGNMENTS [_more_] => /levels/1531866216/pages/506 )
[6] => Array ( [ID] => 504 [name] => 2.7 SUMMARY AND REVIEW [_more_] => /levels/1531866216/pages/504 )
[7] => Array ( [ID] => 502 [name] => 2.6 Your Brand is the Balancing Force [_more_] => /levels/1531866216/pages/502 )
[8] => Array ( [ID] => 497 [name] => 2.5 Brand Trust [_more_] => /levels/1531866216/pages/497 )
[9] => Array ( [ID] => 494 [name] => 2.4 Brand Beliefs [_more_] => /levels/1531866216/pages/494 )
[10] => Array ( [ID] => 491 [name] => 2.3 Brand Promise [_more_] => /levels/1531866216/pages/491 )
[11] => Array ( [ID] => 487 [name] => 2.2 Brand Story [_more_] => /levels/1531866216/pages/487 )
[12] => Array ( [ID] => 484 [name] => 2.1 Branding [_more_] => /levels/1531866216/pages/484 )
[13] => Array ( [ID] => 478 [name] => CHAPTER 2 CREATING THE BRAND HUB: FOUR KEY BRAND ELEMENTS [_more_] => /levels/1531866216/pages/478 )
[14] => Array ( [ID] => 473 [name] => LEVEL 1 ASSESSMENT [_more_] => /levels/1531866216/pages/473 )
[15] => Array ( [ID] => 464 [name] => 2.A-1 Brand Strategy Assignment [_more_] => /levels/1531866216/pages/464 )
[16] => Array ( [ID] => 461 [name] => 2.A-4 Student Instructions [_more_] => /levels/1531866216/pages/461 )
[17] => Array ( [ID] => 459 [name] => 2.A-3 Project Submission Form [_more_] => /levels/1531866216/pages/459 )
[18] => Array ( [ID] => 448 [name] => 1.A PRACTICUM ASSIGNMENTS [_more_] => /levels/1531866216/pages/448 )
[19] => Array ( [ID] => 445 [name] => 1.A-2 Topical Brainstorm Assignment [_more_] => /levels/1531866216/pages/445 )
[20] => Array ( [ID] => 432 [name] => 1.5 SUMMARY AND REVIEW [_more_] => /levels/1531866216/pages/432 )
[21] => Array ( [ID] => 427 [name] => 1.4 A New Inbound Model [_more_] => /levels/1531866216/pages/427 )
[22] => Array ( [ID] => 423 [name] => 1.3 The New Social Era of Marketing [_more_] => /levels/1531866216/pages/423 )
[23] => Array ( [ID] => 410 [name] => 1.2 Traditional Outbound Marketing vs. Digital Inbound Marketing [_more_] => /levels/1531866216/pages/410 )
[24] => Array ( [ID] => 394 [name] => 1.1 Consumer Acquisition from the Digital Landscape [_more_] => /levels/1531866216/pages/394 )
[25] => Array ( [ID] => 392 [name] => CHAPTER 1 INTRODUCING [_more_] => /levels/1531866216/pages/392 )
[26] => Array ( [ID] => 318 [name] => Project Submission Thank You [_more_] => /levels/1531866216/pages/318 ) ) )
Loop through the array like so.
foreach($pages_array_name['0'] as $index => $sub_array) {
echo "index is " . $index . "\n<br/>";
echo "name is " . $sub_array['name'] . "\n<br/>";
echo "_more_ is " . $sub_array['_more_'] . "\n<br/>";
echo "\n<br/>"; // extra space
}
I have an input category array with childs as follows
I want to convert it using a recursive function to another array like
OUTPUT NEEDED
['1'=>'fashion', '10' => 'fashion > women','23'=> 'fashion > women > clothing','29'=> 'fashion > women > clothing > dresses' ... and so on ]
My purpose is to use output array to populate select box options to adda category.
INPUT ARRAY
Array
(
[1] => Array
(
[id] => 1
[name] => fashion
[parent_id] => 0
[childs] => Array
(
[10] => Array
(
[id] => 10
[name] => women
[parent_id] => 1
[childs] => Array
(
[23] => Array
(
[id] => 23
[name] => clothing
[parent_id] => 10
[childs] => Array
(
[29] => Array
(
[id] => 29
[name] => dresses
[parent_id] => 23
[childs] => Array
(
)
)
[30] => Array
(
[id] => 30
[name] => jumpsuits
[parent_id] => 23
[childs] => Array
(
)
)
)
)
[24] => Array
(
[id] => 24
[name] => bags & accessories
[parent_id] => 10
[childs] => Array
(
)
)
[25] => Array
(
[id] => 25
[name] => shoes
[parent_id] => 10
[childs] => Array
(
)
)
[26] => Array
(
[id] => 26
[name] => watches
[parent_id] => 10
[childs] => Array
(
)
)
[27] => Array
(
[id] => 27
[name] => jewelery
[parent_id] => 10
[childs] => Array
(
)
)
[28] => Array
(
[id] => 28
[name] => eye-wear
[parent_id] => 10
[childs] => Array
(
)
)
)
)
[11] => Array
(
[id] => 11
[name] => men
[parent_id] => 1
[childs] => Array
(
)
)
[12] => Array
(
[id] => 12
[name] => kids
[parent_id] => 1
[childs] => Array
(
)
)
[13] => Array
(
[id] => 13
[name] => sports
[parent_id] => 1
[childs] => Array
(
)
)
[14] => Array
(
[id] => 14
[name] => bags
[parent_id] => 1
[childs] => Array
(
)
)
[15] => Array
(
[id] => 15
[name] => eyewear
[parent_id] => 1
[childs] => Array
(
)
)
[16] => Array
(
[id] => 16
[name] => watches & jewelery
[parent_id] => 1
[childs] => Array
(
)
)
)
)
[2] => Array
(
[id] => 2
[name] => supermarket
[parent_id] => 0
[childs] => Array
(
[17] => Array
(
[id] => 17
[name] => food & beverages
[parent_id] => 2
[childs] => Array
(
[31] => Array
(
[id] => 31
[name] => breakfast
[parent_id] => 17
[childs] => Array
(
)
)
[32] => Array
(
[id] => 32
[name] => snacks
[parent_id] => 17
[childs] => Array
(
)
)
)
)
[18] => Array
(
[id] => 18
[name] => dairy products
[parent_id] => 2
[childs] => Array
(
)
)
[19] => Array
(
[id] => 19
[name] => beauty
[parent_id] => 2
[childs] => Array
(
)
)
[20] => Array
(
[id] => 20
[name] => homecare
[parent_id] => 2
[childs] => Array
(
)
)
[21] => Array
(
[id] => 21
[name] => baby world
[parent_id] => 2
[childs] => Array
(
)
)
[22] => Array
(
[id] => 22
[name] => pet world
[parent_id] => 2
[childs] => Array
(
)
)
)
)
[3] => Array
(
[id] => 3
[name] => electronics
[parent_id] => 0
[childs] => Array
(
[33] => Array
(
[id] => 33
[name] => laptops
[parent_id] => 3
[childs] => Array
(
)
)
[34] => Array
(
[id] => 34
[name] => television
[parent_id] => 3
[childs] => Array
(
)
)
)
)
[4] => Array
(
[id] => 4
[name] => mobiles & tablets
[parent_id] => 0
[childs] => Array
(
[35] => Array
(
[id] => 35
[name] => mobiles
[parent_id] => 4
[childs] => Array
(
)
)
[36] => Array
(
[id] => 36
[name] => tablets
[parent_id] => 4
[childs] => Array
(
)
)
)
)
[5] => Array
(
[id] => 5
[name] => baby & toys
[parent_id] => 0
[childs] => Array
(
)
)
[6] => Array
(
[id] => 6
[name] => home
[parent_id] => 0
[childs] => Array
(
)
)
[7] => Array
(
[id] => 7
[name] => perfumes & beauty
[parent_id] => 0
[childs] => Array
(
)
)
[8] => Array
(
[id] => 8
[name] => sports & fitness
[parent_id] => 0
[childs] => Array
(
)
)
[9] => Array
(
[id] => 9
[name] => automotive
[parent_id] => 0
[childs] => Array
(
)
)
)
I need an output array with key,the category id
You can create array by this way:
$array = array(
'category_1' => 'value',
'category_2' => array(
'subcategory_1' => 'value',
'subcategory_2' => 'value',
),
);
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 a long array from which I wish to fetch all the values and store it in a separate variable, and store each value in database.
The array that I have is:
Array
(
[success] => 1
[categories] => Array
(
[0] => Array
(
[category_id] => 39
[name] => BAGS
[categories] => Array
(
[0] => Array
(
[category_id] => 59
[name] => Handcrafted Purses
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 45
[parent_id] => 39
[name] => Laptop Bag
[categories] =>
[status] => 1
)
)
[status] => 1
)
[1] => Array
(
[category_id] => 40
[name] => BOXERS
[categories] => Array
(
[0] => Array
(
[category_id] => 56
[parent_id] => 40
[name] => Women Boxers
[status] => 1
)
)
[status] => 1
)
[2] => Array
(
[category_id] => 91
[parent_id] => 0
[name] => Business Corporate
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 92
[parent_id] => 91
[name] => Bags
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 93
[parent_id] => 92
[name] => Potli Bags
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
[status] => 1
)
[3] => Array
(
[category_id] => 60
[parent_id] => 0
[name] => Business Corporates
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 90
[parent_id] => 60
[name] => Art Cushions
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 67
[parent_id] => 60
[name] => Bags
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 77
[parent_id] => 67
[name] => Potli Bags
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 76
[parent_id] => 67
[name] => Smart Bags
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[2] => Array
(
[category_id] => 86
[parent_id] => 60
[name] => Fashion Jewellery
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 88
[parent_id] => 86
[name] => Coming Soon - Products Uploading
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[3] => Array
(
[category_id] => 61
[parent_id] => 60
[name] => Men Footwear
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 65
[parent_id] => 61
[name] => Canvas Loafers
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[4] => Array
(
[category_id] => 87
[parent_id] => 60
[name] => Shawls And Stoles
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 89
[parent_id] => 87
[name] => Coming Soon - Products Uploading
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
[status] => 1
)
[4] => Array
(
[category_id] => 15
[parent_id] => 0
[name] => ETHNIC WEAR
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 28
[parent_id] => 15
[name] => Designer Lehngas
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 2
[parent_id] => 15
[name] => Suits
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
)
I am able to fetch the outer values of this array by using this code:
if (!empty($array))
{
foreach ($array['categories'] as $category)
{
echo $category['category_id'];
echo "<br>";
}
}
I got values as:
39
40
91
60
15
16
38
57
But I also wish to access the inner most values of the array. Can anyone tell how I can create a nested loop?
If you now how deep your array is you can just simply add other foreach-loops within your main loop.
if (!empty($array))
{
foreach ($array['categories'] as $category)
{
echo $category['category_id'];
echo "<br>";
if(isset($category['categories'])){
foreach($category['categories'] as $category2)
{
echo $category2['category_id'];
echo "<br>";
if (isset($category2['categories'])){
foreach($category2['categories'] as $category3)
{
echo $category3['category_id'];
echo "<br>";
...
} }
}}
}
}
[5] => Array
(
[id] => 29372
[product_id] => Array
(
[0] => stdClass Object
(
[id] => 1469
[type_id] => 1
[title] => Hearth 2 Hearth
[cover] => 21cf9d7d09d403251ba5d01ff33cd089.jpg
[coverid] => 1178
[inserted_by] => 0
[inserted_date] => 2011-02-11 13:55:23
[status_id] => 0
)
)
[variable_id] => 9
[variable_value] => 2011-02-11
[master_value] =>
[released_date] => 2011-02-11
[price] => Array
(
[0] => Array
(
[product_id] => 1469
[media_format] => VCD
[price] => 29000
[discount] => 5
)
)
[media_format] => VCD
)
[6] => Array
(
[id] => 30074
[product_id] => Array
(
[0] => stdClass Object
(
[id] => 1470
[type_id] => 1
[title] => Hearth 2 Hearth
[cover] => 149ddd4d1d5e567c1300d4831323e1c5.jpg
[coverid] => 1177
[inserted_by] => 6
[inserted_date] => 2011-02-16 15:18:58
[status_id] => 0
)
)
[variable_id] => 9
[variable_value] => 2011-02-11
[master_value] =>
[released_date] => 2011-02-11
[price] => Array
(
[0] => Array
(
[product_id] => 1470
[media_format] => DVD
[price] => 39000
[discount] => 0
)
)
[media_format] => DVD
)
I want the result by media_format = DVD so the whole array is gone, is there anyway to delete an array or remove it?
The same way you "delete" any variable:
unset($array[$index]);