我有一个这样的数组:
pnotifyArr[] = array(
"type" => "New ".$RowP[0]."</br>",
"title" => "<a href=\"/sys/mailbox/MailInbox.php?ID=$RowP[3]$rid\" onclick=\"javascript:update_status('$RefID')\" target=\"_blank\">".$TitleForShow."</a></br>",
"AssignedBy" => "From: ".trim($RowP[2]).".",
"RefID"=>$RefID,
"Status"=>$Status,
"Notice_type" => $TypeN,
"Notice_title" => $TitleForShow,
"Notice_AssignedBy" => $RowP[2]
);
从数据库中检索。但我只需要在所有实例中比较
Notice_type
和Notice_title
的键值。if(count($pnotifyArr)>0)
{
$rows = sizeof($pnotifyArr);
$columns = sizeof($pnotifyArr[0]);
//$columnkeys = array_key_exists('Notice_type',$pnotifyArr[0]);
for ($i =0; $i < $columns; $i++)
{
for ($j =0; $j < $rows; $j++)
{
for($k = $j+1; $k < $rows; $k++)
{
if( $pnotifyArr[$j][$columnkeys[$i]] == $pnotifyArr[$k][$columnkeys[$i]]) //&& ($pnotifyArr[$j][$columnkeys[$i]] == $pnotifyArr[$k][$columnkeys[$i]]) )
$pnotifyArr[$k][$columnkeys[$i]] = "";
}
}
}
}
我不知道如何递归地获取这两个特定键的值。有人能帮我解决这个问题吗?
上面的代码为我提供了所有的键值,任何在我的情况下不需要的键值。
非常感谢你花时间。
for ($i =0; $i < $columns; $i++)
{
for ($j =0; $j < $rows; $j++)
{
for($k = $j+1; $k < $rows; $k++)
{
//if( $pnotifyArr[$j][$typekeys[$i]] == $pnotifyArr[$k][$typekeys[$i]]) && ($pnotifyArr[$j][$titlekeys[$i]] == $pnotifyArr[$k][$titlekeys[$i]]) )
if( $pnotifyArr[$j]['Notice_type'] == $pnotifyArr[$k]['Notice_type'] && $pnotifyArr[$j]['Notice_title'] == $pnotifyArr[$k]['Notice_title'] )
$pnotifyArr[$k][$i] = "";
}
}
}
我的输出显示如下。。。。。。。。如下所示
for ($i =0; $i < $columns; $i++)
{
for ($j =0; $j < $rows; $j++)
{
for($k = $j+1; $k < $rows; $k++)
{
//if( $pnotifyArr[$j][$typekeys[$i]] == $pnotifyArr[$k][$typekeys[$i]]) && ($pnotifyArr[$j][$titlekeys[$i]] == $pnotifyArr[$k][$titlekeys[$i]]) )
if( $pnotifyArr[$j]['Notice_type'] == $pnotifyArr[$k]['Notice_type'] && $pnotifyArr[$j]['Notice_title'] == $pnotifyArr[$k]['Notice_title'] )
{
$pnotifyArr[$k][$i] = "";
}
}
}
}
foreach ($pnotifyArr as $key => $value) {
if ($value="") {
unset($pnotifyArr[$key]);
}
}
$pnotifyArr = array_values($pnotifyArr);
请帮帮我。。。。我不明白这里发生了什么。。。。。。
输出:
测试:{“pnotify”:[
{“type”:“New Mail”,“title”:“RE:Hello”,“AssignedBy”:“From:SAMANTHA CHUA PEIXUAN.”,“RefID”:“1FBC4490C47AE00967623BF93F92B9262”,“Status”:“1”,“Notice\u type”:“Mail”,“Notice\u title”:“RE:Hello”,“Notice\u AssignedBy”:“SAMANTHA CHUA PEIXUAN”},
{“type”:“New Mail”,“title”:“RE:Hello”,“AssignedBy”:“From:SAMANTHA CHUA PEIXUAN.”,“RefID”:“ead077c279a7cfdf60882fcc75a8f23b”,“Status”:“1”,“Notice\u type”:“Mail”,“Notice\u title”:“RE:Hello”,“Notice\u AssignedBy”:“SAMANTHA CHUA PEIXUAN”,“0”:,“1”:,“2”:,“3”:,“4”:,“5”:“,“6”:“,“7”:“}”
最佳答案
要在循环中获取特定值,请执行以下操作:$pnotifyArr[$j]['Notice_type'];
$pnotifyArr[$k]['Notice_title'];
例子:
foreach($pnotifyArr as $key=>$val)
{
if($pnotifyArr[$key]['Notice_type'] == $pnotifyArr[$key]['Notice_title'])
{
$pnotifyArr[$key][XXX] = '';
}
}
要删除空密钥:
foreach($pnotifyArr as $key=>$val)
{
if($pnotifyArr[$key]['Notice_type'] == $pnotifyArr[$key]['Notice_title'])
{
$pnotifyArr[$key][XXX] = '';
}
if (is_null($val))
{
unset($pnotifyArr[$key]);
}
}
然后尝试打印