dpsr1670 2013-09-27 14:17
浏览 160
已采纳

使用两个数组在PHP中获取变量和值($ key = $ value)

$cfields = array_filter(explode(",", get_option('CComment_fields')));
$Ctlds = array_filter(explode(",", get_option('Ctlds')));

Sorry, I know this is a mess. Basically I end up with two arrays: $cVal & $Ctlds

The arrays:

$Cval = Array ( [0] => Fun Co [1] => 555-555-5555 [2] => test@test.com [3] => Tom [4] => Smith ) 

$Ctlds = Array ( [0] => user_company [1] => user_phone [2] => user_email [3] => user_firstname [4] => user_lastname )

I want the matching [NUMBER] value in $Ctlds to be the variable with the matching [NUMBER] in $Cval to be the value.

Example: user_company will be assigned the value Fun Co user_phone will be assigned the value 555-555-5555 etc..

This is the code I tried - bad, I know.

foreach ($Ctlds as $Ctlds){
        $cVal[] = $Ctlds[];
    }

I have tried code I know is waaaayy off. Any help would be appreciated. And for search reference this is USING TO ARRAYS TO CREATE VARIABLE (KEYS) AND VALUES IN PHP. Thank you so much.

  • 写回答

4条回答 默认 最新

  • douqiaosu0280 2013-09-27 14:29
    关注

    Use array_combine. That's exactly what it's for.

    $result = array_combine($Ctlds, $Cval);
    

    If you want them to be variables, you can then just extract that array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀