“PHP Dapatkan Kunci Array” Kode Jawaban

php semua kunci dalam array

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

$keys = array_keys($array);		// return array
$values = array_values($array);	// return array
?>
Strange Shrew

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

PHP Dapatkan Kunci Array


<?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).'<br />';
    }
    next($array);
}
?>

Matteoweb

Jawaban yang mirip dengan “PHP Dapatkan Kunci Array”

Pertanyaan yang mirip dengan “PHP Dapatkan Kunci Array”

Lebih banyak jawaban terkait untuk “PHP Dapatkan Kunci Array” di PHP

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya