How to check if there is only one position in the array element?

Asked

Viewed 733 times

1

In my code I do a search on BD adding to the array the results found. And before the value I enter a letter to identify the results.

<?php
    $w_select ="SELECT *    FROM public.sai_cad_patr_seri WHERE 
                                            sai_cad_patr_seri.fk_seq_cara_peri = '$arr_w_param[17]'";
    $w_querybusc = "$w_select;"; 
    $w_queryresult=f_class_conecta_bd($w_querybusc);
    $index = 0;
    $patr = array();
    $seri = array();        
    while($w_registro = pg_fetch_object($w_queryresult))
    {
        $patr[$index] = "P".trim($w_registro->tx_num_patr);
        $seri[$index] = "S".trim($w_registro->tx_num_seri);
        $index++;
    }
?>

But I came across the following problem ... After I have picked up these values I add them into one array JS, but should the array $patr has only inserted the letter 'P' or see the $seri and no value then it will take up space and will mess up any next change.

So I wonder if you have any way of checking if you have any empty position after the 'P' or 'S'!

$patr = [P ];
  • 2

    It would not be better to create a field (element in a new dimension of the array) and treat the type separately from the contents of the register? These solutions make a string to gather data and then have to treat it exceptionally is not a very recommended practice.

  • Yes I agree with you @bigown, but regarding my supervisor I should do this tripe for "good company practices"!

4 answers

2


PHP

In order to check if the second position is a space, you can make use of the function substr() or the function mb_substr() to collect the first two characters depending on the data single-byte or multi-byte:

// singlebyte
$resultado = substr($valor, 0, 2);

// multibyte
$resultado = mb_substr($valor, 0, 2);

Then make a comparison between what was collected and that same value filtered by the function trim():

if ($resultado != trim($resultado)) {
  // tinha um espaço
} else {
  // não tinha um espaço
}

If you specifically want to know if the space was on the right, that is, after the letter P, you can make use of the function rtrim().

The solution can be compressed into a function:

function comEspaco($valor='', $posicao=2) {

    $resultado = mb_substr($valor, 0, $posicao);

    return ($resultado != rtrim($resultado));
}

echo comEspaco('P '); // TRUE
echo comEspaco('P');  // FALSE

This example can be seen in Ideone.


Javascript

In Javascript you can check using the method charAt() which allows you to obtain the character in position X and later using the method trim() to clear any space and thus perform the comparison:

var valor     = 'P ',
    resultado = valor.charAt(1);

alert( resultado != resultado.trim() ); // TRUE

2

You can do a check using the function substr.

The function will return the given part of any string.

In case it would be something like the code below:

if (substr($part["P "], 1, 1) === " "){
    // do stuff when true
} else {
    // do stuff when false
}

1

0

Why don’t you do something like:

while($w_registro = pg_fetch_object($w_queryresult))
{
    $w_patr = trim($w_registro->tx_num_patr);
    $w_seri = trim($w_registro->tx_num_seri);

    if (!empty($w_patr))
        $patr[$index] = 'P' . $w_patr;

    if (!empty($w_seri))
        $seri[$index] = 'S' . $w_seri;

    $index++;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.