Store the string size of an array

Asked

Viewed 92 times

1

Good morning, you guys, I am trying to take the size of a string from an array to store in a variable so that I can then use an "if". I used var_dump() where I can know the size of the string but do not know how to compare this value, these data will be sent to a JSON and displayed in a Datatables (Jquery), could someone give me a light on this or some other alternative ? Thank you.

$sql = "SELECT q_num, time_start, from_userpart, ts_servicing FROM callcent_queuecalls WHERE q_num = '8010' AND ts_servicing = '00:00:00' ORDER BY time_start DESC";

$resultset = pg_query($conn, $sql) or die("database error:" . pg_errormessage($conn));
$data = array();

    while ($rows = pg_fetch_array($resultset)) {
          $array2 = $rows[2];
          $meuarray = explode(' ', $array2);
          foreach($meuarray as $valor) {
              $imprime = strlen($valor);
                  echo $imprime."</br>";
          }
    }

SOLUTION

$array2 = $rows[2];
$intlen = strlen($array2);
if (($intlen === 8) || ($intlen === 9) || ($intlen === 11) || ($intlen ===        12)) {


    $meuarray = explode(" ", $array2);
    foreach ($meuarray as $valor) {
        $str = str_split($valor);
        if (($str[0] === '1') || ($str[0] === '2') || ($str[0] === '7')) {
            $exlui = array_shift($str);
        }
        $implode = implode("", $str);


        echo "<pre>";
        var_dump($implode);
    }
}
  • Have you done any code? You can show the array structure, or exemplify how it would look?

  • @Darleifernandozillmer I just edited the question with part of Cod.

  • try using $imprime = strlen($value) and see what will be the result of echo $prints.

  • @Felipeg. I have tried to use it returns me the value of the correct string until then OK, now how do I take each value of this and be able to compare them after that I’m stuck. The mb_strlen function does not return anything to me.

  • Have an idea of what the final array should look like?

  • So when I use: ' $array2 = $Rows[2]; print_r($array2); ' it seems to return only one arrray. result: 2129880693212988069693212988069311299..., it concatenates all data and when using var_dump it returns to me: string(12) "212988069693" string(12) "212988069693" string(12) "212988069693"...

  • vc want an array just the size of the string? or want an array with the value and size?

  • I will try to explain better, I have the phone field where it has several formats need to standardize them, so I know which edit would need to pick the size of the phone field ex: size 12, if it has 12 characters delete the first character and play a mask and return the formatted phone to the JSON display in my Datatable.

  • That explode() what does it do? the simplest if all is right is to count the direct characters in sql and dispatch the json. I put an example for you to understand the exit: https://repl.it/ThunderousNauticalRingtailedlemur

  • @rray in case the explode() is turning my array into string, if I remove the same and pass only foreach() it does not return me anything. I will take a look at your example, thank you.

  • @rray good, when I use mb_strlen() it returns me an error GET &#xA;http://localhost:8080/projeto1/lostCall/data.php [HTTP/1.0 500 Internal Server Error 39 ms] so I just left strlen(), also could not return only the field Array phone, I had to pass the entire query, the obtained result was this link I noticed that it is also duplicating the values, the size would be stored in the variable $item ?

  • I got it, guys, I’m going edit the question with the solution, thank you all, if anyone has any opinion on Cod I am accepting.

Show 7 more comments
No answers

Browser other questions tagged

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