Results 1 to 9 of 9

Thread: storing array values

  1. #1
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default storing array values

    Hi

    Can anybody help for the following issue

    I have upto 100 integer values in varialbe $i
    I want to store these values in single array name called year..

    something like that $year = array();

    Can any one help for this

    Regards
    Tom

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,734
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    Take a look at the explode() function. If that does not help then please post the variable and the data and I or someone will be able to help you out a little more.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default

    Hi James

    thanks for your message

    Explode() is exploding in this issue

    Please refer the my code

    PHP Code:
    <?php

    $start 
    ;

    $end 15;

    for(
    $i=start$i<=end$i++)
    {
        
    $i
    }
    $i displays numbers - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    I want to store these numbers in an array called $numbers
    Last edited by traq; 02-10-2013 at 06:22 AM. Reason: please format your code and use the forum's [PHP][/PHP] BBCode tags

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by letom View Post
    Explode() is exploding in this issue
    what?

    Quote Originally Posted by letom View Post
    Please refer the my code
    PHP Code:
    <?php
    $start 
    ;
    $end 15;
    for(
    $i=start$i<=end$i++){
        
    $i
    }
    $i displays numbers - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    I want to store these numbers in an array called $numbers
    ...your code seems incomplete. Are you intending to create the variable $i in this loop?
    Using $i both to control the loop and create your result will have unexpected consequences.

    In addition, it seems to conflict with what you said in your original post:

    Quote Originally Posted by letom View Post
    I have upto 100 integer values in varialbe $i
    I want to store these values in single array name called year..
    If
    PHP Code:
    print $i
    results in

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15


    ...then you don't have "integers" - you have a string.

    If you want to convert that string into an array, you can use the explode() function as james suggested:
    PHP Code:
    <?php

    $i 
    "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15";

    $numbers explode' ',$i );
    # now you have an array containing the numbers 1 - 15
    If you don't actually need that string $i for anything else, I'd suggest using range() to create your array instead:
    PHP Code:
    <?php

    $numbers 
    range1,15 );
    # same result
    If neither of these are what you're asking about, please clarify.

  5. The Following User Says Thank You to traq For This Useful Post:

    letom (02-11-2013)

  6. #5
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by traq View Post
    what?


    ...your code seems incomplete. Are you intending to create the variable $i in this loop?
    Using $i both to control the loop and create your result will have unexpected consequences.

    In addition, it seems to conflict with what you said in your original post:



    If
    PHP Code:
    print $i
    results in

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15


    ...then you don't have "integers" - you have a string.

    If you want to convert that string into an array, you can use the explode() function as james suggested:
    PHP Code:
    <?php

    $i 
    "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15";

    $numbers explode' ',$i );
    # now you have an array containing the numbers 1 - 15
    If you don't actually need that string $i for anything else, I'd suggest using range() to create your array instead:
    PHP Code:
    <?php

    $numbers 
    range1,15 );
    # same result
    If neither of these are what you're asking about, please clarify.


    Hi There

    Thanks for your message and valuable advices.

    Your Question --

    ...your code seems incomplete. Are you intending to create the variable $i in this loop?
    Using $i both to control the loop and create your result will have unexpected consequences.

    In addition, it seems to conflict with what you said in your original post:
    --------------------------------------------------------------------------------------
    Answer
    If iam going to write the full coding , in common sense iam missing the valuable time of me and people like you readers. that why i shortened it into a small code, as the coded form is already understand for you.

    Your Question --
    Explode() is exploding in this issue
    what?
    --------------------------------------------------------------------------
    Answer
    Exploding (means) - To show to be false or unreliable. http://www.thefreedictionary.com/exploding
    I refer it with my program output , which is incorrect while using explode() function.

    Your Question -
    If neither of these are what you're asking about, please clarify.
    -------------------------------------------------------------------------
    Answer
    The issue is solved when the range function is used.. Thanks for your kind and valuable suggestions

    See u again

  7. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by letom View Post
    If iam going to write the full coding , in common sense iam missing the valuable time of me and people like you readers. that why i shortened it into a small code, as the coded form is already understand for you.
    While I understand your goal, it is always much more helpful to give a working example.

    Yes, it is a good idea to remove irrelevant code from your samples.
    However, in order to be useful, the code that you do show us needs to demonstrate not only the part that is the problem, but also the part that works.
    In this case, removing portions of your code only added to the confusion by implying other (false) problems.

    Quote Originally Posted by letom View Post
    Exploding (means) - To show to be false or unreliable.
    I refer it with my program output , which is incorrect while using explode() function.
    Alright. How is it being unreliable or incorrect? What was the result it gave you, vs. what you expected?
    The statement "it doesn't work" will not lead to a solution on its own.

    Quote Originally Posted by letom View Post
    The issue is solved when the range function is used.. Thanks for your kind and valuable suggestions
    You're quite welcome, I'm glad you got it figured out.

    If your question has been answered, please mark your thread "resolved":
    • On your original post (post #1), click [edit], then click [go advanced].
    • In the "thread prefix" box, select "Resolved".
    • Click [save changes].

  8. #7
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default

    Thanks..

    False refers wrong or incorrect.. I got incorrect output while using explode() fn.

    It would be nice, if readers will focus & discuss about the meaning and idea of post, not each and every words of post

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

    Default

    The reason we expect clear questions is because it makes it easier to help you. This is all for you, not for us. And although it may feel excessive, the details really are important. We can't guess what you are thinking, so your posts need to be clear enough for us to understand all of the important details. If we don't understand them, we can't help.
    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

  10. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by letom View Post
    Thanks..

    False refers wrong or incorrect.. I got incorrect output while using explode() fn.

    It would be nice, if readers will focus & discuss about the meaning and idea of post, not each and every words of post
    My focus was on your issue. To illustrate, if explode() had given you this result:
    Code:
    bool( false )
    the solution would be very different than if you had gotten this result:
    Code:
    array(1) 
        [0] => 
        string(36) "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
    As Daniel says, we need to know the specifics of your problems, otherwise we're just making wild guesses.

Similar Threads

  1. Resolved Array Values/Other Values
    By bluewalrus in forum JavaScript
    Replies: 8
    Last Post: 12-12-2010, 11:59 PM
  2. Looking for values in an array
    By JasonDFR in forum PHP
    Replies: 7
    Last Post: 02-10-2009, 08:22 AM
  3. Replies: 10
    Last Post: 09-03-2008, 09:07 PM
  4. comparing array values?
    By abs0lut in forum PHP
    Replies: 1
    Last Post: 08-18-2008, 02:56 AM
  5. pushing values in array ??
    By PHPycho in forum JavaScript
    Replies: 1
    Last Post: 07-03-2007, 12:05 PM

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
  •