Avatar ·

How to add elements to an empty array in PHP? Need help for a beginner

Hello everyone! I just started learning PHP and encountered a problem when working with arrays. I need to create an empty array and then dynamically add elements to it, for example, from user input or database query results. Here's what I'm trying to do: How do I correctly add new values to the end of an array? I've heard about the array_push() function, but I'm not sure if that's the only way. I'm also interested in whether I can add elements with a specified key (associative arrays). Please explain with simple examples how to do this.