<dfn id="w48us"></dfn><ul id="w48us"></ul>
  • <ul id="w48us"></ul>
  • <del id="w48us"></del>
    <ul id="w48us"></ul>
  • 如何獲取PHP數(shù)組的鍵與值呢

    時(shí)間:2024-10-25 23:05:53 PHP 我要投稿
    • 相關(guān)推薦

    如何獲取PHP數(shù)組的鍵與值呢

      array_keys($array);//獲取數(shù)組(字典)的所有鍵值,返回一個(gè)鍵值數(shù)組。

      array_values($array)://獲取數(shù)組的所有value值,飯回一個(gè)數(shù)組。

      <?php

      $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; //注明:value不帶雙引號(hào)時(shí),其值只能是數(shù)字。【!!!注意:大括號(hào)兩邊只能是單引號(hào),不能是雙引號(hào)】

      帶雙引號(hào)時(shí),可為任意字符。如:{"id":"1","location":"漢字","oxygen":"3.33","negative":"23.2","humidity":"22","temp":"2.33","pm":"6"}

      //print_r("

    ");

      //var_dump(json_decode($json));

      //var_dump(json_decode($json, true));

      //print_r(json_decode($json));

      //print_r(json_decode($json, true));

      //print_r("");

      $arr1=json_decode($json,true);

      print_r($arr1);

      print_r(array_keys($arr1));

      echo array_keys($arr1)[1];

      echo array_values($arr1)[2];

      while(list($key, $value) = each($arr1))

      {

      echo "$key === $value
    ";

      }

      ?>

      ////從同名txt文本中讀取json字符串,并轉(zhuǎn)換成數(shù)組

      ?php

      //header("Content-Type: text/html; charset=gb2312");

      header("content-Type: text/html; charset=utf-8");//字符編碼設(shè)置

      $name = basename(__file__,".php");

      $file_path = $name.".txt";

      if(file_exists($file_path))

      {

      $str = file_get_contents($file_path);//將整個(gè)文件內(nèi)容讀入到一個(gè)字符串中

      //$str = str_replace(" ","
    ",$str);

      $str = str_replace(" ","",$str);

      //echo $name;

      //echo $file_path;

      echo $str;

      }

      //去掉bom頭

      function rmBOM($string)

      {

      if (substr($string, 0,3) == pack('CCC',0xef,0xbb,0xbf))

      {

      $string = substr($string, 3);

      }

      return $string;

      }

      //echo rmBOM($str);

      $str=rmBOM($str);

      $str2arr = json_decode($str,true);

      print_r($str2arr);

      ?>

      <?php

      //php 二維數(shù)組 知道keyvalue 怎么得到對應(yīng)的value

      function getValueByKey($arr, $key) {

      foreach($arr as $k => $v) {

      if ($k == $key) {

      return $v;

      }

      }

      return '';

      }

      $arr = array('a' => 1, 'b' => 2);

      $result = getValueByKey($arr, 'b');

      echo "$result";

      ?>

      獲取根據(jù)value值獲取key

      <?php

      $array = array(

      'fruit1' => 'apple',

      'fruit2' => 'orange',

      'fruit3' => 'grape',

      'fruit4' => 'apple',

      'fruit5' => 'apple');

      // this cycle echoes all associative array

      // key where value equals "apple"

      while ($fruit_name = current($array)) {

      if ($fruit_name == 'apple') {

      echo key($array).'
    ';

      }

      next($array);

      }

      ?>

      本文實(shí)例講述了PHP獲取數(shù)組的鍵與值方法。分享給大家供大家參考。具體如下:

      使用數(shù)組的過程中經(jīng)常要遍歷數(shù)組。通常需要遍歷數(shù)組并獲得各個(gè)鍵或值(或者同時(shí)獲得鍵和值),所以毫不奇怪,PHP為此提供了一些函數(shù)來滿足需求。許多函數(shù)能完成兩項(xiàng)任務(wù),不僅能獲取當(dāng)前指針位置的鍵或值,還能將指針移向下一個(gè)適當(dāng)?shù)奈恢谩?/p>

      獲取當(dāng)前數(shù)組鍵 key()

      key()函數(shù)返回input_array中當(dāng)前指針?biāo)谖恢玫逆I。其形式如下:

      mixed key(array array)

      下面的例子通過迭代處理數(shù)組并移動(dòng)指針來輸出$fruits數(shù)組的鍵:

    1
    2
    3
    4
    5
    6
    7
    $fruits = array("apple"=>"red", "banana"=>"yellow");
    while ($key = key($fruits)) {
       printf("%s <br />", $key);
       next($fruits);
    }
    // apple 
    // banana

      注意,每次調(diào)用key()時(shí)不會(huì)移動(dòng)指針。為此需要使用next()函數(shù),這個(gè)函數(shù)的唯一作用就是完成推進(jìn)指針的任務(wù)。

      獲取當(dāng)前數(shù)組值 current()

      current()函數(shù)返回?cái)?shù)組中當(dāng)前指針?biāo)谖恢玫臄?shù)組值。其形式如下:

      mixed current(array array)

      下面修改前面的例子,這一次我們要獲取數(shù)組值:

    1
    2
    3
    4
    5
    6
    7
    $fruits = array("apple"=>"red", "banana"=>"yellow");
    while ($fruit = current($fruits)) {
       printf("%s <br />", $fruit);
       next($fruits);
    }
    // red 
    // yellow

      獲取當(dāng)前數(shù)組鍵和值 each()

      each()函數(shù)返回input_array的當(dāng)前鍵/值對,并將指針推進(jìn)一個(gè)位置。其形式如下:

      array each(array array)

      返回的數(shù)組包含四個(gè)鍵,鍵0和key包含鍵名,而鍵1和value包含相應(yīng)的數(shù)據(jù)。如果執(zhí)行each()前指針位于數(shù)組末尾,則返回false。

    1
    2
    3
    $fruits = array("apple", "banana", "orange", "pear");
    print_r ( each($fruits) );
    // Array ( [1] => apple [value] => apple [0] => 0 [key] => 0 )

      each() 經(jīng)常和 list() 結(jié)合使用來遍歷數(shù)組。本例與上例類似,不過循環(huán)輸出了整個(gè)數(shù)組:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $fruits = array("apple", "banana", "orange", "pear");
    reset($fruits);
    while (list($key, $val) = each($fruits))
    {
       echo "$key => $val<br />";
    }
    // 0 => apple
    // 1 => banana
    // 2 => orange
    // 3 => pear

      因?yàn)閷⒁粋(gè)數(shù)組賦值給另一個(gè)數(shù)組時(shí)會(huì)重置原來的數(shù)組指針,因此在上例中如果我們在循環(huán)內(nèi)部將 $fruits 賦給了另一個(gè)變量的話將會(huì)導(dǎo)致無限循環(huán)。

      這就完成了數(shù)組的遍歷。

    【如何獲取PHP數(shù)組的鍵與值呢】相關(guān)文章:

    php數(shù)組函數(shù)序列之a(chǎn)rray-combine() - 數(shù)組合并函數(shù)的代碼03-31

    PHP獲取今天開始和結(jié)束的時(shí)間戳03-18

    如何學(xué)好PHP知識(shí)03-30

    新手如何學(xué)習(xí)PHP語言03-29

    淺析通如何加強(qiáng)php的安全03-06

    php如何過濾危險(xiǎn)html代碼03-30

    如何利用ajax獲取博文列表03-18

    如何在cmd下面寫php代碼01-22

    jQuery(js)如何獲取文字寬度(顯示長度)03-29

    主站蜘蛛池模板: 无码人妻精品一区二区三区66| 国内精品久久久久影院一蜜桃| 亚洲国产精品一区二区三区久久 | 精品午夜福利在线观看| 久草视频在线这里精品| 亚欧乱色国产精品免费视频 | 热综合一本伊人久久精品| 99精品视频在线观看re| 无码精品人妻一区| 影视网欧洲精品| 久久精品中文无码资源站| 精品欧美激情在线看| 国产夫妇精品自在线| 亚洲精品无码专区久久久| 国产亚洲精品a在线观看| 国产精品1024香蕉在线观看| 亚洲级αV无码毛片久久精品| 精品人妻一区二区三区毛片| 久久亚洲精品视频| 国产乱码精品一区二区三区中文| 亚洲AV成人精品日韩一区18p | 97视频在线观看这里只有精品 | 欧美精品亚洲精品日韩1818| 亚洲精品白浆高清久久久久久| 国产综合精品蜜芽| 国产成人精品久久一区二区三区| 亚洲av无码精品网站| 国产午夜亚洲精品国产成人小说 | 亚洲精品无码专区2| 四虎在线精品视频一二区| 爽爽精品dvd蜜桃成熟时电影院| 国产精品自产拍在线18禁| 99精品国产自在现线观看| 合区精品中文字幕| 国产精品99精品视频网站| 无码国内精品人妻少妇| 久久精品中文字幕有码| 亚洲国产精品久久久久久| 精品人妻中文字幕有码在线| 精品久久久久久无码免费| 欧美日韩精品乱国产538|