Jump to content

[SOLVED] Converting /this/example/ to $example['this']['example']


Recommended Posts

Big Picture

I'm in the middle of designing a CMS right now, I've got everything down and a lot of the classes built and general framework working. I'm actually down to the CMS architecture right now. I know how I want it, and how it has to be... but I'm missing one piece. This piece, a very important one!  8O

 

Smaller Picture

I have all non-existing pages re-directing to say request_handler.php?q=$1 with $1 being the requested page maybe /help/me/

 

The handler will check the 'content' table for the requested location. No problem there.

 

The 'content' table contains a field called 'location' in order to generate my site map array I want to loop through the 'location's and explode the /'s to create the associative array I'm looking for.

 

I'm working on designing site map array to create menu's/navigation etc... I've already got them working off of this associative array that i wrote up.

 

Me Rambling

If its possible its not easy and that's probably why i haven't seen this done before. I think if this can be  tackled it will be a real important and smart part of the architecture of the CMS. I've tried a couple of different aproaches but didn't get far at all. I've spent most of my time scratching my head trying to figure out where to start.

 

Obviously I'm going to have to use something like:

 

foreach($link as $blah){
$something[] = explode('/',$blah);
}

 

but that gives me a non associative array.

 

I put a array_flip on that and i got the array keys all good... they just weren't multidimensional? or below each other.

 

I'm completely lost... :cry:

 

The Code Example

 

The _contents isn't important... the important and hard part to figure out (for me) is how to simply convert 

 

<?php // Quick Example
$string = "this/is/my/example/";
// INTO
$example['this']['is']['example']
?>

 

<?php // LONNG EXAMPLE

// This is what I have.

$link[] = "features/silly/text/facts";
$link[] = "features/silly/text/facts/cats";
$link[] = "features/silly/text/facts/dogs";

$link[] = "features/silly/text/jokes";
$link[] = "features/silly/text/jokes/cats";
$link[] = "features/silly/text/jokes/dogs";

$link[] = "features/silly/pictures/pets";
$link[] = "features/silly/pictures/pets/cats";
$link[] = "features/silly/pictures/pets/dogs";

$link[] = "features/silly/video/pets";
$link[] = "features/silly/video/pets/cats";
$link[] = "features/silly/video/pets/dogs";

$link[] = "features/silly/me";
$link[] = "features/silly/you";

$link[] = "features/serious/text/facts";
$link[] = "features/serious/text/facts/cats";
$link[] = "features/serious/text/facts/dogs";

$link[] = "features/serious/text/poems";
$link[] = "features/serious/text/poems/cats";
$link[] = "features/serious/text/poems/dogs";

$link[] = "features/serious/pictures/pets";
$link[] = "features/serious/pictures/pets/cats";
$link[] = "features/serious/pictures/pets/dogs";

$link[] = "features/serious/video/pets";
$link[] = "features/serious/video/pets/cats";
$link[] = "features/serious/video/pets/dogs";

$link[] = "features/serious/me";
$link[] = "features/serious/you";

$link[] = "help/me";
$link[] = "help/me/please";
$link[] = "help/me/please/seriously";

// This is what I want...
//      Some how I want to end up with this resulting associative array.

$assoc['features']['silly']['text']['facts']['_contents'] = "";
$assoc['features']['silly']['text']['facts']['cats'] = "";
$assoc['features']['silly']['text']['facts']['dogs'] = "";

$assoc['features']['silly']['text']['jokes']['_contents'] = "";
$assoc['features']['silly']['text']['jokes']['cats'] = "";
$assoc['features']['silly']['text']['jokes']['dogs'] = "";

$assoc['features']['silly']['pictures']['pets']['_contents'] = "";
$assoc['features']['silly']['pictures']['pets']['cats'] = "";
$assoc['features']['silly']['pictures']['pets']['dogs'] = "";

$assoc['features']['silly']['video']['pets']['_contents'] = "";
$assoc['features']['silly']['video']['pets']['cats'] = "";
$assoc['features']['silly']['video']['pets']['dogs'] = "";

$assoc['features']['silly']['me']['_contents'] = "";
$assoc['features']['silly']['you']['_contents'] = "";

$assoc['features']['serious']['text']['facts']['_contents'] = "";
$assoc['features']['serious']['text']['facts']['cats'] = "";
$assoc['features']['serious']['text']['facts']['dogs'] = "";

$assoc['features']['serious']['text']['jokes']['_contents'] = "";
$assoc['features']['serious']['text']['jokes']['cats'] = "";
$assoc['features']['serious']['text']['jokes']['dogs'] = "";

$assoc['features']['serious']['pictures']['pets']['_contents'] = "";
$assoc['features']['serious']['pictures']['pets']['cats'] = "";
$assoc['features']['serious']['pictures']['pets']['dogs'] = "";

$assoc['features']['serious']['video']['pets']['_contents'] = "";
$assoc['features']['serious']['video']['pets']['cats'] = "";
$assoc['features']['serious']['video']['pets']['dogs'] = "";

$assoc['features']['serious']['me']['_contents'] = "";
$assoc['features']['serious']['you']['_contents'] = "";

$assoc['help']['me']['_contents'] = "";
$assoc['help']['me']['please']['_contents'] = "";
$assoc['help']['me']['please']['seriously'] = "";

?>

The following does what you want:

<?php

$link[] = "features/silly/text/facts";
$link[] = "features/silly/text/facts/cats";
$link[] = "features/silly/text/facts/dogs";

$link[] = "features/silly/text/facts/_contents";
$link[] = "features/silly/text/facts/cats";
$link[] = "features/silly/text/facts/dogs";

$link[] = "features/silly/text/jokes/_contents";
$link[] = "features/silly/text/jokes/cats";
$link[] = "features/silly/text/jokes/dogs";

$assoc = array();
foreach($link as $s_link)
{
   $keys = explode('/', $s_link);

   eval('$assoc[\'' . implode('\'][\'', $keys) . '\'] = "";');
}

echo '<pre>' . print_r($assoc, true) . '</pre>';

?>

How is the $assoc associative array going to be used latter on?

Thank you very much! I was trying to do an eval on it before but i couldn't get the syntax (or my head) right. It was a very late night last night ;D

 

Anyhow I want to thank you very much for helping me out! You have no idea how happy and thankful I am! I'm going to try and stick around in these forums and try and post more... hopefully more answers than questions here. I feel like I need to give back :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.