How to print on screen or console, a string array in php

Asked

Viewed 155 times

0

I’ve been going through the documentation, but it’s unclear how I do it. I have an array of strings:

$remetente = $con->real_escape_string($_REQUEST['rem']);
$arrai= array();
$remetenteArray[] = str_split($remetente, 1);

As you can see, the $remetenteArray[] is an array of strings, and these strings have only one character each. What I need is to iterate over that array and print on screen only the numbers that there is in it, and save them in a string.

I’ll create a regular expression to get the numbers, but the problem right now is how to print these strings that make up the array...

  • 1

    str_split turns a string into an array of letters, to iterate on each letter. What is the content of $remetente ? Or you want to print only the letters that are numbers ?

  • I want to save in an array only strings that are numbers.

  • But what you got $remetente ? And what did you like that $remetenteArray[] had ?

1 answer

1


What you want is to individually check each element of the array. I mean, it would be something like this:

for ($i=(int)0; $i<count($remetenteArray); $i++){
    print ">".$remetenteArray[$i]."< ";
}

I guess you must have tried something like that and then got something like:

PHP Notice:  Array to string conversion in ..

Turns out you made a mistake defining the array:

$remetenteArray[] = str_split($remetente, 1);

By placing the opens and closes brackets you have created a array within the other, that is, the first item of it contains the array resulting from the function str_split() then just remove them the loop will work properly.

But as what you want is to return only the numbers of string within the array, there is not even the need to make this whole juggling, create a function that receives the string and spit out the array desired.

function onlyNumbers($str){
        $result=[];
        for($i=0; $i<strlen($str); $i++){
                $j = substr($str, $i,1);
                if ($j>="0" and $j<="9") $result[]=$j;
        }
        return $result;
}

Thus:

$remetente = "0 A 1 B 2 C 3 D 4";
print_r(onlyNumber($remetente));

Will result in:

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
)

In fact, it was thanks to print_r() I discovered your mistake. :-)

  • Show! But those numbers I extracted from the string array, I would like them to form a single String, not an array.

  • I will use this number set $sender in a mysqli query: mysqli_query($con,"INSERT INTO forum (codUsuario,title,message) VALUES('$result','$title','$message');");

  • I tried to use the implode() function to convert the n[umeros array into a single String, but php keeps accusing "array to string Conversion" in the query line...

  • 1

    If it’s supposed to be one thing, change the $result=[] for $result="" at the beginning of the function and the $result[]=$j for $result=.$j; and the result will be a single string.

Browser other questions tagged

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