Jump to content

Recommended Posts

So I need some help.  I know how to create arrays and parse them in PHP (former C/C++/VB programmer, so PHP is easy to figure out), but here's my problem.

I have an application where I need to have a 2D array with 2 columns, but indexing doesn't matter, and one or both of the columns could be duplicated, as follows:

 

Price           Quantity

200             2

300            1

100            10

200             55

60                1

 

As you can see, the numbers could be repeated.  Ideally, I'd like to do this as an associative array rather than having to add another column as an index column, since the numbers will not need to be referenced, but more or less just added together and summed up.

 

Any ideas on this?

 

 

Link to comment
https://forums.phpfreaks.com/topic/288413-need-help-with-unindexed-array-in-php/
Share on other sites

Okay, in general based solely on what you've posted, you should have an array that looks like this:

 

$products = array(
  'price' => array(200,300,100,200,60),
  'quantity' => array(2,1,10,55,1)
);
Then you could for example do echo array_sum($products['price']); for the total price.

 

However.. I have a sneaking suspicion you could probably be doing this more efficiently based on where you are getting the data from in the first place. For example, if you are getting the data from a flatfile or db you're likely already using some kind of loop already.. why not just throw a $total_price+=$price; in there to get the total? Or e.g. if you have some kind of array (e.g. session var) of stuff a user put into their shopping cart, why are you making a separate array structure just to get the total? Work with what you have (e.g. loop through what you already have for a total).

 

IOW it seems to me that you likely already have the data in xyz format so it's not clear to me why you are trying to restructure things to get a total instead of just using what you already have.

 

sidenote: I know you meant "index column" as in a separate column to act like a unique identifier, but fyi php arrays always have an index for each element. It will either be associative or numeric (or both). So in the example above, it's really the equivalent of

 

$products = array(
  'price' => array(
    0 => 200,
    1 => 300,
    2 => 100,
    3 => 200,
    4 => 60
  ),
  'quantity' => array(
    0 => 2,
    1 => 1,
    2 => 10,
    3 => 55,
    4 => 1
  )
);
Just mentioning this because you can use that instead of a separate index column if need be, for example:

 

$products = array(
  'price' => array(
    'prod_id_1' => 200,
    'prod_id_2' => 300,
    'prod_id_3' => 100,
    'prod_id_4' => 200,
    'prod_id_5' => 60
  ),
  'quantity' => array(
    'prod_id_1' => 2,
    'prod_id_2' => 1,
    'prod_id_3' => 10,
    'prod_id_4' => 55,
    'prod_id_5' => 1
  )
);
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.