Problems with PHP array

1

Friends I have an array that comes from a database with the following format:

["2018-07-25","2018-07-26","2018-07-26"]

With a json decode I convert it into an array, in order to be able to treat it since the code is to find a date and remove it from the array and then return it to the database.

To do that I use the following code:

$ls['fecha']

is the text with the array that comes from the database.

$_POST['glo']

is the date to search.

    $arra = json_decode($ls['fecha']);

    if (in_array($_POST['glo'], $arra, true)) {

        $este = array_keys($arra, $_POST['glo']);
        $clave = array_search($_POST['glo'], $arra);
        unset($arra[intval($clave)]);

        $sql = "UPDATE tabla SET fecha = '".json_encode($arra, JSON_UNESCAPED_UNICODE)."' WHERE r = '".$_POST['r']."'";
        $con = mysqli_query($conx, $sql);

    }

In fact it looks for and deletes the date but when saving the array again in the db it changes the format of the array, like this:

{"0":"2018-07-25","2":"2018-07-26"}

And I need the array to be like this:

["2018-07-25","2018-07-26"]

Any Solution ?? or something am I doing wrong ?? A thousand thanks

    
asked by Mauricio Delgado 25.07.2018 в 16:17
source

2 answers

0

If I understand correctly, you receive a string like this from the database:

["2018-07-25","2018-07-26","2018-07-26"] and you want to convert it into an array to remove a certain value and then return that array to the database.

I will propose a code to do what you want, although I wonder why you bring data to be discarded instead of applying a filter when selecting the data ... but that is another matter.

The solution I propose consists of the following:

  • a. Convert the given string into an array by using json_decode
  • b. At the same time, apply the array_unique function to the result of the array obtained in to to remove possible duplicate values.
  • c. Securely use array_search to find a given value and remove it from the array using unset .

The result will be the same array, but without the value that has been indicated to be removed.

The code would be this:

/*$str representa tus datos reales, a los que no tengo acceso*/
$str='["2018-07-25","2018-07-26","2018-07-26"]';
$arrDatos = array_unique(json_decode($str));
/*Dato que se quiere quitar del array*/
$aBorrar="2018-07-26";
if (($key = array_search($aBorrar, $arrDatos)) !== false) {
    unset($arrDatos[$key]);
}

print_r($arrDatos);

Exit:

Array
(
    [0] => 2018-07-25
)

Proof of concept

You can see here a DEMONSTRATION made in rextester.

I hope it's useful.

    
answered by 26.07.2018 в 00:35
-1

I do not understand why you need to record your dates with this format, but when you convert it to an array it will always show you {"0": "2018-07-25", "2": "2018-07-26"}, what you should to do is to pass your array to the format that you want, I hope the following code will help you in something.

$arra = json_decode('["2018-07-25","2018-07-26","2018-07-26"]');

    if (in_array("2018-07-26", $arra, true)) {

        $este = array_keys($arra, "2018-07-26");
        $clave = array_search("2018-07-26", $arra);
        unset($arra[intval($clave)]);

        $valor = "[";
        $i = 1;
        $count = count($arra);
       
       	// recorro el nuevo arreglo para pasarlo al formato deseado
        foreach ($arra as $key => $value) {
        	if ( $i === $count ) {
        		$valor .= '"'.$value.'"';
        	}else{
        		$valor .= '"'.$value.'",';
        	}

        	$i++;
        }

        $valor .= "]";
    	echo "<pre>"; var_dump($valor); exit();

    }
    
answered by 25.07.2018 в 16:39