Forum Moderators: coopster
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
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?
>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.
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 :)
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
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
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