Jump to content

Array help


jrobles

Recommended Posts

Im trying to put some information into an array but need some assistance. If i want to put the following info into the array how would I go about it?

 

ProductID

Qty

PricePerUnit

 

The array can contain one product or multiple. Each product will have the three fields i.e.

 

ProductID = 1

Qty = 2

PricePerUnit = $4.00

 

ProductID = 2

Qty = 4

PricePerUnit = $2.76

 

ProductID = 3

Qty = 26

PricePerUnit = $4.34

 

etc...

Link to comment
https://forums.phpfreaks.com/topic/186189-array-help/
Share on other sites

You'll need a multidimensional array. One for each item, contained within one for all items.

 

$items = array();
$items[] = array(
  'ProductID' => 1,
  'Qty' => 2,
  'PricePerUnit' => 4.00
);

$items[] = array(
  'ProductID' => 2,
  'Qty' => 4,
  'PricePerUnit' => 2.76
);

$items[] = array(
  'ProductID' => 3,
  'Qty' => 26,
  'PricePerUnit' => 4.34
);

Link to comment
https://forums.phpfreaks.com/topic/186189-array-help/#findComment-983327
Share on other sites

a quick method (I'm sure there's a better way) is something like this:

<?php
$data = "ProductID = 1
Qty = 2
PricePerUnit = $4.00
ProductID = 2
Qty = 4
PricePerUnit = $2.76
ProductID = 3
Qty = 26
PricePerUnit = $4.34";
$data_array = explode("\n", $data);
$count = 0;
$new_array = array();
foreach ($data_array as $val){
$tmp_array = explode(" = ", $val);
if ($tmp_array[0] == "ProductID"){
	$count++;
}
$new_array[$count][$tmp_array[0]] = $tmp_array[1];
}
print_r($new_array);

If you want a full explanation/help understanding any/all parts of this, just ask.

Link to comment
https://forums.phpfreaks.com/topic/186189-array-help/#findComment-983329
Share on other sites

I eventually will be putting the array in a cookie where I can grab it from another page and insert the  values into a mysql table as part of an "order detail" table. Jonsjava's code looks like what I need , how can I have to data in the $data var regardless of how many products I have. Jonsjava can you help me understand your code, i'm fairly new at PHP

 

thanks a million!

 

Link to comment
https://forums.phpfreaks.com/topic/186189-array-help/#findComment-983335
Share on other sites

basically, it takes a dump of data, that is formatted exactly like your input:

ProductID = 1

Qty = 2

PricePerUnit = $4.00

 

ProductID = 2

Qty = 4

PricePerUnit = $2.76

 

ProductID = 3

Qty = 26

PricePerUnit = $4.34

and turns it into an array, based upon new lines. it then parses through each new array value, and splits up the value into a new array. It then checks the first bit to see if it's the first item in a new array. if it is, it auto-increments it to the next number. So, if you were working on array 0, and it sees "ProductID" it will begin array 1 and so on.

Link to comment
https://forums.phpfreaks.com/topic/186189-array-help/#findComment-983336
Share on other sites

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.