Jump to content

Dynamically Set Value Of Array (Layers Deep)


MrFish
 Share

Recommended Posts

I want to use a csv string to set the value of an index a few layers deep in an array. See my following array- This is for breadcrumbing purposes.

$links = array(	 "Home" => array(		  "alias" => "home",		  "link" => "index.php",		  "links" => array(			   "Communities" => array(					  "alias" => "Communities",					  "link" => "communities.php",					  "links" => array()									 ),			   "Home Plans" => array(					  "alias" => "Home-Plans",					  "link" => "plans",					  "links" => array()					    ),			   "Gallery" => array(					 "alias" => "Photo-Gallery",					 "link" => "galery.php",					 "links" => array()					   ),			   "About Us" => array(					 "alias" => "About-Us",					 "link" => "about.php",					 "links" => array(						  "Press" => array(							   "alias" => "press.php",							   "link" => "press.php",							   "links" => array()							  ),						  "Testimonials" => array(								 "alias" => "testimonials.php",								 "link" => "testimonials.php",								 "links" => array()							    )						 )				    ),			   "Contact Us" => array(					 "alias" => "Contact-Us",					 "link" => "contact.php",					 "links" => array()					 ),			   "Available Homes" => array(			  					  "alias" => "Available-Homes",					  "link" => "homes.php",					  "links" => array()					   ),			   "Homebuyer University" => array(					    "alias" => "Homebuyer-University",					    "link" => "homebuyer.php",					    "links" => array(							 "Finance Your Home" => array(									 "alias" => "Finance-Your-Home",									 "link" => "finance.php",									 "links" => array()								    ),							 "Solution Center" => array(									 "alias" => "Solution-Center",									 "link" => "solution.php",									 "links" => array()								    )						    )					   ),			   "Homeowners Corner" => array(					    "alias" => "Homeowner-Warranty",					    "link" => "homeowners.php",					    "links" => array()					   ),			   "Favorites" => array(					 "alias" => "Online-Favorites-Folder",					 "link" => "favorites_login.php",					 "links" => array(											  "View Favorites" => array(								 "alias" => "favorites.php",								 "link" => "favorites.php",								 "links" => array()								 )						 )				    ),			  )		   )	  )

The site I'm working on keeps page aliases inside a table called aliases. I want to dynaimcally load those aliases to certain sections of my array but giving it a little info to go off of-

$likes = array(		 "'gallery.php?gal=%'" => "Home,Gallery"	    );	       foreach($likes as $key => $value)    { // START OF FOREACH	 $query = "SELECT * FROM aliases WHERE als_url LIKE $key";	 $result = mysql_query($query);		 $newLinks = array();	 while($arr = mysql_fetch_assoc($result))	 {	  $newLinks[str_replace("-", " " , $arr[als_slug])] = array(					 "alias" => $arr[als_slug],					 "link" => $arr[als_url],					 "links" => array()					 );	 }

And the string "Home,Gallery" could be of any length and automatically go to that index of the array. To do that I wanted to use eval-

$arrString = "\$links";	 $parts = explode(",", $value);	 foreach($parts as $part)	 {	  $arrString .= "[$part][links]";	 }		 //Testing... does not work...	 echo $arrString;	 print_r(eval($arrString));    } // END OF FOREACH

This prints- $links[Home][links][Gallery][links] So I would think if this was evaled it would work. But I get nothing. Anyone know what I'm doing wrong?

Link to comment
Share on other sites

For one, the array keys aren't quoted.
Not required. Figured it out. Results-
$likes = array(		 "'gallery.php?gal=%'" => "Home,Gallery"	    );	       foreach($likes as $key => $value)    {	 $query = "SELECT * FROM aliases WHERE als_url LIKE $key";	 $result = mysql_query($query);		 $newLinks = array();	 while($arr = mysql_fetch_assoc($result))	 {	  $newLinks['"' . str_replace("-", " " , $arr[als_slug]) . '"'] = array(					 "'alias'" => $arr[als_slug],					 "'link'" => $arr[als_url],					 "'links'" => array()					 );	 }		 $arrString = "";	 $parts = explode(",", $value);	 foreach($parts as $part)	 {	  $arrString .= "['$part']['links']";	 }		 ob_start();	  print_r($newLinks);	 $arrOutput = ob_get_clean();		 $arrOutput = str_replace("Array", "array", $arrOutput);	 $arrOutput = preg_replace("|( => )([^\[\(]+)( \[)|U", "$1'$2',$3", $arrOutput);	 $arrOutput = str_replace("[\"", "\"", $arrOutput);	 $arrOutput = str_replace("\"]", "\"", $arrOutput);	 $arrOutput = str_replace("['", "'", $arrOutput);	 $arrOutput = str_replace("']", "'", $arrOutput);	 $arrOutput = preg_replace('|array[\s]+\([\s]+\)[\s]+\)[\s]+"|U', 'array ( ) ), "', $arrOutput);		 eval("\$links$arrString = $arrOutput;");    }

Wish there was a simpler way but this is what I came up with.

Link to comment
Share on other sites

Not required.
If you want to ignore low-level errors, then you're right, it's not required. PHP thinks you're referring to a constant and will take the time to look that up first before it issues a notice and uses a string, but if you're ignoring notices then you'll never see the error message. It's not a good practice to do that, but you're right, it's not "required". You may want to look into var_export instead of trying to replace the output from print_r. Also, if you pass true as the second parameter to print_r then it will return the output instead of printing it and then you don't need to use buffering to capture it.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...