0

i am trying to create an array which stores various photo albums,

so far i have the following code

$photos = array(
            array("karate","1","2"),
            array("judo","1","2"),
            array("kickboxing","1","2"),
            array("womenselfdefense","1","2")
            );

$sections = array("karate","judo","kickboxing","womenselfdefense");
foreach($sections as $keys => $section)
{
    echo count($photos[$section]);
}

but i dont think i have set up my arrays properly, ideally i'd like the main array $photos to have 4 separate arrays $karate,$judo,$kickboxing,$womenselfdefense within it.

i want to start of by counting the number of items in each array and then choose a random item within each array, however i believe at the moment i have 4 unnamed arrays within the photos array and therefor my code returns several Undefined index: errors

can anyone help me with this please

2
  • We don't understand what's the purpose of this? Commented Dec 23, 2016 at 17:33
  • Do a print_r($photos) that should show you your mistake Commented Dec 23, 2016 at 17:35

4 Answers 4

2

You could use the name of the photo album as the key for the photos array.

$photos = array(
    "karate" => array("1","2"),
    "judo" => array("1","2"),
    "kickboxing" => array("1","2"),
    "womenselfdefense" => array("1","2")
);
$sections = array("karate","judo","kickboxing","womenselfdefense");
foreach($sections as $keys => $section)
{
    echo count($photos[$section]).'<br>';
}
Sign up to request clarification or add additional context in comments.

3 Comments

But you have changed the array to suite YOUR purposes. Thats not answering the OP's question
this looks promising, will try it out later when i get home, many thanks!!!
@RiggsFolly I did not suit my purposes. He still has 4 separate array, however they are identified by the keyword.
2

I think you're looking for an associative array, which uses a label instead of a number as a key:

$photos = array(
    "karate" => array("1","2"),
    "judo" => array("1","2"),
    "kickboxing" => array("1","2"),
    "womenselfdefense" => array("1","2")
);

foreach ($photos as $section=>$values) {
    //now $values is your array of numbers
    echo count($values);
}

Or, in modern syntax:

$photos = [
    "karate" => [1, 2],
    "judo" => [1, 2],
    "kickboxing" => [1, 2],
    "womenselfdefense" => [1, 2],
];

Comments

1

You are trying to access the arrays without assigning the higher level arrays any keys.

Try something like this:

$photos = array(
    'karate' => array("karate","1","2"),
    'judo' => array("judo","1","2"),
    'kickboxing' => array("kickboxing","1","2"),
    'womenselfdefense' => array("womenselfdefense","1","2")
);

Comments

0

I think you mean to use

 echo count($photos[$keys]);

Comments

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.