Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 24

Thread: Dynamically change a variables name

  1. #11
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I edited it, I misunderstood you at first. Try this one.
    There are also simple functions, like array_merge(), but I'm not sure that's customizable enough for your current purposes.
    Not as far as I'm aware.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  2. #12
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    Tried it, still no luck.

    Although I noticed that if the first value of $id is "100" and $qty is "6"

    it will print
    Array ( [0] => 600 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 )

    Whereas if the first value of $id is "100" and $qty is "2"

    it will print
    Array ( [0] => 200 [1] => 0 )

  3. #13
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Still looks like you're using the old code to me.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #14
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    oops, yeah I had both in there!

    The new one is much better although it prints

    Array ( [0] => 100 [1] => 100 [2] => 100 [3] => 100 [4] => 100 [5] => 100 )

    Which is almost what i want but without the "[0] =>"

    but instead the value just seperated with commas

    Array ('100','100','100','100','100','100')

  5. #15
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That's harder because PHP doesn't output that automatically.

    function array_out($a) {
    echo 'Array (\'';
    echo implode('\',\'',$a);
    echo '\')';
    }
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #16
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    Do you know how i'd put that in relation to this: ?

    PHP Code:
    $cart = array();
    foreach(
    $id as $k => $v)
    for(
    $i 0$i $qty[$k]; ++$i)
    $cart[] = $v
    I'm assuming that i'd replace $a with $cart?
    e.g.

    PHP Code:
    function array_out($cart) {
    echo 
    'Array (\'';
    echo 
    implode('\',\'',$cart);
    echo 
    '\')';


  7. #17
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Why are you printing an array like that? The string representation of an array is only meant for debugging purposes.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #18
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    function name($argument) { ....stuff.... }

    That creates a function.

    For $cart, you'd use array_out($cart); and that would execute the commands in it.

    Alternatively, you could just use the three lines from inside the function and replace $a in the second with $cart, as you did in the last code block (but then you don'tneed the first or last line)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #19
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    Perfect, thanks once again fellas!

    My final code ended up like this:

    PHP Code:
    $temp = array();
    foreach(
    $id as $k => $v)
    for(
    $i 0$i $qty[$k]; ++$i)
    $temp[] = $v;    

    $cart "array ('"implode("','",$temp)."')"

  10. #20
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That confused me for a minute because it lacks brackets. I'd add them in for clarity.

    foreach {
    for {
    ...
    }}
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •