繁体   English   中英

将键和值添加到 PHP 中数组的每个元素

[英]Adding Key and value to each element of an array in PHP

我一直在尝试做一件简单的事情,我想为数组的每个索引添加一个键和值,即我有一个这样的数组

   [0] => Array
        (
            [student_id] => 1
            [class_id] => 1
            [student_grno] => 11198
            [student_name] => Iqra Gabol
            [father_name] => Umer Ishaq Gabol
            [student_email] => iqra.gabol11198@habibschools.edu.pk
            [father_email] => 
        )

    [1] => Array
        (
            [student_id] => 2
            [class_id] => 1
            [student_grno] => 11199
            [student_name] => Ayehsa Mughal 
            [father_name] => Zahid Iqbal Mughal 
            [student_email] => ayesha.mughal11199@habibschools.edu.pk
            [father_email] => zim2love@hotmail.com

        )

    [2] => Array
        (
            [student_id] => 3
            [class_id] => 1
            [student_grno] => 11202
            [student_name] => Eisha Fahim
            [father_name] => Fahim Ahmed 
            [student_email] => eisha.fahim11202@habibschools.edu.pk
            [father_email] => fahimeisha@gmail.com
        )

    [3] => Array
        (
            [student_id] => 4
            [class_id] => 1
            [student_grno] => 11204
            [student_name] => Shaanzeh Lodhi
            [father_name] => Sufyan Lodhi
            [student_email] => shaanzeh.lodhi11204@habibschools.edu.pk
            [father_email] => sufyan.lodhi@gmail.com

        )

    [4] => Array
        (
            [student_id] => 5
            [class_id] => 1
            [student_grno] => 11205
            [student_name] => Unaiza Hussain Syed
            [father_name] => Syed Mustafa Hussain
            [student_email] => unaiza.hussain11205@habibschools.edu.pk
            [father_email] => syeda.mustafa.hussain@pk.pwc.com
        )


)

foreach 元素我想为每个学生获得分数并想像这样添加到他们

[0] => Array
    (
        [student_id] => 1
        [class_id] => 1
        [student_grno] => 11198
        [student_name] => Iqra Gabol
        [father_name] => Umer Ishaq Gabol
        [student_email] => iqra.gabol11198@habibschools.edu.pk
        [father_email] => 
        [mark_id]      => 7
    )

[1] => Array
    (
        [student_id] => 2
        [class_id] => 1
        [student_grno] => 11199
        [student_name] => Ayehsa Mughal 
        [father_name] => Zahid Iqbal Mughal 
        [student_email] => ayesha.mughal11199@habibschools.edu.pk
        [father_email] => zim2love@hotmail.com
              [mark_id]      =>7
    )

[2] => Array
    (
        [student_id] => 3
        [class_id] => 1
        [student_grno] => 11202
        [student_name] => Eisha Fahim
        [father_name] => Fahim Ahmed 
        [student_email] => eisha.fahim11202@habibschools.edu.pk
        [father_email] => fahimeisha@gmail.com
         [mark_id]      => 2
    )

[3] => Array
    (
        [student_id] => 4
        [class_id] => 1
        [student_grno] => 11204
        [student_name] => Shaanzeh Lodhi
        [father_name] => Sufyan Lodhi
        [student_email] => shaanzeh.lodhi11204@habibschools.edu.pk
        [father_email] => sufyan.lodhi@gmail.com
        [mark_id]      => 12
    )

[4] => Array
    (
        [student_id] => 5
        [class_id] => 1
        [student_grno] => 11205
        [student_name] => Unaiza Hussain Syed
        [father_name] => Syed Mustafa Hussain
        [student_email] => unaiza.hussain11205@habibschools.edu.pk
        [father_email] => syeda.mustafa.hussain@pk.pwc.com
         [mark_id]      => 2
    )
)

mark_id 在循环中动态出现。

我使用了以下代码,但它只给了我最后一条记录。

foreach($arr as $key => &$val){
    $val['mark_id'] = get_marks_id();
}

请帮忙。 非常感谢

你能详细说明一下这个问题吗? 我尝试了您的代码,它对我来说非常有效。 这是我运行的代码:

function get_marks_id() {
    return rand(1,10);
}

$arr = [
    [
        'student_id' => 1,
        'class_id' => 1,
        'student_grno' => 11198,
        'student_name' => 'Iqra Gabol',
        'father_name' => 'Umer Ishaq Gabol',
        'student_email' => 'iqra.gabol11198@habibschools.edu.pk',
        'father_email' => '',
    ],
    [
        'student_id' => 2,
        'class_id' => 1,
        'student_grno' => 11199,
        'student_name' => 'Ayehsa Mughal',
        'father_name' => 'Zahid Iqbal Mughal',
        'student_email' => 'ayesha.mughal11199@habibschools.edu.pk',
        'father_email' => 'zim2love@hotmail.com',
    ],
    [
        'student_id' => 3,
        'class_id' => 1,
        'student_grno' => 11202,
        'student_name' => 'Eisha Fahim',
        'father_name' => 'Fahim Ahmed',
        'student_email' => 'eisha.fahim11202@habibschools.edu.pk',
        'father_email' => 'fahimeisha@gmail.com',
    ],
];

foreach($arr as $key => &$val){
    $val['mark_id'] = get_marks_id();
}

echo print_r($arr,1);

这打印:

Array
(
    [0] => Array
        (
            [student_id] => 1
            [class_id] => 1
            [student_grno] => 11198
            [student_name] => Iqra Gabol
            [father_name] => Umer Ishaq Gabol
            [student_email] => iqra.gabol11198@habibschools.edu.pk
            [father_email] =>
            [mark_id] => 10
        )

    [1] => Array
        (
            [student_id] => 2
            [class_id] => 1
            [student_grno] => 11199
            [student_name] => Ayehsa Mughal
            [father_name] => Zahid Iqbal Mughal
            [student_email] => ayesha.mughal11199@habibschools.edu.pk
            [father_email] => zim2love@hotmail.com
            [mark_id] => 1
        )

    [2] => Array
        (
            [student_id] => 3
            [class_id] => 1
            [student_grno] => 11202
            [student_name] => Eisha Fahim
            [father_name] => Fahim Ahmed
            [student_email] => eisha.fahim11202@habibschools.edu.pk
            [father_email] => fahimeisha@gmail.com
            [mark_id] => 8
        )

)

您的 get_marks_id() function 可能有问题吗?

而不是$val['mark_id'] ,它没有连接到 foreach 循环内的数组,而是像这样“直接”访问数组$arr[$key]

foreach($arr as $key => &$val){
    $arr[$key]['mark_id'] = get_marks_id();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM