使用php在数组中获取特定列值时出现问题
我创建了一个函数来获取特定标题的计数 我在下面展示了我的数组的一些元素使用php在数组中获取特定列值时出现问题,php,arrays,Php,Arrays,我创建了一个函数来获取特定标题的计数 我在下面展示了我的数组的一些元素 $arrayName = array('0' => array('page_title' => '10 Best Free VPS Hosting Sites 2019 (with Pros and Cons)', 'COUNT' => '465'), '1' => array('page_title' => 'Surfshark Review: Should we Surf the Inter
$arrayName = array('0' => array('page_title' => '10 Best Free VPS Hosting Sites 2019 (with Pros and Cons)', 'COUNT' => '465'), '1' => array('page_title' => 'Surfshark Review: Should we Surf the Internet Ocean with this VPN? (2019)', 'COUNT' => '113'));
我想从这个数组中获取特定页面标题的计数。为此,我创建了以下函数:
function particular_page_title_count($arrayName, $find_aff){
foreach ($arrayNameas $row) {
$affiliate = $row['page_title'];
$count = $row['COUNT '];
if ($affiliate == $find_aff) {
return $count;
break;
}
}
}
$particular_page_title_count = particular_page_title_count($arrayName, 'Surfshark Review: Should we Surf the Internet Ocean with this VPN? (2019)');
echo $particular_page_title_count;
当我打印这个函数的输出时,它显示null,即使数组中有这个页面标题
我不知道为什么会发生这种情况。有人能帮我吗。你可以用array\u列来做同样的事情
$arrayName = array('0' => array('page_title' => '10 Best Free VPS Hosting Sites 2019 (with Pros and Cons)', 'COUNT' => '465'), '1' => array('page_title' => 'Surfshark Review: Should we Surf the Internet Ocean with this VPN? (2019)', 'COUNT' => '113'));
$temp = array_column($arrayName, 'COUNT', 'page_title');
$particular_page_title_count = ($temp['Surfshark Review: Should we Surf the Internet Ocean with this VPN? (2019)'] ?? '');
echo $particular_page_title_count;
-从输入数组中的单个列返回值
注意:-数组_列()返回由列_键标识的输入的单个列的值。可选地,可以提供索引键,以通过输入数组的索引键列中的值对返回数组中的值进行索引
我正在考虑你将有独特的网页标题
O/p
113
您的问题在这一行:
$count = $row['COUNT '];
在COUNT
之后有一个额外的空格。换成
$count = $row['COUNT'];
而且效果很好
然而,PHP中有许多函数可以使编写此类函数更容易。例如,您可以使用在页面标题
列中查找标题,然后使用该键值获取计数
:
function particular_page_title_count($arrayName, $find_aff){
if (($k = array_search($find_aff, array_column($arrayName, 'page_title'))) !== false) {
return $arrayName[$k]['COUNT'];
}
return 0;
}
输出:
113
这是您编辑的工作代码,请执行它
<?php
$arrayName = array('0' => array('page_title' => '10 Best Free VPS Hosting Sites 2019 (with Pros and Cons)', 'COUNT' => '465'), '1' => array('page_title' => 'Surfshark Review: Should we Surf the Internet Ocean with this VPN? (2019)', 'COUNT' => '113'));
function particular_page_title_count($arrayName, $find_aff){
foreach ($arrayName as $row) {
$affiliate = $row['page_title'];
$count = $row['COUNT'];
if ($affiliate == $find_aff) {
return $count;
}
}
return 0;
}
$particular_page_title_count = particular_page_title_count($arrayName, 'Surfshark Review: Should we Surf the Internet Ocean with this VPN? (2019)');
echo $particular_page_title_count;
?>
添加了空间$arrayName,并从$row['COUNT']中删除了空间,这样就可以工作了。一个建议是不要使用与变量名相同的函数名。它变得很难处理。您的代码中有一些输入错误:-在foreach中,as
应该用空格分隔-在键COUNT
后有一个``号,这可能会导致一些问题。这也是一个很好的解决方法。您可能需要考虑<代码> ISSET (或NULL聚结运算符)以避免未定义的索引错误。正如您所说,MID。现在看一看。@Rahul它向我显示了错误:未定义索引:Surfshark评论:我们应该用这个VPN在互联网海洋上冲浪吗?(2019)我在回答中分享了演示链接。您可以直接检查。@Mohini如果原始数组中不存在标题,则返回0。如果您选中my,您可以看到在数组中标题所在的位置,它返回正确的COUNT
值。