Forum Moderators: coopster

Message Too Old, No Replies

Array in PHP

Multi level

         

ukgimp

11:22 am on May 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it possible to add another key to an array that has a key and one value:

eg
Array ( [1] => 7 [hello] => 3 [world] => 1 [test] => 1 [help] => 2 [2] => 2 )

So that a calculated value is inserted into to array corresponding to the key. If that does not make sense, I mean that I want to know if it is possible to divide the figures for each key by a certain number and insert that value back into the array.

struggling to get my head round this one :(

Cheers

Nick_W

11:24 am on May 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you mean like 5 => "WebmasterWorld" unless 5 already exists?

Nick

brotherhood of LAN

11:26 am on May 29, 2003 (gmt 0)

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



foreach($array as $key => $value)
{

}

you can get the keys from in there as you no doubt know, and you can divide/do math with them.

only prob it seems is if you have a dupe key if its going to be inserted in the same array?

Nick_W

11:28 am on May 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Array_key_exists() [php.net]

Nick

ukgimp

11:33 am on May 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>you can get the keys from in there as you no doubt know, and you can divide/do math with them

But how do I hurl them back into the array so that the values all match up so that the calculation of key[0] lines up in the right colomn. Essentiall leaving a 3 column array (key, value, calculated_value)

I hope that makes sense

Multidimensional Arrays?

brotherhood of LAN

11:40 am on May 29, 2003 (gmt 0)

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



array_push will put the new value in, which I'm sure you know too ;)

>Multidimensional Arrays?

yah if I'm following you, you'd want the calculated value to "line up" with the key still. If you do I'd put the calculated value as an array under the current key, or create a new array with the same keys, and you can merge them.

<<just unsure where you'd like the new calculated value to go.

ukgimp

11:52 am on May 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry for my ignorance and super confusing explanation, this is my first real foray in arrays :)

Array ( [1] => 7 [hello] => 3 [world] => 1 [test] => 1 [help] => 2 [2] => 2 )

The number 7 associated with key[0] is, lets say has 4 added to it. Then that calculation is done for each of the values in the array. That new value is then inputted into the array so that key[0] has both 7 and 11 associated with it.

Eg:

1 : 7 : 11
hello : 3 : 7
world : 1 : 4

I am not doing a good job of this. DG will have a heart attack when he reads my copy :)

mavherick

11:53 am on May 29, 2003 (gmt 0)

10+ Year Member



Multidimensional arrays?

I'd say yes.

array( "key1" => array( "value1" => 1, "calc1" => 3), ...)

then it's easy to work with it:

$calc = someArray["key1"]["value1"];
// some math
someArray["key1"]["calc1"] = $calc;

I used string keys in the second dimension but you could as well use the default 0, 1.

someArray["key1"][0] ...

mavherick

ukgimp

3:52 pm on May 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks all.

These multidimenional arrays are pretty damn cool. I have more of a clue tnow than I have in along while. They really are cool. I have used them before but not really understood what the hell I was doing.

Cheers

jatar_k

3:52 pm on May 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe, you're making it hard to help you ukgimp. ;)

depending on the starting data you can do this a bunch of different ways. It sounds like the gist of it is

take your array which has your key value pairs and walk it with a foreach/while/counter. Perform your math on each element and tack the calculated value onto the end of that row.

Multidimensional arrays are just like tables, they have columns and rows. All you're doing is adding a new column to your table. When you have one row you can access it by $arrayname['rownum']['colnum'].

Sorry if I am over simplifying and you are callng me names but I figured it might help you get it straight in your head. hth

aspr1n

4:50 pm on May 29, 2003 (gmt 0)

10+ Year Member



yeah I agree with jatar - I don't have the vagest idea of what you're trying to do ;)

In answer to your specific question:

That new value is then inputted into the array so that key[0] has both 7 and 11 associated with it.

Eg:

1 : 7 : 11
hello : 3 : 7
world : 1 : 4

No you can't, an array is comprised of a key/value pair, therefore only one value can be stored against a key.

You could a) store the value as a string 'a:b' and then separate them as requried. alternatively you'll need to store an array at the key thus:

$array[ 'hello' ] = array( 3, 7 );
$array[ 'world' ] = array( 1, 4 );

You would now refer to them as $array[ 'hello' ][ 0 ] (which would contain '3') $array[ 'hello' ][ 1 ] would contain '7'

On a side note I personally find it far less confusing to not mix associative and numerical indicies in the same dimension thus not have: $array[ 'hello' ] & $array[ 0 ]

I have no idea if this is of any use to you, but if you're short on loo roll - print my answer ;)

asp