0

I have one element of a project that is holding me back and try as I might I have not been able to get this to work. Can someone please assist me - I am sure I am just confused by it all and that there is an easy answer. I have 3 multi-dimensional arrays (3 different pricing data from different companies), EACH holding this sort of data:

[0] => Array
(
    [itemcode] => 27663
    [buy] => 50000
    [perquantity] => 5
)

[1] => Array
(
    [itemcode] => 38663
    [buy] => 41000
    [perquantity] => 4
)

The same itemcodes appear in the data from all 3 companies. I was to create an html table that has the following columns from this data:

ItemCode  | Supplier1  | Supplier2  | Supplier3  | Difference (2nd lowest minus lowest)
38663   | 12500  | 12425  |  16440  |  75

My question: Is there a shortcut to doing this, say by creating another array to temp store data? Or is there a concept you feel is required to complete this easily?

Any help is VERY appreciated.

2
  • whhy use 3 dimensional array why not use a class and create an object store all the information in the object and create array or list of objects Commented Jun 5, 2012 at 6:13
  • The data I receive is pulled from malformed JSON data that was only able to be pull apart into arrays. I don't have any control over the data extracted from the software the suppliers are using. Commented Jun 5, 2012 at 23:42

1 Answer 1

1

I don't know where the numbers in your HTML table come from, but I think creating a temporary array would be the way to go.

$items=array();
foreach ($supplier1 as $data) {
  $items[$data['itemcode']]['Supplier1buy']  = $data['buy'];
  $items[$data['itemcode']]['Supplier1perq'] = $data['perquantity'];
}
foreach ($supplier2 as $data) {
  $items[$data['itemcode']]['Supplier2buy']  = $data['buy'];
  $items[$data['itemcode']]['Supplier2perq'] = $data['perquantity'];
}
// etc.

To make prettier code, I think I'd need to know more about how the data works. For example, I don't know if you really need 'perquantity'. Is the number in your HTML table a function of buy and perquantity?

Once you've got your large $items array, you can walk through it to create the rows of your HTML table.

Sign up to request clarification or add additional context in comments.

1 Comment

I will definitely use a temporary array - it seems the most efficient way to pull the information together. I probably only need (buy / perquanitity) to get the per each price. Another part of the problem that I need to deal with is that each price list may contain more than one list of the same itemcode. Say for bulk buying or the likes. I really only need the lowest possible price for each item from each supplier. But I think I will be right from here on out - thank you very much for your recommendations. Much appreciated /bow :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.