Jump to content

Cant recall how the pointers to memorypoint work, any help?


NikkiLoveGod

Recommended Posts

Hi all!

 

I'm having a small problem here, I dont remember how did those pointer to some part of memory worked. Had something to do with &-sign, I think.

 

Here is my code and problem. I want to make this part here alot shorter and more efficient with the memorything(I think). I want this to be in ~2 foreaches, one for $_SESSION['mitat']and one for $_SESSION['kaltevuudet']. As the only thing that changes is the path to a different array, I want to make it so i can put it like "$path = $_SESSION['ulokkeet'][$indexi]['mitat'][$key]" ( note that there are two variables that are assigned by the foreach ). Is this possible, if it is, how?

 

<?php
if ( $_POST['uloke'] != '' ) {

	$uloke = $_POST['uloke'];
	$indexi = count($_SESSION['ulokkeet']);

	$_SESSION['ulokkeet'][$indexi]['id'] = $uloke;

	foreach($_SESSION['mitat'] as $key => $value) {
		$_SESSION['ulokkeet'][$indexi]['mitat'][$key] = str_replace(',', '.', $_POST[$key]);
		$$key = $_SESSION['paakatto']['mitat'][$key];
	}
	foreach($_SESSION['kaltevuudet'] as $key => $value) {
		$_SESSION['ulokkeet'][$indexi]['mitat'][$key] = str_replace(',', '.', $_POST[$key]);
		$$key = $_SESSION['paakatto']['mitat'][$key];
	}
}

elseif ( $_POST['uloke'] == '' ) {

	$_SESSION['paakatto']['mitat'] = NULL;

	foreach($_SESSION['mitat'] as $key => $value) {
		$_SESSION['paakatto']['mitat'][$key] = str_replace(',', '.', $_POST[$key]);
		$$key = $_SESSION['paakatto']['mitat'][$key];
	}
	foreach($_SESSION['kaltevuudet'] as $key => $value) {
		$_SESSION['paakatto']['mitat'][$key] = str_replace(',', '.', $_POST[$key]);
		$$key = $_SESSION['paakatto']['mitat'][$key];
	}
}
?>

 

ps. Sorry if it is a bit obfuscated :) I usually comment it when its done

 

I think I MAY be referring to variable variables that i am even using there, but during a few tests, this doesnt work:

 

<?php
    $_SESSION['test']['kala'] = 'LOL';

    $path = '$_SESSION["test"]["kala"]';

    $$path = 'test';

    echo $_SESSION["test"]["kala"];
?>

 

it still echoes 'LOL'. Nor does it work with $path = '_SESSION["test"]["kala"]';

 

Looks like I have something to learn here >_<

Of course its going to echo LOL that is still the value of your session index which you set on line 1. What your next two lines have done, will not affect the echo statement at the end because that's not what your echoing.

Try this:

<?php

    $_SESSION['test']['kala'] = 'LOL';

    $kala = &$_SESSION["test"]["kala"]; //Cretae pointer kala

    $kala = 'test';

    echo $_SESSION["test"]["kala"];

?>

 

THANK YOU, ysamu!

 

I was sure it had something to do with the &-sign! We had a 2 minute lesson about this in school, but I couldnt remember it, and dont know what this is called :) Thanks a bunch!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.