开发者

How to print array index? [closed]

开发者 https://www.devze.com 2023-03-11 00:34 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad开发者_StackOverflow社区, or rhetorical andcannot be reasonably answered in its curr
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad开发者_StackOverflow社区, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I want to print the array index for some purpose. Can anybody tell me about this ?


Get the first index of an elemnt:

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
echo $key;

For all elemnts

foreach($array as $key => $value){
    echo "{$key} => {$value}\n";
}

will output

0 => blue
1 => red
2 => green
3 => red

All keys:

echo implode(', ',array_keys($array));

will output

 0, 1, 2, 3


not sure what you mean with this question but you could try

echo "<pre>";
print_r ($arr);
echo "</pre>";

that way you'll see your entire array including all the indexes


if you mean the keys of the array:

$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

//result
Array
(
    [0] => 0
    [1] => color
)

This will provide an array of the keys

0

精彩评论

暂无评论...
验证码 换一张
取 消