2

I want to make my category menu responsive to the projects displayed. The projects have multiple categories so multiple category-menu-links can be active. Whichever category the project belongs to is specified on an included php file in an multidimensional array…

e.g.

$allProjects = array(
    'project1' => array('corporate'),
    'project2' => array('corporate', 'print'),
    'project3' => array('web')
);

Now I want to check for each category if it is a value of the array 'project1', 'project2', 'project3',… within the array $allProjects and if so echo 'active'.

So far I have…

<?php if (($_GET['cat'] == 'corporate')) {echo 'active';}; || if (isset ($_GET['project'])) {if in_array('corporate', $_GET['project']) {echo 'active';}; ?>

Does that make sense?

UPDATE:

What I need is to check in the multidimensional array at a specific key (2nd level array) if a value exists (3rd level array)

so I guess something like this…

$project = $_GET('project');
$category = $_GET('cat');

foreach ($allProjects as $project => $categories) {
if in_array($category, $project);
echo 'yes';
}

I use $project, variables defined above, as key but that doesn't work. It expects the parameters to be arrays. All I want is to use the name of the project (defined in $project) as a key for the in_array function and check for appearance of $category in the values.

Thanks for help

3 Answers 3

2

You can use array_search() to search for a specific value in your array. This function returns the key corresponding to the value if found, false otherwise.

So what you will want to do is loop each subarray:

$category = $_GET['cat'];
$allProjects = array(
    'project1' => array('corporate'),
    'project2' => array('corporate', 'print'),
    'project3' => array('web')
);

foreach ($allProjects as $projectName => $categories) {
    $categoryIndex = array_search($category, $categories);
    if ($categoryIndex !== false) {
        echo 'active: ' . $categoryIndex;
        // Do something with $categoryIndex and $projectName here
    }
}

Update:

Looks like this is your answer:

$project = $_GET('project');
$category = $_GET('cat');

if (isset($allProjects[$project]) && in_array($category, $allProjects[$project])) {
    echo 'yes';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but it gives me an invalid argument: <b>Warning</b>: Invalid argument supplied for foreach()
@tgifred Make sure $allProjects is initialized correctly like shown in your question. Updated my answer.
thanks RuubW, I tried, I think I didn't make myself clear. I updated my question
2

I'm not sure, what is in $_GET['project'] in your condition, but this will at least make your code better readable. :)

isActive = false;
foreach($allProjects as $project) {
  if(in_array($_GET['cat'], $project) isActive = true;
}

1 Comment

you should add a break to finish the cycle after first match. there's no need to continue the loop
1

You perhaps want to go through the array using a foreach and set your active / non active indicator accordingly.

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.