I am building a portfolio with multiple categories. I have a multidimensional array…
$allProjects = array(
'project1' => array('corporate', 'web'),
'project2' => array('corporate', 'print', 'animation'),
'project3' => array('web')
);
and I need to check in that multidimensional array at a specific key (e.g 'project1') if a value exists (e.g. 'web')
so I guess something like this…
$project = $_GET('project'); //this is the project that is displayed
$category = 'print' //for example, I would redefine this variable for each category.
foreach ($allProjects as $project => $categories) {
if in_array($category, $project);
echo 'yes';
}
I use $project, variable defined above, as key but that doesn't work. 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
Try this:
$project = $_GET['project'];
$category = 'print';
$categories = $allProjects[$project];
if (!empty($categories) && in_array($category, $categories)) {
echo 'yes';
}
What you want doesn't need a loop:
<?php
$allProjects = array(
'project1' => array('corporate', 'web'),
'project2' => array('corporate', 'print', 'animation'),
'project3' => array('web')
);
$project = $_GET['project'];
$category = 'print';
if(in_array($category, $allProjects[$project])) {
echo 'yes';
}
?>