Log in

View Full Version : storing array values



letom
02-09-2013, 11:45 AM
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

james438
02-09-2013, 04:39 PM
Take a look at the explode() (http://php.net/manual/en/function.explode.php) 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.

letom
02-10-2013, 06:23 AM
Hi James

thanks for your message

Explode() is exploding in this issue

Please refer the my code


<?php

$start = 1 ;

$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

traq
02-10-2013, 07:33 AM
Explode() is exploding in this issue
what?


Please refer the my code
<?php
$start = 1 ;
$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:


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

If
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() (http://php.net/explode) function as james suggested:
<?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() (http://php.net/range) to create your array instead:
<?php

$numbers = range( 1,15 );
# same result

If neither of these are what you're asking about, please clarify.

letom
02-10-2013, 08:07 AM
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
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() (http://php.net/explode) function as james suggested:
<?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() (http://php.net/range) to create your array instead:
<?php

$numbers = range( 1,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

traq
02-10-2013, 07:19 PM
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.


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.


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].

letom
02-11-2013, 06:30 AM
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

djr33
02-11-2013, 06:37 AM
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.

traq
02-11-2013, 03:08 PM
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() (http://php.net/explode) had given you this result:
bool( false )the solution would be very different than if you had gotten this result:
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.