1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Dear Visitor, we are presently having a membership drive and as a result EVERY new member that registers an account and makes THREE meaningful posts will have a mighty $10,000$ forum credits added to their wallets ... GET YOURS NOW!
    Dismiss Notice
  3. IMSpot.Org is under new management and we look forward to bringing you a range of exciting enhancements in coming days.
    Dismiss Notice
  4. Dear Visitors, why not register an account now and say hello ... we really would love to hear from you?
    Dismiss Notice
  5. Welcome to IMSpot.Org an internet marketing forum and very friendly place for discussion, learning and help in your online endeavours ... please consider joining our internet marketing community and sharing your knowledge with our members.
    Dismiss Notice

Converting Variables into Arrays (And Vice Versa)

Discussion in 'PHP' started by beingchinmay, Dec 28, 2015.

Tags:
  1. beingchinmay

    beingchinmay An Active Forum Member

    Joined:
    Jun 24, 2015
    Messages:
    28
    Likes Received:
    1
    Sometimes you want the information in an array stored in variables that you can use in PHP statements. Or you need variables converted to array elements. For example, you might want to perform the same operation on a
    bunch of variables, such as add 1 to each variable value. If you convert the variables into elements of an array, you can use one foreach statement to access the variable values one at a time, rather than write a bunch of statements to access each variable separately.

    Using the extract statement, you can retrieve all the values from an array, and insert each value into a variable, by using the key for the variable name. In other words, each array value is copied into a variable named for the key. For example, the following statements get all the information from an array and echo it:

    The output for these statements is the following:

    Conversely, you can also convert a group of simple variables into an array by using a compact statement that copies the value from each specified variable name into an array element. The use of the compact statement is, shown in the following statements:



    As you can see, the names of the variables are used as the keys.
    Notice that two different methods are used in the compact statement to specify the variables that make up the array:

    First method: You can use the variable names directly, as strings. The two variables color1 and color2 in the example show this method.
    Second method: You use an array that contains the names of the variables. In the previous code, $arrayIn contains the variable names: a and b. Then in the compact statement, the array name is used to add the
    variables to the array.


    You can use either method. If you have only a few variables to compact into an array, the first method of just using the variable names is probably fine.However, if you have a lot of variables to include, you may prefer putting the names into an array first, and then using the array in the compact statement.
     
Loading...
Live Thread addon by sonnb

Share This Page