我正在尝试创建一个存储各种相册的数组,
到目前为止,我有以下代码
$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]);
}
但是我不认为我已经正确设置了我的阵列,理想情况下我希望主阵列$照片中有4个单独的阵列$ karate,$ judo,$ kickboxing,$ womenselfdefense。
我想首先计算每个数组中的项目数,然后在每个数组中选择一个随机项,但我相信目前我在photos数组中有4个未命名的数组,因此我的代码返回了几个Undefined index :错误
任何人都可以帮助我吗
答案 0 :(得分:2)
您可以使用相册的名称作为照片数组的键。
$oDate1 = new DateTime($datas['due_date']);
$due_date = $oDate1->format("M d, Y");
$sDate1 = $oDate1->format("M d, Y");
答案 1 :(得分:2)
我认为您正在寻找一个associative array,它使用标签代替数字作为关键字:
$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);
}
或者,在现代语法中:
$photos = [
"karate" => [1, 2],
"judo" => [1, 2],
"kickboxing" => [1, 2],
"womenselfdefense" => [1, 2],
];
答案 2 :(得分:1)
您正在尝试访问阵列而不为任何键分配更高级别的阵列。
尝试这样的事情:
$photos = array(
'karate' => array("karate","1","2"),
'judo' => array("judo","1","2"),
'kickboxing' => array("kickboxing","1","2"),
'womenselfdefense' => array("womenselfdefense","1","2")
);
答案 3 :(得分:0)
我认为你的意思是使用
echo count($photos[$keys]);