Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.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 如果已在输入值中,如何不回显值?_Php - Fatal编程技术网

Php 如果已在输入值中,如何不回显值?

Php 如果已在输入值中,如何不回显值?,php,Php,我在自定义字段中有一些值: save_post = "1200" 也可能是,因为我最终需要一份清单: save_post = "1200, 1460, 1334" 现在,当我加载页面时,我获取这些值,并在输入字段中设置它们,我还从当前页面添加当前值id: $allPosts = $userPosts.", ".$postid; 其中,$userPosts是自定义字段中的单个值或值列表,$postd是我要添加的当前页面id 结果是: <input type="text" value=

我在自定义字段中有一些值:

save_post = "1200" 
也可能是,因为我最终需要一份清单:

save_post = "1200, 1460, 1334"
现在,当我加载页面时,我获取这些
,并在
输入字段中设置它们
,我还从
当前页面添加
当前值id

$allPosts = $userPosts.", ".$postid;
其中,
$userPosts
是自定义字段中的单个值或值列表,
$postd
是我要添加的当前页面id

结果是:

<input type="text" value="1200, 23138, 23138, 23138">
此外:


这是一个非常基本的示例,但我认为这对满足您的需求非常有用:

<?php
// Input data
$userPosts = '19000, 23138, 23138';
$postid = '23138';

// With array
$userPosts = str_replace(' ', '', $userPosts);
if (empty($userPosts)) {
    $a = array();
} else {
    $a = explode(',', $userPosts);
}   
$a = array_unique($a, SORT_STRING);
if (in_array($postid, $a) === false) {
    $a[] = $postid;
}
$userPosts = implode(', ', $a);
echo 'Result using array: '.$userPosts.'</br>';
?> 
然后,在加载表单时,您可以尝试以下操作:

...
$a = array_unique($a, SORT_STRING); 
...
update_user_meta($user_id, 'save_post', getUniquePosts($a, $user_id));

以下是我在提交后检查重复值的代码:

$userPosts = '19000, 23138, 23138';
$postid = '23138';
$pattern = "/(?:^|\W)".$postid."(?:$|\W)/";

if(preg_match($pattern, $userPosts, $matches))
{
    print 'There is a duplicate '.rtrim($matches[0] , ",");
}

基本上我重用了Zhorov的变量,但在他的方法中,他将其放入数组,然后检查该数组是否包含提交的值,我的方法与他的方法几乎相同,但不是将其放入数组中;我使用正则表达式来确定字符串中是否存在值。

在数组函数中使用数组更好check@SanoojT更新了问题我在执行以下操作时仍然获得重复值:
$postd=$post->ID;$userPosts=get_user_meta($user_id,'save_post',TRUE);$a=分解(“,”,$userPosts);如果(在数组中($postid,$a)==false){$a[]=$postid;}$userPosts=内爆(',',$a)
然后
如果我回显我得到的那些值
$postid=23138
$userPosts=19000、23138、23138
如果自定义字段为空,当前id将显示在输入中,但它的开头有逗号
,19000
它应该是
19000
$userPosts=ltrim($userPosts,,”)
@rob.m我已经更新了答案以检查空$userPosts。如果我这样做:
$a=array\u unique($a,SORT\u STRING);更新_user_meta($user_id,'save_post',$a)
提交之前
,为了
首先用
唯一值更新字段
,如果您不想输出匹配项,则它不起作用,然后在If语句的代码块内执行您需要完成的任何操作。或者简单地将其修改为并添加else语句。
$allPosts = array($userPosts.", ".$postid); 
$allPosts = array_unique($allPosts); 
$allPosts = implode(", ", $allPosts);
<input type="text" name="save_post_value" value="<?php echo $allPosts; ?>"
<?php
// Input data
$userPosts = '19000, 23138, 23138';
$postid = '23138';

// With array
$userPosts = str_replace(' ', '', $userPosts);
if (empty($userPosts)) {
    $a = array();
} else {
    $a = explode(',', $userPosts);
}   
$a = array_unique($a, SORT_STRING);
if (in_array($postid, $a) === false) {
    $a[] = $postid;
}
$userPosts = implode(', ', $a);
echo 'Result using array: '.$userPosts.'</br>';
?> 
<?php
function getUniquePosts($xposts, $xid) {
    $xposts = str_replace(' ', '', $xposts);
    if (empty($xposts)) {
        $a = array();
    } else {
        $a = explode(',', $xposts);
    }   
    $a = array_unique($a, SORT_STRING);
    if (in_array($xid, $a) === false) {
        $a[] = $xid;
    }
    $xposts = implode(', ', $a);
    $xposts = ltrim($xposts, ",");

    return $xposts;
}

$userPosts = '19000, 23138, 23138';
$postId = '23138';
echo getUniquePosts($userPosts, $postId).'</br>';
?> 
...
$a = array_unique($a, SORT_STRING); 
...
update_user_meta($user_id, 'save_post', getUniquePosts($a, $user_id));
$userPosts = '19000, 23138, 23138';
$postid = '23138';
$pattern = "/(?:^|\W)".$postid."(?:$|\W)/";

if(preg_match($pattern, $userPosts, $matches))
{
    print 'There is a duplicate '.rtrim($matches[0] , ",");
}