0

カンマ区切りの文字列変数から 1 つの値を削除したい Eg hdnListCL ="'ABC','PQR','XYZ'" そのうちの 1 つを削除したいのですが、これを配列に変換せずに削除するにはどうすればよいですか? htmlコード

<input type="hidden" name="hdnlistCL" id="hdnlistCL"/>
<table>
 <tr>
    <td>
            <div id="ProjList"><?php print $strLocName; ?></div><br/>
         <input type="text" name="txtCommonLocation" id="txtCommonLocation" size="40" value=""/>
         <img src="images/plus.gif" title="Click Here" onclick="AddNewLocation()" style='cursor:pointer;' align="right"/>  </td>    
     </tr>
</table>

JavaScript

<script type="text/javascript" src="../scripts/jquery.min.js"></script>
<script type="text/javascript">
function AddNewLocation()
{
    var listCL = "";
    this.strProjName;
    if ( typeof strProjName == 'undefined' ) {
          strProjName = '';
    }
    var newLoc = document.getElementById('txtCommonLocation').value;
    document.getElementById('txtCommonLocation').value = "";
    if(document.getElementById('hdnlistCL').value == '')
    { 
       document.getElementById('hdnlistCL').value =newLoc;
    }
    else
    {
      document.getElementById('hdnlistCL').value += ","+newLoc;
    }
      listCL  = newLoc; 
      if(listCL != '')  
        {
             strProjName = strProjName + '<div id="'+listCL+'">'+listCL+'<div  class="close" onclick="removeLocation(\''+listCL+'\')"></div></div>';            
        }
    //  alert(strProjName);
        $('#ProjList').html(strProjName);   
}
function removeLocation(pLocation)
{
    var hdnListLocation = document.getElementById('hdnlistCL').value;
    if(window.confirm("Are you sure you want to delete this?"))
                {      
                            url = 'test_st.php';
                            $.post(
                                url,
                                {"act":"Delete","DelLocation":pLocation,"hdnListLocation": hdnListLocation},
                                function(responseText){
                                    alert(responseText);
                                    return false;
                                  window.location="test_st.php";                    
                                },
                                "html"
                            );
                            return false;
                }   
}
</script>

phpコード

<?php
 if(isset($_POST['act']))   
       $act = $_POST['act'];

        if($act == "Delete")
        {
            $arrhdnListLocation     = explode(",", $_POST['hdnListLocation']);
            if(in_array($_POST['DelLocation'],$arrhdnListLocation))
            {
            print("I want to remove'".$_POST['DelLocation']."' from hdnlistCL");        
                    exit();
            }
            else
            {
                print("No");
                exit();
            }

        }
?>

ここに画像の説明を入力

閉じるボタンをクリックすると、hdnlistCL からその場所を削除したいと思います。どうすればこれを行うことができますか? hdnListCL の値は ABC,PQR,XYZ これから PQR を削除したい

4

2 に答える 2

3
$string = "'ABC','PQR','XYZ'";
$remove  = "'PQR'";

// you can use array_diff method to remove specific array item
$result = array_diff(str_getcsv($string), array($remove))
echo implode(',', $result);  // output is "'ABC','XYZ'"

PHP フィドルの例: http://phpfiddle.org/main/code/v260-p3cf

于 2017-02-01T08:23:23.343 に答える
2

これを試してください

$hdnListCL ="'ABC','PQR','XYZ'";
$hdnListCL=explode(',',$hdnListCL);
$index = array_search('PQR',$hdnListCL);
if($index !== false){
    unset($hdnListCL[$index]);
}
$hdnListCL=implode(',',$hdnListCL);
print_r($hdnListCL);
于 2012-11-07T05:45:09.030 に答える