Forum Moderators: coopster

Message Too Old, No Replies

Variable as an array

         

yoavr

2:06 am on Dec 31, 2009 (gmt 0)

10+ Year Member



I have the var:
$a = ".1.2.3.4.5.6.7.8.";
and I want that this var will be like the array:
$a = array(1,2,3,4,5,6,7,8);

but I don't know how can I get a "n" value from the var.
From the array I can get it by $a[n] ($a[0],$a[1]...), but from the var I don't know how to do that.
Each value can be identified by ".n.", but how can I actually choose the n value?
I tried to do that with substr and strpos functions, but that wasn't work.
Maybe the solution is with regex?

Thanks.

Psychopsia

2:17 am on Dec 31, 2009 (gmt 0)

10+ Year Member



Try this...
substr is to remove first and last dot, else you will get empty values for first and last keys.

$a = substr($a, 1, -1);

$arr = explode('.', $a);
print_r($arr);

yoavr

2:26 am on Dec 31, 2009 (gmt 0)

10+ Year Member



Yeah, but I want to do that without using array.. :)

Psychopsia

2:32 am on Dec 31, 2009 (gmt 0)

10+ Year Member



Why not?
Should be easier to use array...

yoavr

2:36 am on Dec 31, 2009 (gmt 0)

10+ Year Member



I know, but I need it as a string in variable, sorry.. :]

incrediBILL

2:41 am on Dec 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Unless I miss what you're trying to do in the example above you want:

b$ = substr( a$, N*2, 2);

where b$ contains basically a$[N]

Now whether you want the dots or not...

yoavr

2:54 am on Dec 31, 2009 (gmt 0)

10+ Year Member



The dots are for separating the numbers, and I can't do substr($a,N*2,2) because there can be other numbers also (it can be ".1.2.3.11.13.22.101.234.", sorry that I didn't note that) and then it won't work. So I put dots for identify the numbers. Hope you understand..

incrediBILL

3:07 am on Dec 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What's the purpose here?

Are you using it for a validation list to only allow the items in the list or do you want to be able to pull out elements by number [1] [2] etc?

Arrays will make your like much easier, you would've been done by now!

yoavr

3:15 am on Dec 31, 2009 (gmt 0)

10+ Year Member



I know that it is more easy using array, but I need to use a lot of arrays (huge number of them) so I prefer to use variables instead of the arrays, because when I use arrays the page loads very slowly.

TheMadScientist

4:45 am on Dec 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Could you elaborate a bit on what you are trying to do and how you will be using it if the following is not what you are looking for?

$b='.'.$SumNum.'.';
if(strpos($a,$b)!==FALSE) {
/* Do Stuff */
}

Would tell you if SumNum is in the string.

In looking closer, are you trying to find the Nth value?
Then you could just count the dots:

$a = ".10.2.315.407.51.6.179.824.";
$NthNum=4;

preg_match('#^(\.[^.]+){'.($NthNum-1).'}\.([^.]+)#',$a,$NumMatches);
$FoundMatch=array_pop($NumMatches);
echo $FoundMatch;

Should get you close...

Made some minor edits to the regex.

ADDED:

You could probably use a non-capturing pattern for the 1st grouping to be more efficient too...

preg_match('#^(?:\.[^.]+){'.($NthNum-1).'}\.([^.]+)#',$a,$NumMatches);
$FoundMatch=$NumMatches[1];
echo $FoundMatch;

yoavr

11:50 am on Dec 31, 2009 (gmt 0)

10+ Year Member



Thank you. I understand that there is no way to do that without array.. (preg_match..). Probably there is a way to use regex without array?

TheMadScientist

8:35 pm on Dec 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think you're worrying way too much about setting an array piece or two... I would think there would be very little difference in setting two array pieces like you are with the preg_match I posted and setting two variables. I would also guess where you were running into the slowdown is setting all the array pieces you needed to set... Think of each array piece as a variable, which is basically what it is. If you set a string variable for each number you use you would probably slow the page load time down too... Really setting a couple of array pieces or a couple of variables should be '6 of one or half dozen of the other' situation. Personally I'd worry much less about what they're called and more about how many are set in this situation, which is why I switched to a non-capturing pattern for the first grouping in the second regex I posted. You only set two array pieces that way.