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

1

In my code I do a search on BD adding array to 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 get these values I add them to a array JS , but if array $patr has only entered the letter 'strong' 'P' or vise-versa with $seri and no value then it will take up space and will hinder any subsequent changes.

So I'd like to know if you have any unsaved position after 'P' or 'S' !

$patr = [P ];
    
asked by anonymous 29.10.2014 / 14:18

4 answers

2

PHP

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

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

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

Then you make a comparison between what was collected and this same value filtered by trim() function. :

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

If you want to know specifically if the space was on the right, that is, after the letter P , you can make use of 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 at > Ideone .

JavaScript

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

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

alert( resultado != resultado.trim() ); // TRUE
    
29.10.2014 / 15:13
2

You can do a check using the substr function.

The function will return the given part of any string.

In the case it would look something like the code below:

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

Make a count in php

$count = count($patr);

If the count is 1, then the array has only one position.

References link link

    
29.10.2014 / 15:08
0

Why do not you do something like this:

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++;
}
    
29.10.2014 / 15:38