Forum Moderators: coopster

Message Too Old, No Replies

multi-dimensional array

two into one

         

SteveD

4:02 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



Hi,
My first posting on webmasterworld, so hello to all, hope I can find some help here.
I am not (yet) fully competent in PHP but getting there, have been delving into arrays and have got stuck on the following problem. It works like this:

/*two arrays created from db output*/
Array ( [0] => Strontium [1] => Nitrate [2] => Magnesium [3] => Coral Calcium 250ml [4] => Natural Iodine 1000ml )

Array ( [0] => Test Kits [1] => Test Kits [2] => Test Kits [3] => Additives [4] => Additives)


I want to turn them into an md array that looks like this:

array(array(Test Kits => Strontium, Nitrate, magnesium),
array(Additives =>Coral Calcium,Natural Iodine 1000ml)
);

To create an associative array I would use


$arr = array();
for($i = 0; $< count($array1); $i++)
$arr[$array1[$i]]=("$array2[$i]");

This wouldn't work on the above as the duplicate keys will be lost and the return is not what I am looking for. I can count the duplicate elements in $array1 like:


for($i = 0; $i < count($cat); $i++)
{
$tmp[$cat[$i]] =! isset($tmp[$cat[$i]])? 1 : $tmp[$cat[$i]] + 1;
}

print_r($tmp);
//returns
Array ( [Test Kits] => 3 [Additives] => 2 )

I think then I need to use a while loop, but I cannot figure out the syntax to make this happen, so I would appreciate any and all help I can get

Thanks in advance

SteveD

mincklerstraat

4:26 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi SteveD, welcome to webmasterworld.

Is what you're trying to do something like this:


$TestKits = array('Strontium', 'Nitrate', 'Element3', 'Element4');
$Additives = array('Coral Calcium', 'Natural Iodine', 'additive 3');
$BigArray = array('TestKits' => $TestKits, 'Additives' => $Additives);

This will yield:

Array('TestKits' => array (0=>
'Strontimum',
1 => 'Nitrate',
(...)),
'Additives' => array (0=>
'Coral Calcium',
1 => 'Natural Iodine',
(...))

SteveD

4:40 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



Hi,
That <was a rapid response, answer yes.
Ultimately what I am trying to do is echo like this:
Test Kits
strontium
Nitrate
magnesium

Additives
Coral Calcium 250ml
Natural Iodine 1000ml

I cannot echo as above with the arrays that I have, so the $bigarray looks like what I need, so how do I get there?

Cheers

SteveD

mincklerstraat

5:56 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looking for something like this?

foreach($BigArray as $k => $v){
echo '<strong>'.$k.'</strong><br />';
foreach($v as $v1){
echo $v1.'<br />';
}
}

Each time you need to 'add' something to part of this array, do it like this:
$BigArray['Additives'][] = 'Monosodiumglutumate';

You can still output the contents of multidimensional arrays by just doing:
echo '<pre>'; 
print_r($BigArray);
echo '</pre>';

SteveD

8:16 am on Feb 8, 2005 (gmt 0)

10+ Year Member



Hi,
I probably haven't explained myself properly, in that I do not have a problem with printing arrays, and in general, manipuating arrays, what I am not getting is how to create the arrays from the info I have.
First the arrays:

$prodTypes = ("Test Kits", "Test Kits", "Test Kits", "Additives", "Additives");

$products = ("Strontium","Nitrate","Magnesium","Calcium","Iodine");

I do not know the contents or the number of elements in each array, the only thing I know for sure is that both arrays have exactly the same number of elements, so by using the following:

for($i = 0; $i < count($prodTypes ); $i++)
{
$tmp[$prodTypes [$i]] =! isset($tmp[$prodTypes [$i]])? 1 : $tmp[$prodTypes [$i]] + 1;
}

print_r($prodTypes); returns
Array ( [Test Kits] => 3 [Additives] => 2 )

which tells me that the first three elements of $products are Test kits and the next two are additives

Now, my problem is how to get the correct number of elements into arrays $Testkits and $additives.
I have tried using a do while loop, a foreach loop a for loop and if/else arguments but I am missing something somewhere and it is beginning to drive me a little crazy.
I dont' really care if the arrays are multidimensional or associative ( where the value is a string of the product names), however all I am managing to achieve is one array of product names or an infinite loop, which implies ( to me ) that there is a problem with the syntax.
I hope that explains the problem better and that you can help me ( please!)

Cheers

SteveD

orion_rus

8:37 am on Feb 8, 2005 (gmt 0)

10+ Year Member



SteveD, why don't u simple make array1 by array2:

Array ( [0] => Strontium [1] => Nitrate [2] => Magnesium [3] => Coral Calcium 250ml [4] => Natural Iodine 1000ml )

Array ( [0] => Test Kits [1] => Test Kits [2] => Test Kits [3] => Additives [4] => Additives)


U would have array (['strontium']=>['Test Kits'],[Nitrate]=>['Test Kits'] ...)
and so on?
You wouldn't have a overwrite effect and can count as much as u want
with element loop like foreach ($newarray as $key=>$value)?

SteveD

10:41 am on Feb 8, 2005 (gmt 0)

10+ Year Member



Hi,
If I am understanding you correctly, I am already there, I am just not breaking into the loop properly ( syntax?)
Here is my code to date and the results, also I have renamed my arrays in order to understand my code better, but.....

$specials = array();
for($i = 0; $i < count($prods); $i++)
{
$specials[$prods[$i]]=("$category[$i]");
}

returns :
Array ( [Strontium] => Test Kits [Nitrate] => Test Kits [Magnesium] => Test Kits [Coral Calcium 250ml] => Additives [Natural Iodine 1000ml] => Additives )

foreach($tmp as $key =>$val)
{
$n = number_format($val);
$type = $key;
echo "<b>$type</b><br />";
$i = 0;
foreach($specials as $k => $v)
{

if($i <= $n)
{
echo "$k<br />";
}
else if($i == $n)
{
echo "$type<br />";
}
}
$i++;
}

returns:
Test Kits
Strontium
Nitrate
Magnesium
Coral Calcium 250ml
Natural Iodine 1000ml
Additives
Strontium
Nitrate
Magnesium
Coral Calcium 250ml
Natural Iodine 1000ml

The second foreach loop is a variation on a theme, in that I have tried do while, for etc etc
Something is wrong in the way I have built the loop and I suspect that because I have been looking at it longer than I ever wanted to I amissing something very obvious and probably very simple!

Cheers
SteveD

SteveD

4:51 pm on Feb 8, 2005 (gmt 0)

10+ Year Member



Hi,
Just to let anyone interested know that I finally solved the problem like this:
$productlist = array();

for ($i = 0;($i < count($prodTypes)) and ($i < count($products)); $i++)
{
if (!array_key_exists($prodTypes[$i], $products)) {
$products[$prodTypes[$i]] = array();
}
$productlist[$prodTypes[$i]][] = $products[$i];

}

Took a while, and some help from here, PHP .net and of course O'reilly, thanks to all who contributed

Cheers
SteveD

mincklerstraat

7:22 am on Feb 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad to hear you got sorted. Arrays can get really, really hairy; but learning to deal with them efficiently gives you such tremendous power in your scripts. Happy coding.