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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.