Good day. It is necessary to sort the associative array by key. In particular, an array with the necessary data is formed in my store. It is necessary to sort them by product categories so that the same categories go side by side and when added, after the same category, they get up side by side. I hope clearly written). While I can not sort myself, I read the documentation. Who knows, tell me, please.
- Sort products with the same category. In the array in which all data on the product is written, it is most likely that it is useless to sort it. I have not yet figured out how to arrange the condition correctly. Already, when the goods are displayed, if the category is one (the field will be equal with the same category), they must be displayed in order. Is it possible to go through the whole array before displaying it, or it is necessary to form a new sorted array. It is not clear how this will work after adding a new product to the cart. - SnowMan September
|
3 answers
usort () - Sort an array by values using a user-defined comparison function
$array = array( array('id' => 1, 'category_name' => 'category_2'), array('id' => 2, 'category_name' => 'category_3'), array('id' => 3, 'category_name' => 'category_1') ); usort($array, function($l, $r) { return strcmp($l["category_name"], $r["category_name"]); }); var_dump($array);
- I went to dinner. your example inserted and replaced with your own array, anyway the error Parse error: syntax error, unexpected T_FUNCTION in Z: \ home \ testpzbf \ www \ components \ com_virtuemart \ themes \ default \ templates \ basket \ basket_b2c.html.php on line 29 Apparently, the function should be declared earlier and in usort ($ array, "function_name") probably should be so - SnowMan
- function sort_cat ($ l, $ r) {return strcmp ($ l ["category_name"], $ r ["category_name"]);} usort ($ product_rows, "sort_cat"); that's what happened in the end - SnowMan
|
ksort !
Added by
$ar = array( 'v' => 'aaa', 'd' => 'bbb', 'b' => 'vvv', 'a' => 'ddd', ); ksort($ar); print_r($ar);
So do?
- Does not sort so. I tried all the options in the directory. Even if I specify so
ksort($array, $element)
. - SnowMan September - Yes. I get the data from the database into the array, then it is displayed by keys in the template. So I think how to sort it by a specific key. this is part of what print_r outputs Array ([0] => Array ([row_color] => sectiontableentry1 [product_name] => Boom [product_attributes] => [product_sku] => BM-SND-502 [product_thumb_image] => resized / Boom_4e4376dacd1a9_100x11 .jpg [product_packaging] => 0 [category_name] => Notebooks [product_price] => 45.41 rub. [subtotal] => 45.41 rub. [subtotal_with_tax] => 45.41 rub. - SnowMan
- You need to sort by this field
[category_name] => Тетради общие 96 листов
- SnowMan - Then make the print_r array and see if the keys are sorted. - ling
- what print_r prints is written above and the key as it is written above. I tried ksort ($ product_rows, $ category_name) - 0 results. You just need to output products in order of their categories. if they are the same. then just order them. Although maybe I don’t think correctly about sorting. maybe something else needs to be sorted, not the array itself - SnowMan
|
And "order by category_name
asc" to add in the query to the database?
- I also thought about this, but this is a VM and in order not to dig into a pile of files, save time). Therefore, maybe someone knows a way to just sort already when outputting an array of data. There, a separate array is formed with data, and a sample of the sorted base will give the same result. It seems to me. - SnowMan September
- administrator \ components \ com_virtuemart \ classes \ ps_product_category.php function get_child_list ($ category_id) $ q. = "ORDER BY #__ {vm} _category.list_order, #__ {vm} _category.category_name ASC"; try replacing with $ q. = "ORDER BY #__ {vm} _category.category_name ASC"; This is the case if you need to display the subcategories with a standard output - Anatoly Sokolov
- I do not need subcategories) I added a general category output for each product. $ product_rows [$ i] ['category_name'] = $ ps_product_category-> get_field ($ cart [$ i] ["category_id"], "category_name"); accordingly, an array is formed for each product. then it runs in a loop <? php foreach ($ product_rows as $ product) {?> then the output of data for each product from the array is further. do not need to sort the array itself, but already output the data) - SnowMan
|