Multidimensional 2d array sort - php

here is my array:
Array
(
[0] => Array
(
[product_option_id] => 1072
[option_id] => 5
[name] => Sizes
[type] => select
[option_value] => Array
(
[0] => Array
(
[product_option_value_id] => 8282
[option_value_id] => 57
[name] => 10
[price] =>
[price_prefix] => +
)
[1] => Array
(
[product_option_value_id] => 8283
[option_value_id] => 58
[name] => 11
[price] =>
[price_prefix] => +
)
[2] => Array
(
[product_option_value_id] => 8284
[option_value_id] => 59
[name] => 12
[price] =>
[price_prefix] => +
)
[3] => Array
(
[product_option_value_id] => 8285
[option_value_id] => 60
[name] => 13
[price] =>
[price_prefix] => +
)
[4] => Array
(
[product_option_value_id] => 8279
[option_value_id] => 61
[name] => 7
[price] =>
[price_prefix] => +
)
[5] => Array
(
[product_option_value_id] => 8280
[option_value_id] => 62
[name] => 8
[price] =>
[price_prefix] => +
)
[6] => Array
(
[product_option_value_id] => 8281
[option_value_id] => 63
[name] => 9
[price] =>
[price_prefix] => +
)
)
[required] => 1
)
)
is it possible to sort by [option_value][$i][name] ? so output of this should be:
Array
(
[0] => Array
(
[product_option_id] => 1072
[option_id] => 5
[name] => Sizes
[type] => select
[option_value] => Array
(
[0] => Array
(
[product_option_value_id] => 8279
[option_value_id] => 61
[name] => 7
[price] =>
[price_prefix] => +
)
[1] => Array
(
[product_option_value_id] => 8280
[option_value_id] => 62
[name] => 8
[price] =>
[price_prefix] => +
)
[2] => Array
(
[product_option_value_id] => 8281
[option_value_id] => 63
[name] => 9
[price] =>
[price_prefix] => +
)
[3] => Array
(
[product_option_value_id] => 8282
[option_value_id] => 57
[name] => 10
[price] =>
[price_prefix] => +
)
[4] => Array
(
[product_option_value_id] => 8283
[option_value_id] => 58
[name] => 11
[price] =>
[price_prefix] => +
)
[5] => Array
(
[product_option_value_id] => 8284
[option_value_id] => 59
[name] => 12
[price] =>
[price_prefix] => +
)
[6] => Array
(
[product_option_value_id] => 8285
[option_value_id] => 60
[name] => 13
[price] =>
[price_prefix] => +
)
)
[required] => 1
)
)
I lost many hours on this, if somebody could, please help me with it (tryed array_multisort but no result)

PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column()
array_multisort(array_column($array[0]['option_value'], 'name'),
SORT_ASC, $array[0]['option_value']);

Related

multi dimensional array from single array

i need make multidimensional array from a single array loop, the value of array will like color size material and others, and child will red, orange
EX:
Array
(
[0] => Array
(
[option_value_id] => 46
[option_id] => 11
[image] =>
[sort_order] => 2
[language_id] => 1
[name] => Size
[type] => select
[option_value_name] => Small
)
[1] => Array
(
[option_value_id] => 50
[option_id] => 13
[image] =>
[sort_order] => 1
[language_id] => 1
[name] => color
[type] => image
[option_value_name] => Black
)
[2] => Array
(
[option_value_id] => 48
[option_id] => 11
[image] =>
[sort_order] => 2
[language_id] => 1
[name] => Size
[type] => select
[option_value_name] => Large
)
[3] => Array
(
[option_value_id] => 50
[option_id] => 13
[image] =>
[sort_order] => 1
[language_id] => 1
[name] => color
[type] => image
[option_value_name] => Black
)
[4] => Array
(
[option_value_id] => 47
[option_id] => 11
[image] =>
[sort_order] => 2
[language_id] => 1
[name] => Size
[type] => select
[option_value_name] => Medium
)
[5] => Array
(
[option_value_id] => 50
[option_id] => 13
[image] =>
[sort_order] => 1
[language_id] => 1
[name] => color
[type] => image
[option_value_name] => Black
)
)
this is the array i get i need make to this format
Array
(
[0] => Array
(
[product_option_value] => Array
(
[0] => Array
(
[option_value_id] => 50
[name] => Black
)
)
[option_id] => 13
[name] => color
[type] => image
)
[1] => Array
(
[product_option_value] => Array
(
[0] => Array
(
[option_value_id] => 46
[name] => Small
)
[1] => Array
(
[option_value_id] => 47
[name] => Medium
)
[2] => Array
(
[option_value_id] => 48
[name] => Large
)
)
[option_id] => 11
[name] => Size
[type] => select
)
)

Looping a Complex Multidimensional Array

I'm trying to extract all the variables from a complex array of nutrition results from Neutronix API.
The array is as follows:
Food Array:
Array ( [foods] =>
Array ( [0] => Array (
[food_name] => kale
[brand_name] =>
[serving_qty] => 1
[serving_unit] => cup, chopped
[serving_weight_grams] => 130
[nf_calories] => 36.4
[nf_total_fat] => 0.52
[nf_saturated_fat] => 0.07
[nf_cholesterol] => 0
[nf_sodium] => 29.9
[nf_total_carbohydrate] => 7.32
[nf_dietary_fiber] => 2.6
[nf_sugars] => 1.63
[nf_protein] => 2.47
[nf_potassium] => 296.4
[nf_p] => 36.4
[full_nutrients] => Array (
[0] => Array (
[attr_id] => 203
[value] => 2.47
)
[1] => Array (
[attr_id] => 204
[value] => 0.52
)
[2] => Array (
[attr_id] => 205
[value] => 7.319
)
[3] => Array (
[attr_id] => 207
[value] => 1.131
)
[4] => Array (
[attr_id] => 208
[value] => 36.4
)
[5] => Array (
[attr_id] => 221
[value] => 0
)
[6] => Array (
[attr_id] => 255
[value] => 118.56
)
[7] => Array (
[attr_id] => 262
[value] => 0
)
[8] => Array (
[attr_id] => 263
[value] => 0
)
[9] => Array (
[attr_id] => 268
[value] => 152.1
)
[10] => Array (
[attr_id] => 269
[value] => 1.625
)
[11] => Array (
[attr_id] => 291
[value] => 2.6
)
[12] => Array (
[attr_id] => 301
[value] => 93.6
)
[13] => Array (
[attr_id] => 303
[value] => 1.17
)
[14] => Array (
[attr_id] => 304
[value] => 23.4
)
[15] => Array (
[attr_id] => 305
[value] => 36.4
)
[16] => Array (
[attr_id] => 306
[value] => 296.4
)
[17] => Array (
[attr_id] => 307
[value] => 29.9
)
[18] => Array (
[attr_id] => 309
[value] => 0.312
)
[19] => Array (
[attr_id] => 312
[value] => 0.2028
)
[20] => Array (
[attr_id] => 315
[value] => 0.5408
)
[21] => Array (
[attr_id] => 317
[value] => 1.17
)
)
[nix_brand_name] =>
[nix_brand_id] =>
[nix_item_name] =>
[nix_item_id] =>
[upc] =>
[consumed_at] => 2017-09-08T22:44:54+00:00
[metadata] => Array ( )
[source] => 1
[ndb_no] => 11234
[tags] => Array (
[item] => kale
[measure] =>
[quantity] => 1.0
[tag_id] => 644
)
[alt_measures] => Array (
[0] => Array (
[serving_weight] => 130
[measure] => cup, chopped
[seq] => 1
[qty] => 1 )
[1] => Array (
[serving_weight] => 130
[measure] => cup
[seq] => 80
[qty] => 1 )
[2] => Array (
[serving_weight] => 2.71
[measure] => tsp
[seq] => 101
[qty] => 1 )
[3] => Array (
[serving_weight] => 8.13
[measure] => tbsp
[seq] => 102
[qty] => 1 )
)
[lat] =>
[lng] =>
[meal_type] => 5
[photo] => Array (
[thumb] => https://d2xdmhkmkbyw75.cloudfront.net/644_thumb.jpg
[highres] => https://d2xdmhkmkbyw75.cloudfront.net/644_highres.jpg
)
[sub_recipe] =>
)
)
)
My PHP Code so far is below. I've inserted comments where I am stuck as to how to get the variables.
I'm also not sure if I'm looping the segments of the array correctly.
foreach ($foods as $food){
foreach($food as $key => $val) {
//get values for each variable
foreach($full_nutrients as $nutrient){
foreach($nutrient as $key => $val){
//get values for each variable
}
}
foreach($metadata as $meta){
$source = $meta['source'];
$ndb_no = $meta['ndb_no'];
foreach($tags as $tag){
$tag_item = $tag['item'];
$tag_measure = $tag['measure'];
$tag_quantity = $tag['quantity'];
$tag_id = $tag['tag_id'];
}
}
foreach($alt_measures as $alt_meaasure){
foreach($alt_meaasure as $key => $val){
//get alt_measures
}
}
foreach($photo as $image){
$image_thumb = $image['thumb'];
$image_highres = $image['highres'];
}
}
}

Sort Php array on the basis of salary and point

I have an players array that already sorted on the basis of points descending order.like this:
$positionArray =
Array
(
[PG] => Array
(
[0] => Array
(
[id] => 1
[player_id] => 31471
[salary] => 10800
[points] => 51.53
[position] => PG
)
[1] => Array
(
[id] => 3
[player_id] => 3223
[salary] => 8000
[points] => 49.53
[position] => PG
)
[2] => Array
(
[id] => 5
[player_id] => 4356
[salary] => 11000
[points] => 45.53
[position] => PG
)
)
[3] => Array
(
[id] => 4
[player_id] => 5654
[salary] => 6000
[points] => 42.53
[position] => PG
)
)
[SF] => Array
(
[0] => Array
(
[id] => 2
[player_id] => 4433
[salary] => 11000
[points] => 45.1
[position] => SF
)
[1] => Array
(
[id] => 6
[player_id] => 7667
[salary] => 10800
[points] => 44
[position] => SF
)
[2] => Array
(
[id] => 9
[player_id] => 4533
[salary] => 8000
[points] => 39.53
[position] => SF
)
)
[SG] => Array
(
[0] => Array
(
[id] => 8
[player_id] => 3245
[salary] => 9000
[points] => 56.53
[position] => SG
)
[1] => Array
(
[id] => 7
[player_id] => 7867
[salary] => 6000
[points] => 54.53
[position] => SG
)
[2] => Array
(
[id] => 15
[player_id] => 4543
[salary] => 7000
[points] => 53.53
[position] => SG
)
)
[2] => Array
(
[id] => 15
[player_id] => 2322
[salary] => 5000
[points] => 51.53
[position] => SG
)
)
[PF] => Array
(
[0] => Array
(
[id] => 11
[player_id] => 7777
[salary] => 4000
[points] => 60.53
[position] => PF
)
[1] => Array
(
[id] => 54
[player_id] => 1232
[salary] => 8000
[points] => 59.3
[position] => PF
)
[2] => Array
(
[id] => 18
[player_id] => 5678
[salary] => 3000
[points] => 54.45
[position] => PF
)
)
[C] => Array
(
[0] => Array
(
[id] => 23
[player_id] => 8906
[salary] => 5000
[points] => 47.53
[position] => C
)
[1] => Array
(
[id] => 22
[player_id] => 9007
[salary] => 4000
[points] => 45.53
[position] => C
)
[2] => Array
(
[id] => 43
[player_id] => 1008
[salary] => 11000
[points] => 43.53
[position] => C
)
)
)
PG,SG,SF,PF,C are the positions of the player.I want an array that contain 2 player of PG position ,2 player of SF position,2 player of SG position,2 player of SF position and only one player of C position.
like this:
Array
(
[PG_1] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => PG
)
[PG_2] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => PG
)
[SG_1] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => SG
)
[SG_2] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => SG
)
[SF_1] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => SF
)
[SF_2] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => SF
)
[PF_1] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => PF
)
[PF_2] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => PF
)
[C_1] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => c
)
)
That's mean I just want 9 player on the basis of maximum points and sum of the player salary should not exceed the limit of 60,000.
Note:Player should not be duplicate and If all the above positions are not in the array that just throw an error.It mean we need all these positions
This is what I did so far:
$lineupTemp = array(
"PG",
"PG",
"SG",
"SG",
"SF",
"SF",
"PF",
"PF",
"C"
);
$lineupCount=array_count_values($lineupTemp);
foreach ($lineupCount as $posss=>$countloop){
if(!isset($positionArray[$posss])){
break;
}
foreach ($this->positionArray[$posss] as $reco_){
for($llll=0;$llll<$countloop;$llll++){
array_push($combineLinupArray,array(
'array_id'=>$posss.'_'.($llll + 1),
'point'=>$this->positionArray[$posss][$llll]['points'],
'salary'=>$this->positionArray[$posss][$llll]['salary'],
'fantasy_id'=>$this->positionArray[$posss][$llll]['id'],
'position'=>$posss,
'key_used'=>$llll
)
);
}
break;
}
}
if(count($combineLinupArray)<9){
//return message that all the positions are not found..
}
After that I am adjusting the salary to iterate the array.
The problem is,It's taking too much time,So how can I make this array?

Adding Values them removing duplicates in a php array

I have an array that I need to process by adding the values, then removing any added duplicates.
Here's the Original Array...
Array (
[0] => Array ( [PID] => 2872 [QTY] => 1 )
[1] => Array ( [PID] => 3145 [QTY] => 2 )
[2] => Array ( [PID] => 3107 [QTY] => 1 )
[3] => Array ( [PID] => 2739 [QTY] => 1 )
[4] => Array ( [PID] => 3137 [QTY] => 1 )
[5] => Array ( [PID] => 3107 [QTY] => 1 )
[6] => Array ( [PID] => 2739 [QTY] => 1 )
[7] => Array ( [PID] => 3107 [QTY] => 1 )
[8] => Array ( [PID] => 3137 [QTY] => 4 )
[9] => Array ( [PID] => 3551 [QTY] => 1 )
[10] => Array ( [PID] => 3107 [QTY] => 1 )
[11] => Array ( [PID] => 3107 [QTY] => 1 )
[12] => Array ( [PID] => 3137 [QTY] => 1 )
[13] => Array ( [PID] => 3551 [QTY] => 2 )
[14] => Array ( [PID] => 3136 [QTY] => 1 )
[15] => Array ( [PID] => 3137 [QTY] => 1 )
[16] => Array ( [PID] => 3032 [QTY] => 1 )
[17] => Array ( [PID] => 3551 [QTY] => 1 )
[18] => Array ( [PID] => 3107 [QTY] => 1 )
[19] => Array ( [PID] => 3459 [QTY] => 1 )
[20] => Array ( [PID] => 3141 [QTY] => 1 )
[21] => Array ( [PID] => 2724 [QTY] => 1 )
[22] => Array ( [PID] => 2743 [QTY] => 1 )
[23] => Array ( [PID] => 3139 [QTY] => 2 )
[24] => Array ( [PID] => 3137 [QTY] => 2 )
[25] => Array ( [PID] => 3107 [QTY] => 1 )
)
What I need to do is take this array and Add the [QTY] values from the same [PID] values then after this end up with an array like this...
Array (
[0] => Array ( [PID] => 2724 [QTY] => 1 )
[1] => Array ( [PID] => 2739 [QTY] => 2 )
[2] => Array ( [PID] => 2743 [QTY] => 1 )
[3] => Array ( [PID] => 2872 [QTY] => 1 )
[4] => Array ( [PID] => 3032 [QTY] => 1 )
[5] => Array ( [PID] => 3107 [QTY] => 7 )
[6] => Array ( [PID] => 3136 [QTY] => 1 )
[7] => Array ( [PID] => 3137 [QTY] => 9 )
[8] => Array ( [PID] => 3139 [QTY] => 2 )
[9] => Array ( [PID] => 3141 [QTY] => 1 )
[10] => Array ( [PID] => 3145 [QTY] => 2 )
[11] => Array ( [PID] => 3459 [QTY] => 1 )
[12] => Array ( [PID] => 3551 [QTY] => 4 )
)
So add all the QTY then remove the duplicates.
I'm not quite sure the best way to proceed here. Any suggestions?
Loop over them, make a new array with PID as key and QTY as value so you can add the latter up.
foreach ($array as $row) {
list($pid, $qty) = $row;
$sums[$pid] += $qty;
}
Obviously you could throw an isset in to suppress the notices.
And if you want your former array structure, convert it once more.
Id just use a simple loop:
$combined = array();
// $org will be the original structure
foreach($org as $id => $data) {
$pid = $data['PID'];
if(!isset($combined[$pid])) {
$combined[$pid] = $data;
} else {
$combined[$pid]['QTY'] += $data['QTY'];
}
}
// return keys to normal indexs instead of the PID
array_values($combined);

Removing an array result

[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]);

Categories