在php中处理每个键和值。

时间:2021-08-26 22:09:06

I have a $row array that will print the below array

我有一个$row数组,它将打印下面的数组。

        Array
        (
        [BookCode] => 124
        [BookName] => Book1
        )

        Array
        (
        [BookCode] => 123
        [BookName] => Book2
        )

        ...........

I have a code like this:

我有一个这样的代码:

    <?php foreach($row as $key=>$value){  ?>
    <a href="process.php?bcode=<?php echo $value['BookCode'] ?>"><span style="color:red;font-weight:bold;"><?php echo $value; ?></span></a>
    <?php } ?>

But I'm not able to get the BookCode in my anchor tag since I'm using foreach with key-value. In my case I have to use foreach only (as intructed by my client)

但是我不能在我的锚标记中获得书签,因为我使用的都是键值。在我的情况下,我只能使用foreach(由我的客户输入)

So how can I get the value inside the anchor tag?

那么如何才能在锚标记内获取值呢?

2 个解决方案

#1


1  

Your foreach loop is not returning what you expect—you have an array of associative arrays.

您的foreach循环并没有返回您所期望的,您有一个关联数组的数组。

<? foreach ( $row as $column ): ?>

  <a href="process.php?bcode=<?=$column['BookCode']?>">

<? endforeach; ?>

In the above, the link will be process.php?bcode=124

在上面,链接将是process.php?bcode=124。

Alternatively, if you really want to use the $key=>$value:

或者,如果你真的想使用$key=>$value:

<? foreach ( $row as $column ): ?>

  <? foreach ( $column as $key=>$value ) ?> // here $key = 'BookCode'
    <a href="process.php?bcode=<?=$value?>">
  <? endforeach; ?>

<? endforeach; ?>

#2


1  

Try foreach like this. It may help to you

foreach这样的尝试。这对你有帮助。

foreach($row as $data)
{?>
    <a href="....<?php echo $data['Bookcode']; ?>"></a>
<?php } ?>

#1


1  

Your foreach loop is not returning what you expect—you have an array of associative arrays.

您的foreach循环并没有返回您所期望的,您有一个关联数组的数组。

<? foreach ( $row as $column ): ?>

  <a href="process.php?bcode=<?=$column['BookCode']?>">

<? endforeach; ?>

In the above, the link will be process.php?bcode=124

在上面,链接将是process.php?bcode=124。

Alternatively, if you really want to use the $key=>$value:

或者,如果你真的想使用$key=>$value:

<? foreach ( $row as $column ): ?>

  <? foreach ( $column as $key=>$value ) ?> // here $key = 'BookCode'
    <a href="process.php?bcode=<?=$value?>">
  <? endforeach; ?>

<? endforeach; ?>

#2


1  

Try foreach like this. It may help to you

foreach这样的尝试。这对你有帮助。

foreach($row as $data)
{?>
    <a href="....<?php echo $data['Bookcode']; ?>"></a>
<?php } ?>