Home > Backend Development > PHP Tutorial > Simple entry case for PHP array

Simple entry case for PHP array

小云云
Release: 2023-03-20 21:16:02
Original
2065 people have browsed it

Arrays can store one or more values ​​in separate variable names. This article mainly shares with you a simple entry-level case of PHP arrays, hoping to help you.

<?php
    //普通声明
    $arr = array(&#39;aaa&#39;,&#39;bbbb&#39;,&#39;ccccc&#39;);    echo $arr[0],&#39;<br>&#39;;    echo $arr[2],&#39;<br>&#39;,&#39;<br>&#39;;    $arr1[] = &#39;xxxx&#39;;    $arr1[] = &#39;yyyyy&#39;;
    print_r($arr1);    //键值对方式声明数组。
    $kv = array(&#39;name&#39;=>&#39;张三&#39;,&#39;sex&#39;=>&#39;男&#39;,&#39;age&#39;=>18);    echo $kv[&#39;name&#39;],&#39;<br>&#39;;    echo $kv[&#39;sex&#39;],&#39;<br>&#39;;    echo $kv[&#39;age&#39;],&#39;<br>&#39;;    //数组声明二
    $skv[&#39;x&#39;] = &#39;xxxx&#39;;    $skv[&#39;y&#39;] = &#39;yyyyy&#39;;
    print_r($skv);    echo &#39;<br>&#39;;    //特殊情况:字符串‘10’ 和 数字10 在数组中会转换成数字10 。 
    $test[&#39;10&#39;] = &#39;张三&#39;;    $test[10] = &#39;丽丽&#39;;    $test[&#39;10.0&#39;] = &#39;隔壁&#39;;//加小数点后的字符串不会被转换成数字。***
    //小数会取整后,都是9 同样会覆盖。
    $test[9.12] = &#39;老王&#39;;    $test[9.14] = &#39;丽丽&#39;;    //&#39;&#39; 在数组中等同意 null 也会被覆盖
    $test[&#39;&#39;] = &#39;马云&#39;;    $test[null] = &#39;丽丽&#39;;
    print_r($test);    //通过 extract 函数 将数组中的变量,导出成对象和值:
    extract($kv);    echo &#39;<br>&#39;,$name,&#39;<br>&#39;,$sex,&#39;<br>&#39;,$age;?>
Copy after login

The output is as follows:

aaa
cccccArray ( [0] => xxxx [1] => yyyyy ) 张三
男18Array ( [x] => xxxx [y] => yyyyy ) 
Array ( [10] => 丽丽 [10.0] => 隔壁 [9] => 丽丽 [] => 丽丽 )
张三
男18
Copy after login

Related recommendations:

Summary of PHP array sorting method

PHP arrays and characters Methods for converting between strings

php Definition and usage of array function current()

The above is the detailed content of Simple entry case for PHP array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template