當前位置:才華齋>設計>網頁設計>

如何獲取PHP陣列的鍵與值呢

網頁設計 閱讀(3.16W)

array_keys($array);//獲取陣列(字典)的所有鍵值,返回一個鍵值陣列。

如何獲取PHP陣列的鍵與值呢

array_values($array)://獲取陣列的所有value值,飯回一個數組。

<?PHP

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

帶雙引號時,可為任意字元。如:{"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字串,並轉換成陣列

?php

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

header("content-Type: text/html; charset=utf-8");//字元編碼設定

$name = basename(__file__,"");

$file_path = $name."";

if(file_exists($file_path))

{

$str = file_get_contents($file_path);//將整個檔案內容讀入到一個字串中

//$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 二維陣列 知道keyvalue 怎麼得到對應的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";

?>

獲取根據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);

}

?>

本文例項講述了PHP獲取陣列的鍵與值方法。分享給大家供大家參考。具體如下:

使用陣列的過程中經常要遍歷陣列。通常需要遍歷陣列並獲得各個鍵或值(或者同時獲得鍵和值),所以毫不奇怪,PHP為此提供了一些函式來滿足需求。許多函式能完成兩項任務,不僅能獲取當前指標位置的鍵或值,還能將指標移向下一個適當的`位置。

獲取當前陣列鍵 key()

key()函式返回input_array中當前指標所在位置的鍵。其形式如下:

mixed key(array array)

下面的例子通過迭代處理陣列並移動指標來輸出$fruits陣列的鍵:

1234567$fruits= array("apple"=>"red", "banana"=>"yellow");while($key= key($fruits)) {printf("%s <br />", $key);next($fruits);}// apple// banana

注意,每次呼叫key()時不會移動指標。為此需要使用next()函式,這個函式的唯一作用就是完成推進指標的任務。

獲取當前陣列值 current()

current()函式返回陣列中當前指標所在位置的陣列值。其形式如下:

mixed current(array array)

下面修改前面的例子,這一次我們要獲取陣列值:

1234567$fruits= array("apple"=>"red", "banana"=>"yellow");while($fruit= current($fruits)) {printf("%s <br />", $fruit);next($fruits);}// red// yellow

獲取當前陣列鍵和值 each()

each()函式返回input_array的當前鍵/值對,並將指標推進一個位置。其形式如下:

array each(array array)

返回的陣列包含四個鍵,鍵0和key包含鍵名,而鍵1和value包含相應的資料。如果執行each()前指標位於陣列末尾,則返回false。

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

each() 經常和 list() 結合使用來遍歷陣列。本例與上例類似,不過迴圈輸出了整個陣列:

12345678910$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

因為將一個數組賦值給另一個數組時會重置原來的陣列指標,因此在上例中如果我們在迴圈內部將 $fruits 賦給了另一個變數的話將會導致無限迴圈。

這就完成了陣列的遍歷。