Forum Moderators: coopster
/*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)
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
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);
Array('TestKits' => array (0=>
'Strontimum',
1 => 'Nitrate',
(...)),
'Additives' => array (0=>
'Coral Calcium',
1 => 'Natural Iodine',
(...))
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
foreach($BigArray as $k => $v){
echo '<strong>'.$k.'</strong><br />';
foreach($v as $v1){
echo $v1.'<br />';
}
}
$BigArray['Additives'][] = 'Monosodiumglutumate';
echo '<pre>';
print_r($BigArray);
echo '</pre>';
$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
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)
$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
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