dongtou2097 2019-01-10 06:14
浏览 78
已采纳

PHP包含另一个文件,循环数组和每个记录的运行函数

I would like to access the values of an array in another php file ... loop over it and run a function over each record. I can't seem to access the values though ... I'm getting an internal error. How to properly do this? That's my setup:

contacts.php

<?php

$contacts_de = array(
    'name01' => array(
        'firstName' => 'FirstName01',
        'lastName' => 'LastName01',
        'gender' => 'm',
        'language' => 'de',
        'email' => 'email01'
    ),
    'name02' => array(
        'firstName' => 'FirstName02',
        'lastName' => 'LastName02',
        'gender' => 'f',
        'language' => 'de',
        'email' => 'email02'
    )
);

mail.php

<?php

include('contacts.php');

function renderContacts($arr) {
    global $lang,$contacts_de;
    $d = '';
    foreach($arr as $i) {
        if ($i['gender'] == 'm') {
            .$d = 'Mr. '.$i['firstName'].' '.$i['lastName']
        } else if ($i['gender'] == 'm') {
            .$d = 'Ms. '.$i['firstName'].' '.$i['lastName']
        }
    }
    echo $d;
}

renderContacts();

default.js

$('#sendbtn').on('click', function() {

    $.ajax({
        type: "POST",url: '/mail.php',
        success: function(response,textStatus,jqXHR) {
            console.log(response);

        },
        error: function (jqXHR, status, err) {
            console.log(err);
        }
    });

}); 

Desired Console.log

Mr. FirstName01 LastName01
Ms. FirstName02 LastName02

展开全部

  • 写回答

1条回答 默认 最新

  • douqian8238 2019-01-10 06:23
    关注

    The simple answer to what you're asking is:

    <?php # contacts.php
    
    $contacts = array(
        'name01' => array(
            'firstName' => 'FirstName01',
            'lastName' => 'LastName01',
            'gender' => 'm',
            'language' => 'de',
            'email' => 'email01'
        ),
        'name02' => array(
            'firstName' => 'FirstName02',
            'lastName' => 'LastName02',
            'gender' => 'f',
            'language' => 'de',
            'email' => 'email02'
        )
    );
    

    and

    <?php # whatever.php
    
    require __DIR__ . '/contacts.php';
    
    function render_contacts(array $contacts) {
        foreach ($contacts as $contact) {
            $prefix = $contact['gender'] == 'm' ? 'Mr' : 'Ms';
            printf("%s. %s %s
    ", $prefix, $contact['firstName'], $contact['lastName']);
        }
    }
    
    render_contacts($contacts);
    

    A non-separated sandbox can be seen here: http://sandbox.onlinephpfunctions.com/code/6daa0147671fcaac9c51fe919c4a8f916181fad1

    I've also cleaned up your code a little bit for you, removing things like the global keyword, some syntax errors, and the JavaScript you linked, as it's irrelevant to the issue.

    GL.

    展开全部

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

报告相同问题?

悬赏问题

  • ¥100 二维码被拦截如何处理
  • ¥15 怎么解决LogIn.vue中多出来的div
  • ¥15 优博讯dt50巴枪怎么提取镜像
  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部