Results 1 to 3 of 3

Thread: [resolved] split string to array, then foreach

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    Resolved [resolved] split string to array, then foreach

    Hello all.

    I am posting a string, $_POST['hotels'], which is: "2,33,22,20,21"

    I would like to split that string, at the comma, into an array, converting each to a number, then doing a foreach.

    I was thinking:
    Code:
    $hotels = split(',',$_POST['hotels']);
    
    foreach ($hotels as $x) {
    	//convert $x to a number
    	//do something with $x
    }
    But I'm not doing it right.
    Any help is greatly appreciated.
    Many thanks.
    Last edited by solitario; May 27th, 2005 at 02:52 PM.

  2. #2
    Fanatic Member ALL's Avatar
    Join Date
    Jul 2004
    Location
    192.168.1.1
    Posts
    711

    Re: split string to array, then foreach

    well, i am no wiz at php, but cant strings be used as a number value as well? i know CGI it is like that.

    so couldnt you do a calculation like this:
    Code:
    $hotels = split(',',$_POST['hotels']);
    echo $hotels."*5=";
    $a = $hotels[0] * 4;
    echo $a;
    that should take the first array of $hotels and multiply it by 4 then echo the result.
    Please support one of my projects?
    TKForums.com

    Web Forum
    JavaScript Wiki
    ________________________
    If somone helps you, please rate their post, by clicking the to rate their post

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    Re: split string to array, then foreach

    Thanks (again) ALL. That works perfectly.

Posting Permissions

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



Click Here to Expand Forum to Full Width