php 不带循环和带循环的数组的回显键和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3406726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:37:48  来源:igfitidea点击:

echo key and value of an array without and with loop

phparraysecho

提问by esafwan

This is an array i have

这是我有的数组

<?php
$page['Home']='index.html';
$page['Service']='services.html';
?>

How do i get to echo something like this for individual one like

我如何才能为个人像这样回声

Home is at index.html

and again how can i do this through a loop and echo all?

再次我如何通过循环来做到这一点并回显所有?

回答by Mchl

foreach($page as $key => $value) {
  echo "$key is at $value";
}

For 'without loop' version I'll just ask "why?"

对于“无循环”版本,我只会问“为什么?”

回答by Alix Axel

Without a loop, just for the kicks of it...

没有循环,只是为了它的开始......



You can either convert the array to a non-associative one, by doing:

您可以通过执行以下操作将数组转换为非关联数组:

$page = array_values($page);

And then acessing each element by it's zero-based index:

然后通过从零开始的索引访问每个元素:

echo $page[0]; // 'index.html'
echo $page[1]; // 'services.html'


Or you can use a slightly more complicated version:

或者您可以使用稍微复杂一点的版本:

$value = array_slice($page, 0, 1);

echo key($value); // Home
echo current($value); // index.html

$value = array_slice($page, 1, 1);

echo key($value); // Service
echo current($value); // services.html

回答by kennytm

If you must not use a loop (why?), you could use array_walk,

如果你不能使用循环(为什么?),你可以使用array_walk

function printer($v, $k) {
   echo "$k is at $v\n";
}

array_walk($page, "printer");

See http://www.ideone.com/aV5X6.

请参阅http://www.ideone.com/aV5X6

回答by Your Common Sense

for the first question

对于第一个问题

$key = 'Home';
echo $key." is at ".$page[$key];

回答by avi

Echo key and value of an array without and with loop

不带循环和带循环的数组的回显键和值

$array = array(
            'kk6NFKK'=>'name',
            'nnbDDD'=>'claGg',
            'nnbDDD'=>'kaoOPOP',
            'nnbDDD'=>'JWIDE4',
            'nnbDDD'=>'lopO'
         );


print_r(each($array));  

Output

输出

Array
(
    [1] => name
    [value] => name
    [0] => kk6NFKK
    [key] => kk6NFKK
)

回答by Mark Baker

function displayArrayValue($array,$key) {
   if (array_key_exists($key,$array)) echo "$key is at ".$array[$key];
}

displayArrayValue($page, "Service"); 

回答by cloud

How to echo key and value of an array without and with loop

如何在没有循环和有循环的情况下回显数组的键和值

$keys = array_keys($page);
implode(',',$keys);
echo $keys[0].' is at '.$page['Home'];

回答by Karol Samborski

My version without a loop would be like this:

我没有循环的版本是这样的:

echo implode(
    "\n", 
    array_map(
         function ($k, $v) { 
             return "$k is at $v"; 
         }, 
         array_keys($page), 
         array_values($page)
    )
);

回答by Omair

array_walk($v, function(&$value, $key) {
   echo $key . '--'. $value;
 });

Learn more about array_walk

了解有关array_walk 的更多信息

回答by Shivanand Sharma

A recursive function for a change;) I use it to output the media information for videos etc elements of which can use nested array / attributes.

用于更改的递归函数;) 我使用它来输出视频等元素的媒体信息,这些元素可以使用嵌套数组/属性。

function custom_print_array($arr = array()) {
    $output = '';
    foreach($arr as $key => $val) {
        if(is_array($val)){
            $output .= '<li><strong>' . ucwords(str_replace('_',' ', $key)) . ':</strong><ul class="children">' . custom_print_array($val) . '</ul>' . '</li>';
        }
        else {
            $output .=  '<li><strong>' . ucwords(str_replace('_',' ', $key)) . ':</strong> ' . $val . '</li>';
        }
    }
    return $output;

}

}