Link to home
Create AccountLog in
Avatar of R-Byter
R-ByterFlag for Serbia

asked on

Use of double dollar sign in PHP

Hello fellow experts,

I have some problem which Im sure it will be solved quickly by some of you. It seems that I cant ger past this as Im overwhelmed with various tasks.
I want to dynamically create variable name and then show that value.
index from querystring can be sub_cat,name_sub_cat for example.
$position then becomes svasta['sub_cat']['name_sub_cat'].
Why $$position shows blank? Is there any other way to access infinite multidimensional array variable without this manual creation of the variable itself? I have a feeling that other way do exist, but I'm tired and I need help from you.

Thanks in advance.
Regards

<?php
	$index = $_GET["index"];
	$svasta = array ("cat_name" => "category1",
	 "id_cat" => "1",
	 "sub_cat" => array ("name_sub_cat" => "subCategory", "id_sub_cat" => "4", "ss_cat" =>array ("ss_cat_name" =>"ss_cat1", "id_ss_cat" => "4")
		   )
	  );
	$path = explode(",", $index);
	$position = "svasta";
	foreach($path as $key => $value)
	{
		$position .= "['$value']";
	}
	
	echo $position;
			
	echo "<form action='edit.php' method='POST'>";
	echo "<input type='text' name='$index' value='" . $$position . "' />";
	echo "<input type='submit' value='Izmeni' />";
	echo "</form>";
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of pius_babbun
pius_babbun

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of gr8gonzo
Yes $$ is a dynamic variable name:

<?php
$temperature = "hot";
$color = "blue";

$myVarName = "temperature";
echo $$myVarName; // Evaluates to echo $temperature, which will echo "hot"

$myVarName = "color";
echo $$myVarName; // Will echo "blue"
?>

That said, using an "infinite" array depth like that is probably not the best way to do things. You can accomplish infinite depth by using parent/child relationships:

$elements = array();
$elements[498] = array("id" => 498, "name" => "Parent", "parentID" => 0, "children" => array());
$elements[1022] = array("id" => 1022, "name" => "Child 1", "parentID" => 498, "children" => array());
$elements[1025] = array("id" => 1025, "name" => "Child 2", "parentID" => 498, "children" => array());

// Populate the children arrays with the child IDs
foraech($elements as $id => $element)
{
  $parentID = $element["parentID"];
  if(isset($elements[$parentID]))
  {
    $elements[$parentID]["children"][$id] = $id;
  }
}

Using that type of structure, everything is accessible from the main level using IDs, and if you need to go up or down the hierarchical chain, it's easy to do via the children array or by looking up the parentID.
Avatar of R-Byter

ASKER

I feel I need to provide more explanation so you can get better understanding of what I'm trying to achieve.

I do know the meaning of double dollar sign.
I do not create this array, this is the array which I'm getting from some VOIP software.
I do not know its size or depth, its not predefined.

In light of all that, Im trying to create some generic parser of that array.
Broader explanation is this:

get array from VOIP software
display all elements hierarchically structured (that part I wrote)
make two links for every single element (edit and delete) - passing nodes of the element
that links goes to page on which you can edit or delete array element

So, I'm giving you both of my pages, index.php and edit.php.

index.php
<pre>
<?php
	function Array2Form($array,$index){
		foreach($array as $key => $value){
		   $index = $index . $key . ",";
		   if(is_array($value)){
			  Array2Form($value,$index);
		   }
		   else{
			 $index = substr($index,0,-1);
			 echo "<b>$key:</b> $value&nbsp;&nbsp;&nbsp;<a href='edit.php?index=$index'>Izmeni</a>&nbsp;&nbsp;&nbsp;<a href='delete.php?index=$index'>Obriši</a><br />";
			 $index = str_replace($key, "", $index);
		  }
	  }
	}

    $svasta = array ("cat_name" => "category1",
	 "id_cat" => "1",
	 "sub_cat" => array ("name_sub_cat" => "subCategory", "id_sub_cat" => "4", "ss_cat" =>array ("ss_cat_name" =>"ss_cat1", "id_ss_cat" => "4")
		   )
	  );
	  
	Array2Form($svasta);
?>
</pre>

Open in new window


edit.php
<?php
	$index = $_GET["index"];
	$svasta = array ("cat_name" => "category1",
	 "id_cat" => "1",
	 "sub_cat" => array ("name_sub_cat" => "subCategory", "id_sub_cat" => "4", "ss_cat" =>array ("ss_cat_name" =>"ss_cat1", "id_ss_cat" => "4")
		   )
	  );
	$path = explode(",", $index);
	$position = "svasta";
	foreach($path as $key => $value)
	{
		$position .= "['$value']";
	}
	
	echo $position;
			
	echo "<form action='edit.php' method='POST'>";
	echo "<input type='text' name='$index' value='" . $$position . "' />";
	echo "<input type='submit' value='Izmeni' />";
	echo "</form>";
?>

Open in new window


Why this line shows blank for $$position, what I'm missing here?

echo "<input type='text' name='$index' value='" . $$position . "' />";

Open in new window


Let me know if any more explanation is needed.
Thanks
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of R-Byter

ASKER

Thanks for your assistance experts. I managed to find solution based on the first answer but also Roads Roads gave me better understanding, so I'll split the points.
Here is the solution (I'm using recursive function):

$_SESSION['voip'] is holding multidimensional array of data with unknown depth
function VariableArray($arr, $string)
    {
		preg_match_all('/\[([^\]]*)\]/', $string, $arr_matches, PREG_PATTERN_ORDER);
	   
		$return = $arr;
		foreach($arr_matches[1] as $dimension)
			{
				$return = $return[$dimension];
			}
		   
		return $return;
    }

Open in new window


$index = $_REQUEST["index"];
$path = explode(",", $index);
$position = "";

foreach($path as $key => $value)
{
	$position .= "[$value]";
}
$position = VariableArray($_SESSION['voip'], $position);
echo "<form action='edit.php' method='POST'>";
echo "<input type='text' name='value' value='" . $position . "' />";
echo "<input type='hidden' name='index' value='" . $index . "' />";
echo "<input type='submit' name='submit' value='Izmeni' />";
echo "</form>";

Open in new window


Regards