PHP获得数组值和数组键

时间:2021-06-29 22:07:55

I want to run a for loop through an array and create anchor elements for each element in the array, where the key is the text part and the value is the URL.

我想在数组中运行for循环并为数组中的每个元素创建锚点元素,其中键是文本部分,值是URL。

How can I do this please?

我该怎么做呢?

Thank you.

谢谢你!

4 个解决方案

#1


43  

This should do it

这个应该怎么做

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

Edit: As per Capsule's comment - changed to single quotes.

编辑:根据胶囊的评论-更改为单引号。

#2


23  

For some specific purposes you may want to know the current key of your array without going on a loop. In this case you could do the following:

对于某些特定的目的,您可能想要知道数组的当前键,而不需要执行循环。在这种情况下,你可以做以下事情:

reset($array);
echo key($array) . ' = ' . current($array);

The above example will show the Key and the Value of the first record of your Array.

上面的示例将显示数组的第一个记录的键和值。

The following functions are not very well known but can be pretty useful in very specific cases:

下面的函数不是很有名,但在非常具体的情况下是非常有用的:

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

#3


2  

Like this:

是这样的:

$array = array(
    'Google' => 'http://google.com',
    'Facebook' => 'http://facebook.com'
);

foreach($array as $title => $url){
    echo '<a href="' . $url . '">' . $title . '</a>';
}

#4


1  

In a template context, it would be:

在模板上下文中,它将是:

<?php foreach($array as $text => $url): ?>
    <a href="<?php echo $url; ?>"><?php echo $text; ?></a>
<?php endforeach; ?>

You shouldn't write your HTML code inside your PHP code, hence avoid echoing a bunch of HTML.

您不应该在PHP代码中编写HTML代码,因此避免重复一大堆HTML。

This is not filtering anything, I hope your array is clean ;-)

这不是过滤任何东西,我希望你的数组是干净的;

#1


43  

This should do it

这个应该怎么做

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

Edit: As per Capsule's comment - changed to single quotes.

编辑:根据胶囊的评论-更改为单引号。

#2


23  

For some specific purposes you may want to know the current key of your array without going on a loop. In this case you could do the following:

对于某些特定的目的,您可能想要知道数组的当前键,而不需要执行循环。在这种情况下,你可以做以下事情:

reset($array);
echo key($array) . ' = ' . current($array);

The above example will show the Key and the Value of the first record of your Array.

上面的示例将显示数组的第一个记录的键和值。

The following functions are not very well known but can be pretty useful in very specific cases:

下面的函数不是很有名,但在非常具体的情况下是非常有用的:

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

#3


2  

Like this:

是这样的:

$array = array(
    'Google' => 'http://google.com',
    'Facebook' => 'http://facebook.com'
);

foreach($array as $title => $url){
    echo '<a href="' . $url . '">' . $title . '</a>';
}

#4


1  

In a template context, it would be:

在模板上下文中,它将是:

<?php foreach($array as $text => $url): ?>
    <a href="<?php echo $url; ?>"><?php echo $text; ?></a>
<?php endforeach; ?>

You shouldn't write your HTML code inside your PHP code, hence avoid echoing a bunch of HTML.

您不应该在PHP代码中编写HTML代码,因此避免重复一大堆HTML。

This is not filtering anything, I hope your array is clean ;-)

这不是过滤任何东西,我希望你的数组是干净的;