“Cara Mendapatkan Kunci Array di PHP” Kode Jawaban

Dapatkan Kunci Nilai Array PHP


<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?>

Kaotik

PHP Dapatkan kunci dan nilai dari array

foreach($yourArray as $key => $value) {
    echo '<a href="' . $value . '">' . $key . '</a>';
}

// OR

//Use functions
key($array);     //Returns current key
reset($array);   //Moves array pointer to first record
current($array); //Returns current value
next($array);    //Moves array pointer to next record and returns its value
prev($array);    //Moves array pointer to previous record and returns its value
end($array);     //Moves array pointer to last record and returns its value
Lazurite

PHP Keys of Array

$array = array(0 => 100, "cor" => "vermelho");
print_r(array_keys($array));

$array = array("azul", "vermelho", "verde", "azul", "azul");
print_r(array_keys($array, "azul"));

$array = array("cor"     => array("azul", "vermelho", "verde"),
               "tamanho" => array("pequeno", "medio", "grande"));
print_r(array_keys($array));
Fernando Gunther

Dapatkan Kunci Elemen Array PHP

$people = array(
  2 => array(
    'name' => 'John',
    'fav_color' => 'green'
  ),
  5=> array(
    'name' => 'Samuel',
    'fav_color' => 'blue'
  ));
$found_key = array_search('blue', array_column($people, 'fav_color'));
Andrew Lautenbach

Nilai Kunci Array PHP

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
foreach($age as $x=>$x_value)
  {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
  }
?>
Lonely Lemur

Cara Mendapatkan Kunci Array di PHP


<?php
$array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple');

// this cycle echoes all associative array
// key where value equals "apple"
for($i = 0; $i< sizeof($array);$i++){
if (key($array[$i]) == 'apple') {
        echo key($array).'<br />';
    }
    //next($array);
}
?>

Successful Shrike

Jawaban yang mirip dengan “Cara Mendapatkan Kunci Array di PHP”

Pertanyaan yang mirip dengan “Cara Mendapatkan Kunci Array di PHP”

Lebih banyak jawaban terkait untuk “Cara Mendapatkan Kunci Array di PHP” di PHP

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya