Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can PHP Array Keys have an Alias?

Tags:

arrays

alias

php

This is just a curious question, the reasoning behind it is purely to be slightly more lazy on my part. Here is what I mean..

Say I have a website, where htaccess makes nice urls, and sends that data to the $_GET['p'] array key as the current 'page'. In the index file, I setup the page, and the first thing I do is setup some page settings in a config file, $_PAGE array. Now, say I have multiple pages I want to have the same settings, (and down in the page, other things may slightly change that do not correspond to the settings. So currently, I have something that looks like the following 2 php files.

// index.php
include('page.array.php');

echo '<title>'.$_PAGE[$_GET['p']]['title'].'</title>';

// page.array.php
$_PAGE = array(
    'some/page/' => array(
        'title' => 'This is an example'
    )
)
$_PAGE['some/aliased/page/'] = $_PAGE['some/page/'];

Notice that at the end ofthe page array, in order to 'alias' a page I must add this to the end after the array has been created.

Is there any method in php that maybe I am just unaware of, that could make me a tad bit lazier (and at the same time add to cleaner code), and make it so I can simply alias the key? I notice the following doesn't work, and I suppose my question is, is there any way to create the alias within the same array during the creation of the array?

This example deosn't work:

// page.array.php
$_PAGE = array(
    'some/page/' => array(
        'title' => 'This is an example'
    ),
    'some/aliased/page/' => $_PAGE['some/page/']
)

Maybe a way to refer to "this" array, from within itself?

If this is not possible, I don't have an issue with the "Not Possible" answer. Though if you have a better method of solving this, other then the way I have described above, in the sake of being lazier, I would be interested in reading it :)

like image 828
NinjaKC Avatar asked Dec 12 '12 00:12

NinjaKC


People also ask

Can an array have same key PHP?

Arrays contains unique key. Hence if u are having multiple value for a single key, use a nested / multi-dimensional array. =) thats the best you got.

Which array has named keys PHP?

Associative arrays are arrays that use named keys that you assign to them.

Can a Key have multiple values PHP?

To directly answer your question, no. PHP arrays can only contain one set of data for the key.

What are array keys PHP?

PHP | array_keys() Function The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.


1 Answers

I don't believe you can have array values that mirror other values in the array like this. The first thing that comes to mind though would be for you to construct your $_PAGE array from within a switch statement, using fall-through values as aliases:

// Define path for testing, and empty page array
$path = "some/aliased/page";
$page = Array();

// Time to evaluate our path
switch ($path) {
    // If it's either of these two cases
    case "some/page":
    case "some/aliased/page":
        // Assign this array to $page
        $page = Array("Title" => "Two Paths, One Page.");
        break;
    // If it's this case
    case "some/other/path":
        // Assign this array to $page
        $page = Array("Title" => "Something else.");
        break;
    // If the path isn't found, default data
    default:
        $page = Array("Title" => "Page not found");
}

// Output the result
var_dump($page);

Execute it: http://sandbox.onlinephpfunctions...ebd3dee1f37c5612c25

like image 164
Sampson Avatar answered Oct 24 '22 09:10

Sampson