Jump to content

[SOLVED] Array Problems


berridgeab

Recommended Posts

Hi

 

I got a problem, I have two arrays: -

 

$PartsArray = array(806890591, 806890386, 806890578); //Array One with some example values (Part numbers)
$QtyArray = array(10, 12, 2);               //Array two with some example Quantities (Quantites of Part numbers)

 

In one array I have Part numbers/ product codes. In the other array there are the quantites of each part number. They are both filled Incremently so: -

 

806890591 = Qty 10

 

or in other words

 

$PartsArray[0] relates to $QtyArray[0]

$PartsArray[1] relates to $QtyArray[1]

 

etc etc etc

 

What I need to do is put the Data within $QtyArray in Highest Order, i.e so it reads: -

 

12

10

2

 

However, when outputting, I also need to maintain which Qty relates to which part number. If I ran a simple sort on the $QtyArray, it would result in

 

12 = 806890591

10 = 806890386

2  = 806890578

 

Obviously, this messes up the way the part numbers relate to the Qty, is there any way of me Sorting the $QtyArray whilst still retaining a relationship with the $PartsArray?

 

Basically I want PHP to output the page like this: -

 

12 = 806890386

10 = 806890591

2  = 806890578

 

This will be done with 25 'top' selling parts once I have it finished, I just can't figure out how to list it the way I want. I feel it's something to do with the Array function

 

asort();

 

but not too sure on how to implement it. Any ideas?

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/105732-solved-array-problems/
Share on other sites

I believe you want to do something like this:

<?php
$PartsArray = array(806890591, 806890386, 806890578); //Array One with some example values (Part numbers)
$QtyArray = array(10, 12, 2);   
$new = array_combine($QtyArray,$PartsArray);
echo '<pre>' . print_r($new,true) . '</pre>';
krsort($new);
echo '<pre>' . print_r($new,true) . '</pre>';
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.