Is it possible without using foreach to add key-value to the every array element? I need to add at the end of all arrays type => image . I have array like this:
array(5) {
[0]=>
array(7) {
["idfile"]=>
string(2) "71"
["iduser"]=>
string(1) "1"
["name"]=>
string(60) "11745552_873843849371628_3215645663726390853_n-57335133dee48"
["extension"]=>
string(3) "jpg"
["isImage"]=>
string(1) "1"
["is_deleted"]=>
string(1) "0"
["add_date"]=>
string(19) "2016-05-11 15:35:15"
}
[1]=>
array(7) {
["idfile"]=>
string(2) "72"
["iduser"]=>
string(1) "1"
["name"]=>
string(19) "black-57335133e1ded"
["extension"]=>
string(3) "jpg"
["isImage"]=>
string(1) "1"
["is_deleted"]=>
string(1) "0"
["add_date"]=>
string(19) "2016-05-11 15:35:15"
}
[2]=>
array(7) {
["idfile"]=>
string(2) "73"
["iduser"]=>
string(1) "1"
["name"]=>
string(21) "Canon_1-57335133e4394"
["extension"]=>
string(3) "jpg"
["isImage"]=>
string(1) "1"
["is_deleted"]=>
string(1) "0"
["add_date"]=>
string(19) "2016-05-11 15:35:15"
}
}
You can use array_walk() and define the first argument of your callback as reference, but I assume foreach is a bit faster.
array_walk($array, function (&$value) {
$value['type'] = 'image';
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.