通过索引

时间:2017-08-09 07:47:22

标签: php arrays

我不是一名php开发人员,并且不幸地在努力解决问题。

这是我的目标: -

"prx_replacement" => array(
        "ComforTone" => array(
            'img' => '/content/images/prx/comfortone.jpg',
            'description' => '<em>ComforTone</em>: Reduce acoustic noise and enhance MR patient experience'
        ),
        "ScanWise Implant" => array(
            'img' => '/content/images/prx/scanwise-implant.jpg',
            'description' => '<em>ScanWise Implant</em>: A key to confidence with MR Conditional implants'
        )
    )

我的索引值为$index

我需要使用索引号动态访问img值,但无法管理它。

$lang['prx_replacement']["ComforTone"]["img"]

有效但

$lang['prx_replacement'][$index]["img"]不会。

我的代码:

foreach( $case_solutions as $index => $solution ){
<img src="<?php echo $base_url.$lang['prx_replacement']["ComforTone"]["img"]?>" />
}

感谢任何帮助,谢谢!

3 个答案:

答案 0 :(得分:1)

您需要使用?>foreach内输出原始HTML。

foreach( $case_solutions as $index => $solution ){ 
    ?>
    <img src="<?php echo $base_url.$lang['prx_replacement'][$index]["img"]?>" />
    <?php
}

或者对于简单的单行,您可以使用echo

foreach( $case_solutions as $index => $solution ){ 
    echo '<img src="' . $base_url.$lang['prx_replacement'][$index]["img"] . '"';
}

答案 1 :(得分:1)

你必须这样做: -

<?php

$lang_array  = array(
    "prx_replacement" => array(
        "ComforTone" => array(
            'img' => '/content/images/prx/comfortone.jpg',
            'description' => '<em>ComforTone</em>: Reduce acoustic noise and enhance MR patient experience'
        ),
        "ScanWise Implant" => array(
            'img' => '/content/images/prx/scanwise-implant.jpg',
            'description' => '<em>ScanWise Implant</em>: A key to confidence with MR Conditional implants'
        )
));

$lang_array = array_values($lang_array['prx_replacement']);

foreach( $case_solutions as $index => $solution ){?>
    <img src="<?php echo $base_url.$lang_array[$index]['img'];?>" />
<?php }?>

输出: - https://eval.in/843261(在浏览器上会显示图片,不用担心)

答案 2 :(得分:1)

您需要使用$case_solutions["prx_replacement"]

更深地循环一个级别

所以,你的代码看起来像是:

<?php 
foreach( $case_solutions['prx_replacement'] as $index => $solution ){ ?>
  <img src="<?php echo $base_url.$lang['prx_replacement']["$index"]["img"]?>" />
<?php } ?>