Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从结果数组中获取变量,为什么不能获取大于1的值_Php_Mysql_Arrays_Variables_Foreach - Fatal编程技术网

Php 从结果数组中获取变量,为什么不能获取大于1的值

Php 从结果数组中获取变量,为什么不能获取大于1的值,php,mysql,arrays,variables,foreach,Php,Mysql,Arrays,Variables,Foreach,好的,我的问题如下,基本上我试图做的是构建href,并将类别主题名称放入。到目前为止,我只能使用$key变量来放置类别名称,但不能使用我的其他变量,如$category\u topic['category\u folder']或$category\u主题['category\u page'] 需要帮忙吗 while ($category_topic = $resc -> fetch()){ $result[$category_topic['category_name']][] =

好的,我的问题如下,基本上我试图做的是构建href,并将类别主题名称放入
到目前为止,我只能使用
$key
变量来放置类别名称,但不能使用我的其他变量,如
$category\u topic['category\u folder']
$category\u主题['category\u page']

需要帮忙吗

while ($category_topic = $resc -> fetch()){ 
    $result[$category_topic['category_name']][] = $category_topic;
}
foreach ($result as $key => $value){
    $category_topic_name = str_replace("&", "and", $key);
    $category_topic_url = DST.$category_topic['category_folder'].DS.$category_topic['category_page'];
print <<<END
<div class="by_cat">
<h2 class="h_unln"><a class="more_templ" href="$category_topic_url">$category_topic_name</a></h2>
<ul class="cat_tmp_list">
END;
但是如何将结果写入变量我不知道


请,请,请帮助。

因为您正在查看
$category\u主题
,这是您在while循环中给出的名称。该变量的值将等于该循环的最后一次迭代

您的foreach循环应该如下所示:

foreach ($result as $key => $value){
    $category_topic_name = str_replace("&", "and", $key);
    $category_topic_url = DST.$value['category_folder'].DS.$value['category_page'];
print <<<END
<div class="by_cat">
<h2 class="h_unln"><a class="more_templ" href="{$category_topic_url}">{$category_topic_name}</a></h2>
<ul class="cat_tmp_list">
END;
foreach($result as$key=>$value){
$category\u topic\u name=str\u replace(“&”,“and”,$key);
$category_topic_url=DST.$value['category_folder'].DS.$value['category_page'];
打印$bar)
您将使用
$bar


希望这能有所帮助!

好的,现在我已经把一切都弄明白了,下面是我的答案

在我的第一种方法中,我只访问第一个数组值(对象),其中的另一个数组是另一个数组。因此,为了访问第二个数组“子数组”,我添加了另一个
foreach
,因此我的整个语句如下所示,最重要的是,所有操作都正常

foreach ($result as $key => $value){
    foreach ($value as $k2 => $v2){
    $category_topic_name = str_replace("&", "and", $v2['category_name']);
    $category_topic_url = DST.$v2['category_folder'].DS.$v2['category_page'];
    }
print <<<END
<div class="by_cat">
<h2 class="h_unln"><a class="more_templ" href="{$category_topic_url}">{$category_topic_name}</a></h2>
<ul class="cat_tmp_list">
END;
foreach($result as$key=>$value){
foreach(价值为$k2=>$v2){
$category_topic_name=str_replace(&,“,”and“,$v2['category_name');
$category_topic_url=DST.$v2['category_folder'].DS.$v2['category_page'];
}

打印让打印($category\u topic)在你的while loopum中,添加这个链接$category\u name[2]@Mihai,我应该在哪里添加它,以及如何添加它,你能解释一下吗?@AlexB刚刚克隆了带有$category\u topic[2]的herdoc而不是$category\u主题_name@Mihai我已经补充了,工作起来很奇怪,所以现在第一个循环的标签变成了第二个循环的标签,然后第二个循环出现在第三个循环上,依此类推,第一个循环因为某种原因空了回来?知道为什么会这样吗?谢谢你的回答,但问题是$category\u topic\u url中的值返回为空,不知道发生了什么。很抱歉,完全忽略了这一点…您需要使用herdoc将变量包装在
{}
中。DS为正斜杠“/”而DST为目标目录,如“folder/”不,仍然不执行任何操作,是否要查看该页面?请确保已将url变量更改为我的答案中使用的变量:)
foreach ($result as $key => $value){
    foreach ($value as $k2 => $v2){
    $category_topic_name = str_replace("&", "and", $v2['category_name']);
    $category_topic_url = DST.$v2['category_folder'].DS.$v2['category_page'];
    }
print <<<END
<div class="by_cat">
<h2 class="h_unln"><a class="more_templ" href="{$category_topic_url}">{$category_topic_name}</a></h2>
<ul class="cat_tmp_list">
END;