dongyi2993 2017-01-23 13:28
浏览 27
已采纳

打印具有给定字符串的匹配键的所选数组元素

I have a first array with:

$first_array = Array
(
    "1" => "Loading dock",
    "2" => "Forklift"
);

and, a second string with:

either case 1:

$second_string = 1;

or case 2:

$second_string = 2;

or case 3:

$second_string = 1,2;

I want to print value of

  • first array

in which matched with the keys of first array with value of second string.

I have tried this:

$first_array [$second_string];

It works fine with case 1 & 2 for case 3 I need help....!

  • 写回答

1条回答 默认 最新

  • douchuntang2827 2017-01-23 15:22
    关注

    If you need such situation why not to try something like this:

    $first_array = Array
    (
    "1" => "Loading dock",
    "2" => "Forklift"
    );
    
    // $new_array = []; // use this if you want to make new array
    $second_string = [1];
    // $second_string = [1,2];
    
    if(is_array($second_string)) {
      foreach($second_string as $k=>$s) {
         // $new_array[$s] = $first_array[$s];
         echo $first_array[$s]; // if you only want to print value
      }
    }
    

    Send your second_string in terms of array which will be best at this situation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度