Array question

Array question

am 25.08.2003 08:58:59 von disko_kex

------=_NextPart_000_0064_01C36AE7.2050ED30
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

If I have an array like this > [0] => 1 [1] => 2 [2] => 3 ... [99] =>
100

If I want to select [50] => 51 and store the value that's 2 positions
before and 1 position after, how can I do?

If I want to know what position value 71 have?


I have search the PHP-manual and found some functions as prev, next, pos
etc. But with these commands I have to loop thru the array every time to
reach the the values and positions. It has to be an easier way to do it.

// jocke

------=_NextPart_000_0064_01C36AE7.2050ED30--

RE: Array question

am 25.08.2003 09:33:14 von Warren Vail

in my experience, using numeric indices to your array, you can deposit and
reference occurances as follows; however your reference is not a valid
array definition;

[0] => 1 [1] => 2 [2] => 3 ... [99] => 100 (I don't believe these are valid
as a definition)

numeric indices would be defined as follows;

array(0 => 1, 1 => 2, 2 => 3 .... 99 => 100);

$x = $dataarray[50];

$dataarray[50 - 2] = $x;
$dataarray[50 + 1] = $x;

if($dataarray[71] == 74) do this....

On the other hand, if the indices is alpha-numeric string representations of
numbers,

array("0" => 1, "1" => 2, "2" => 3 ... "99" => 100)

you will need to use some other tricks to access them.

hope this is what you are looking for, you used several terms like "select"
and "know the value" of that are somewhat ambiguous ;-)

Warren Vail
warren@vailtech.net


-----Original Message-----
From: Disko_kex [mailto:disko_kex@swedish-mushroom.com]
Sent: Sunday, August 24, 2003 11:59 PM
To: php-windows@lists.php.net
Subject: [PHP-WIN] Array question


If I have an array like this > [0] => 1 [1] => 2 [2] => 3 ... [99] =>
100

If I want to select [50] => 51 and store the value that's 2 positions
before and 1 position after, how can I do?

If I want to know what position value 71 have?


I have search the PHP-manual and found some functions as prev, next, pos
etc. But with these commands I have to loop thru the array every time to
reach the the values and positions. It has to be an easier way to do it.

// jocke

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Array question

am 25.08.2003 10:38:41 von Sek-Mun Wong

hi again jocke... seems i'll give a go at answering your questions again ;-)

If I understand your question properly, I doubt the previous poster's
response would be useful to you.

The bad news (I think): there are limited functions that deal with array
pointers. There are plenty to deal with arrays themselves.

as you've noted: pos, next, prev seem to be the only way to deal with a
pointer, so yes, the way that I implemented my solution was to use next and
pos to get the positioning and in doing so, reading the *entire array*
(yuck) and storing into a new array based on conditions.

option 1) I haven't tried, so I'm not sure if array_search (for key) and
in_array (for value) move the pointer - maybe you could try those. (gut feel
is they don't). If they do, then you can for loop a prev/next to get what
you want.

option 2) I guess you could actually use integers as your index and have
them sort-ed (or array_push them on) that might solve your problem, but I
think that's a bit of a kludge. (ie, implicitly using the array key value as
a pointer)

option 3) So.... the only way I found was to loop thru the whole array, it's
messy for large sets, but if your array size is only 100 (I doubt it is) it
may be ok. It seems excessive though.

looping thru the entire set might not be too bad, I assume that's what an
array_search might be doing as well(?) so you're not saving any more time
even if that works.

Anyhow, tell me if you find another method... i'll go off and do more
research on it now too.

"Disko_kex" wrote in message
news:006301c36ad6$5cc81d30$e900a8c0@lokalguirdcfee...
> If I have an array like this > [0] => 1 [1] => 2 [2] => 3 ... [99] =>
> 100
>
> If I want to select [50] => 51 and store the value that's 2 positions
> before and 1 position after, how can I do?
>
> If I want to know what position value 71 have?
>
>
> I have search the PHP-manual and found some functions as prev, next, pos
> etc. But with these commands I have to loop thru the array every time to
> reach the the values and positions. It has to be an easier way to do it.
>
> // jocke
>

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Array question

am 25.08.2003 10:55:25 von disko_kex

Well its not exactly what I was looking for but you gave me some ideas,
but still got this small problem:

If I know the value in the array, say for example 49. Then I want to
store the previous and next value in the array? I know the other way
when I have the position, just use pos($dataarray) then like you sad
$dataarray($key+1), that's easy. But the other way????


-----Original Message-----
From: Warren Vail [mailto:warren@vailtech.net]
Sent: den 25 augusti 2003 09:33
To: Disko_kex; php-windows@lists.php.net
Subject: RE: [PHP-WIN] Array question

in my experience, using numeric indices to your array, you can deposit
and
reference occurances as follows; however your reference is not a valid
array definition;

[0] => 1 [1] => 2 [2] => 3 ... [99] => 100 (I don't believe these are
valid
as a definition)

numeric indices would be defined as follows;

array(0 => 1, 1 => 2, 2 => 3 .... 99 => 100);

$x = $dataarray[50];

$dataarray[50 - 2] = $x;
$dataarray[50 + 1] = $x;

if($dataarray[71] == 74) do this....

On the other hand, if the indices is alpha-numeric string
representations of
numbers,

array("0" => 1, "1" => 2, "2" => 3 ... "99" => 100)

you will need to use some other tricks to access them.

hope this is what you are looking for, you used several terms like
"select"
and "know the value" of that are somewhat ambiguous ;-)

Warren Vail
warren@vailtech.net


-----Original Message-----
From: Disko_kex [mailto:disko_kex@swedish-mushroom.com]
Sent: Sunday, August 24, 2003 11:59 PM
To: php-windows@lists.php.net
Subject: [PHP-WIN] Array question


If I have an array like this > [0] => 1 [1] => 2 [2] => 3 ... [99] =>
100

If I want to select [50] => 51 and store the value that's 2 positions
before and 1 position after, how can I do?

If I want to know what position value 71 have?


I have search the PHP-manual and found some functions as prev, next, pos
etc. But with these commands I have to loop thru the array every time to
reach the the values and positions. It has to be an easier way to do it.

// jocke

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Array question

am 25.08.2003 10:57:03 von Joachim

// This is how I would do it. The first question is easily solved by putting
a mathematical
// expression inside the array brackets.

$selectIndex = 50; // The index you're interested in

$selectedValue = yourArray[$select];
$selectedValueMinus2 = yourArray[$select-2];
$selectedValuePlus1 = yourArray[$select+1];

// The second problem would force me to loop through the array, though.

$selectValue = 71; // The value you want to look for

$sizeOfArray = count($yourArray); // Number of elements in your array

// Search through the array for the value you want, break when found
for($i=0;$i<$sizeOfArray;$i++){
if($yourArray[$i]==$selectValue)
break;
}

// $i contains the position of the value in the array

// Lycka till!

// Joachim

-----Ursprungligt meddelande-----
Fran: Disko_kex [mailto:disko_kex@swedish-mushroom.com]
Skickat: den 25 augusti 2003 08:59
Till: php-windows@lists.php.net
Amne: [PHP-WIN] Array question


If I have an array like this > [0] => 1 [1] => 2 [2] => 3 ... [99] =>
100

If I want to select [50] => 51 and store the value that's 2 positions
before and 1 position after, how can I do?

If I want to know what position value 71 have?


I have search the PHP-manual and found some functions as prev, next, pos
etc. But with these commands I have to loop thru the array every time to
reach the the values and positions. It has to be an easier way to do it.

// jocke

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Array question

am 26.08.2003 06:45:26 von Warren Vail

I am beginning to see, I think, what you are dealing with, the pos()
function (and others) are used to step thru arrays moving a cursor pointer
as you go, and logically would be connected with next() and prev() to step
forward and backward. This is more logical to use when the keys assigned
are not consecutive (0, 1, 2, 3, etc).

Also sounds like you want to insert occurrences between occurrences already
in the array. I think you used the term store. This can be done with php
arrays (as in perl) when the associated key is actually a data item. There
may be other ways, but I always test for the presence of an occurrence by;

if(isset($datatable[$key])) update occurance...
else $datatable[$key] = insert new occurance...

PHP arrays can also be multi-dimensional;

$datatable[$key] = array($column1, $column2);

and, if it make sense all new entries can be added to the end of an array

$datatable[] = array($column1, $column2);

and resorted to place the entire array into order.

sort($datatable);

php multi-dimensional arrays can also be referrenced a multitude of ways. I
frequently resort to references like

$datatable[$rowno][$colno] = insert value;

$recallvalue = $datatable[$rowno][$colno];

This may not answer your questions, but perhaps give you a few more ideas.
Just try a few to learn more.

good luck,

Warren Vail
warren@vailtech.net


-----Original Message-----
From: Disko_kex [mailto:disko_kex@swedish-mushroom.com]
Sent: Monday, August 25, 2003 1:55 AM
To: 'Warren Vail'; php-windows@lists.php.net
Subject: RE: [PHP-WIN] Array question


Well its not exactly what I was looking for but you gave me some ideas,
but still got this small problem:

If I know the value in the array, say for example 49. Then I want to
store the previous and next value in the array? I know the other way
when I have the position, just use pos($dataarray) then like you sad
$dataarray($key+1), that's easy. But the other way????


-----Original Message-----
From: Warren Vail [mailto:warren@vailtech.net]
Sent: den 25 augusti 2003 09:33
To: Disko_kex; php-windows@lists.php.net
Subject: RE: [PHP-WIN] Array question

in my experience, using numeric indices to your array, you can deposit
and
reference occurances as follows; however your reference is not a valid
array definition;

[0] => 1 [1] => 2 [2] => 3 ... [99] => 100 (I don't believe these are
valid
as a definition)

numeric indices would be defined as follows;

array(0 => 1, 1 => 2, 2 => 3 .... 99 => 100);

$x = $dataarray[50];

$dataarray[50 - 2] = $x;
$dataarray[50 + 1] = $x;

if($dataarray[71] == 74) do this....

On the other hand, if the indices is alpha-numeric string
representations of
numbers,

array("0" => 1, "1" => 2, "2" => 3 ... "99" => 100)

you will need to use some other tricks to access them.

hope this is what you are looking for, you used several terms like
"select"
and "know the value" of that are somewhat ambiguous ;-)

Warren Vail
warren@vailtech.net


-----Original Message-----
From: Disko_kex [mailto:disko_kex@swedish-mushroom.com]
Sent: Sunday, August 24, 2003 11:59 PM
To: php-windows@lists.php.net
Subject: [PHP-WIN] Array question


If I have an array like this > [0] => 1 [1] => 2 [2] => 3 ... [99] =>
100

If I want to select [50] => 51 and store the value that's 2 positions
before and 1 position after, how can I do?

If I want to know what position value 71 have?


I have search the PHP-manual and found some functions as prev, next, pos
etc. But with these commands I have to loop thru the array every time to
reach the the values and positions. It has to be an easier way to do it.

// jocke

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Array Question

am 17.08.2007 19:06:15 von Trevor Gryffyn

I assume you're getting fields with the same name from pulling data from two or more tables. Something like "SELECT * FROM TABLE1, TABLE2 WHERE TABLE1.id = TABLE2.id" or something like that.

At any rate, if you can.. it's always good to name the fields you want to pull instead of using "SELECT *". I know it's a pain sometimes, but in this case it may be somewhat necessary.

What you can do is use an alias for the duplicate field name. Something like this:

"SELECT TABLE1.id as id1, TABLE2.id as id2 FROM...."

Then in your array, it'll be $mydata['id1'] and $mydata['id2']

Works the same with a single table pull and I usually alias the table names too:

SELECT t1.id id1, t2.id id2 FROM TABLE1 t1, TABLE2 t2 WHERE t1.id = t2.id

(can't remember if you can use the alias in the WHERE to make it "id1 = id2" or not)

The "as" is usually optional, but sometimes nice to keep things clear. In some flavors of SQL it's required.

-TG

= = = Original message = = =

I have pull a bunch of data from an SQL database through a query. The problem I am running into is I pull two fields with the exact same name. How do I grab the right info? I have been using mydata ~ the array name. mydata['contactID'] Should I instead use the array value so mydata[8]?




___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php