dongtou2097 2019-01-10 14: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 14: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.

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘