Is the string an array?

Source: Internet
Author: User
Is the string an array?
In the following code, $STR also did not declare an array in advance to write the style of the arrays, so it is not too far-fetched, because the string can be an array, if it is OK, you can declare beforehand Ah, then the question came, this is exactly why
$str = "1234567890";
$s = ";

for ($i =0; $i <4; $i + +)
{
$k =mt_rand (1, strlen ($STR));
$s. = $str [$k-1];
}

------to solve the idea----------------------
String and array are different types, and strings can be accessed as arrays. For your example, $str [$k-1] is shorthand for substr ($STR, $k-1, 1), so you can use it.

But if this is the case, it doesn't work, so it's important to use the right type.

$str = "1234567890";
$str 1= "ABCDEFG";
$result = Array_diff ($str, $str 1); Error
Print_r ($result);

$str =array (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ');
$str 1=array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ');
$result = Array_diff ($str, $str 1); Normal
Print_r ($result);

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.