简体   繁体   中英

PHP: Dynamically sort a multi-dimensional array

I have a function common to sites based in different locations. The two-dimensional array looks something like this:

$sites = array(
    'UK' => array(
        "sitecode" => "AUTest",
        "domain" => ".com.au",
        "label" => "Australia",
        ), 
    'DE' => array(
        "sitecode" => "DETest",
        "domain" => ".de",
        "label" => "Germany"
        ), 
    'CA' => array(
        "sitecode" => "CATest",
        "domain" => ".ca",
        "label" => "Canada"
        )
);

I'd like to display a list of all countries on each site, but always display the current country at the top of the list, with the others listed in their natural order, eg

In Australia, I want to see:

<li class="foo"><a href="http://example.com.au">Australia</a></li>
<li><a href="http://example.de">Germany</a></li>
<li><a href="http://example.ca">Canada</a></li>

In Germany:

<li class="foo"><a href="http://example.de">Germany</a></li>
<li><a href="http://example.com.au">Australia</a></li>
<li><a href="http://example.ca">Canada</a></li>

In Canada:

<li class="foo"><a href="http://example.ca">Canada</a></li>
<li><a href="http://example.com.au">Australia</a></li>
<li><a href="http://example.de">Germany</a></li>

If I have something like this:

foreach ($sites as $key => $list) {  
    if (My_External_Variable ==  $list['sitecode']) { 
        echo '<li><a class="foo" href="http://www.example' . $list['domain'] .'/">' . $list['label'] . '</a>';
    } 
    else { 
        echo '<li><a href="http://www.example' . $list['domain'] . '">' . $list['label']  . '</a></li>'; 
    } 
}

I can find the current country site I'm viewing and, for instance, apply a css class, but as it is, it's always going to display in it's index order.

I've looked at a lot of sort man pages, but have to confess I'm not a clever man. I think what I need to do is dynamically change the index based on the country site I'm currently viewing so that I can place it first, then iterate through the remaining. I'm sure it can be done, but am stuck trying to make it so.

I'm hoping someone can point me in the right direction.

you can save the output to variable and the selected to variable and after the loop chain them together so the first one will be the selected

$list = ''; $selected = '';
   foreach ($sites as $key => $list) {

        if (My_External_Variable ==  $list['sitecode']) { 
            $selected = '<li><a class="foo" href="http://www.example' . $list['domain'] .'/">' . $list['label'] . '</a>';
        } 
        else { 
            $list .= '<li><a href="http://www.example' . $list['domain'] . '">' . $list['label']  . '</a></li>'; 
        } 
    }
echo $selected . $list;

Why dont use another array to describe the order?

<?php
$sites = array(
    'UK' => array(
        "sitecode" => "AUTest",
        "domain" => ".com.au",
        "label" => "Australia",
        ), 
    'DE' => array(
        "sitecode" => "DETest",
        "domain" => ".de",
        "label" => "Germany"
        ), 
    'CA' => array(
        "sitecode" => "CATest",
        "domain" => ".ca",
        "label" => "Canada"
        )
);

$sites_order = array(
    "UK" => array("UK", "DE", "CA"),
    "DE" => array("DE", "UK", "CA"),
    "CA" => array("CA", "UK", "DE")
);

$selected_language = "DE";

foreach($sites_order[$selected_language] as $language) {
    printf("<pre>%s</pre>",$sites[$language]["label"]);
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM