Home  >  Article  >  Backend Development  >  Android programmers learn PHP development (19)-Basic concepts and definitions of arrays (1)-PhpStorm

Android programmers learn PHP development (19)-Basic concepts and definitions of arrays (1)-PhpStorm

黄舟
黄舟Original
2017-03-03 09:26:351170browse


PHP’s arrays are much more powerful than arrays in other languages. Let’s take a look at the basic concepts of arrays and how they are defined:




<?php
    /**
     * 数组
     * 直接赋值声明数组,如下:
     * $arr = array("one"=>"111111","two"=>"222222");
     * 其中,one、two是key(键名),111111、222222是value(值、也叫键值)
     */

    echo "---------- 索引数组 : 下标是整数 ----------<br>";
    $arr[0] = 1;
    $arr[1] = 2;
    $arr[2] = 3;
    print_r($arr); // 打印结果:Array ( [0] => 1 [1] => 2 [2] => 3 )
    echo "<br>";

    echo "---------- 关联数组 : 下标是字符串 ----------<br>";
    $arr1[&#39;one&#39;] = 1;
    $arr1[&#39;two&#39;] = 2;
    $arr1[&#39;three&#39;] = 3;
    print_r($arr1); // 打印结果:Array ( [one] => 1 [two] => 2 [three] => 3 )
    echo "<br>";

    echo "---------- 数组 Demo ----------<br>";
    $arr2 = $arr + $arr1;
    print_r($arr2);
    echo "<br>";

    echo "---------- 数组 Demo 2 ----------<br>";
    $arr3[] = 1;
    $arr3[] = 2;
    $arr3[] = 3;
    $arr3[] = 4;
    print_r($arr3);
    echo "<br>";

    /**
     *     <pre class="brush:php;toolbar:false">标签 函数的结果格式化输出,即,按原型打印。阅读起来就比较方便。
     *     echo &#39;<pre class="brush:php;toolbar:false">&#39;;
     *     print_r($arr);
     *     echo &#39;
'; */ echo "---------- 数组 Demo 3 ----------
"; $arr4 = array(); for ($i=0; $i<10; $i++){ $arr4[] = $i * $i; } echo '
&#39;;
    print_r($arr4);
    echo &#39;
'; /* 打印结果: Array ( [0] => 0 [1] => 1 [2] => 4 [3] => 9 [4] => 16 [5] => 25 [6] => 36 [7] => 49 [8] => 64 [9] => 81 ) */ echo "---------- 数组 Demo 4 ----------
"; $arr5 = array(); for ($i=0; $i<10; $i++){ //echo $i."
"; if ($i == 4){ $arr5['刘德华'] = "欧巴"; } if ($i == 7){ $arr5[-100] = 6666; } $arr5[] = $i * $i; } echo '
&#39;;
    print_r($arr5);
    echo &#39;
'; /* 打印结果: Array ( [0] => 0 [1] => 1 [2] => 4 [3] => 9 [刘德华] => 欧巴 [4] => 16 [5] => 25 [6] => 36 [-100] => 6666 [7] => 49 [8] => 64 [9] => 81 ) */ /** * 以下是几种定义数组的方式: */ echo "---------- 数组 Demo 5 ----------
"; $arr6 = array(1,2,3,4,5,6); $arr7 = array("one","two","three"); $arr8 = array(0=>"aaa",1=>"bbb",2=>"ccc"); $arr9 = array("aaa",6=>"bbb","ccc"); $arr10 = array("name"=>"wang","age"=>20);

The above is the content of Android programmers learning PHP development (19) - Basic concepts and definition methods of arrays (1) - PhpStorm. For more related content, please Follow the PHP Chinese website (www.php.cn)!

Statement:
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