0

Im trying to make a multidimensional array with two columns. Name and Counter. I can do a single array with all the names. But I dont know how to make it multidimensional and be able to still update the counters. Code i got so far is

if (!in_array($prodname, $da)){
    array_push($da, $prodname);
}

and then I can dump it back out with a foreach. How do I make it two dimensional? How can I say alright this exists update the old value? etc.

3
  • If you just need name and a counter can't you use a normal array? Commented Jan 23, 2013 at 2:07
  • 1
    Where are your counters or what are you trying to count? Fill in the question a little more. Commented Jan 23, 2013 at 2:12
  • I am trying to generate a list of inventory and whats in stock. Im doing sql calls to differnt tables and I want to have a list of all the products and out put it in a list like Hammer:1 Screwdriver:5 TapeMeasure:2 etc Commented Jan 23, 2013 at 2:54

2 Answers 2

1

If you only need name and counter then you should just be able to use a normal array:

$nameCountArray = array();
foreach($names as $name){
    if(!array_key_exists($name,$nameCountArray)){
        $nameCountArray[$name] = 1;
    }else{
        $nameCountArray[$name] = $nameCountArray[$name] + 1;
    }
}

If you do need multidimensional arrays these are just arrays of arrays and can be accessed as such. A good example of this is using a 2d array to store locations (say on a 3 by 3 grid):

$twoDArray = array(
                  0 => array(0 => 1,
                             1 => 4,
                             2 => 7), 
                  1 => array(0 => 2,
                             1 => 5,
                             2 => 8), 
                  2 => array(0 => 3,
                             1 => 6,
                             2 => 9)
              );

//Grab the item at 1,2
$item = $twoDArray[1][2];//Will give '8'
Sign up to request clarification or add additional context in comments.

2 Comments

The first code worked. Is there anyway I can get all the $names that are in the array. Like I can get all the variables by the foreach loop. Can I get the names as well so I can have an output like Hammer: 1
You can loop over the $nameCountArray using foreach($nameCountArray as $name => $count){ then in your loop you can use $name and $count to print out a message.
1

Supposing you want $da to look like this:

Array(
   "name1" => array("score1" => 80, "score2" => 100),
   "name2" => array("score1" => 50, "score2" => 60),
   "name3" => array("score1" => 90, "score2" => 80),
   ...
)

Then all you need to do is something like:

function setScore($prodName, $scoreName, $score)
{
    global $da;
    if (!array_key_exists($prodName, $da)) {
        $da[$prodName] = array();
    }
    $da[$prodName][$scoreName] = $score;
}

setScore("name1", "score1", 80);
setScore("name1", "score2", 100);
setScore("name2", "score1", 50);
...

Unless I'm misunderstanding your question, which is very possible.

1 Comment

This will actually work without the array_key_exists check.

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.