切换阵列的两个键

时间:2013-01-05 06:50:37

标签: php function foreach associative-array

我觉得我在这个关联数组中成功切换Jim和Jill非常接近。我也希望它是可重复的,所以如果'Joe'被添加到最后,它也将交换'Jim'和'Joe'。有什么指针吗?

 <?php
 function jim_is_jill($their_name) {

    $first = key($their_name);

    foreach ($their_name as $key => $value) {
        $lastmaybe = $key;
    }   
        $lastmaybe = $these;              // Lastmaybe is Jill
        $these     = $first;

        return $their_name;
 }  

 $their_name = array(
 //       Key   =>  Value
         'Jim'   => 'dad', 
         'Josh'  => 'son', 
         'Jamie' => 'mom', 
         'Jane'  => 'daughter', 
         'Jill'  => 'daughter'
         );

 print_r(jim_is_jill($their_name));
 ?>

CURRENT OUTPUT:

Array
(
    [Jim] => dad
    [Josh] => son
    [Jamie] => mom
    [Jane] => daughter
    [Jill] => daughter
)

期望的输出:

Array
(
    [Jill] => dad
    [Josh] => son
    [Jamie] => mom
    [Jane] => daughter
    [Jim] => daughter
)

2 个答案:

答案 0 :(得分:3)

考虑阵列

$their_name = array(
 //   Key   =>  Value
    'Jim'   => 'dad', 
    'Josh'  => 'son', 
    'Jamie' => 'mom', 
    'Jane'  => 'daughter', 
    'Jill'  => 'daughter'
);

此功能将产生:

function array_swap_values($array, $key1, $key2) {
    $temp = $array[$key1];
    $array[$key1] = $array[$key2];
    $array[$key2] = $temp;
    return $array;
}

$their_name = array_swap_values($their_name, 'Jim', 'Jill');
// -> array(
//      'Jim'   => 'daughter', 
//      'Josh'  => 'son', 
//      'Jamie' => 'mom', 
//      'Jane'  => 'daughter', 
//      'Jill'  => 'dad'
// );

或者这个功能会产生

function array_swap_keys($array, $key1, $key2) {
    $ret = array();
    foreach ($array as $key => $value) {
        if ($key === $key1) {
            $ret[$key2] = $array[$key2];
        } else if ($key === $key2) {
            $ret[$key1] = $array[$key1];
        } else {
            $ret[$key] = $value;
        }
    }
    return $ret;
}

$their_name = array_swap_keys($their_name, 'Jim', 'Jill');
// -> array(
//      'Jill'  => 'daughter', 
//      'Josh'  => 'son', 
//      'Jamie' => 'mom', 
//      'Jane'  => 'daughter', 
//      'Jim'   => 'dad'
// );

** 更新 **

在您上次编辑之后,我修改了最后一个函数以返回预期的内容。它非常接近第一个函数,但它保留了键排序:

function array_swap_key_value($array, $key1, $key2) {
    $ret = array();
    foreach ($array as $key => $value) {
        if ($key === $key1) {
            $ret[$key2] = $array[$key1];
        } else if ($key === $key2) {
            $ret[$key1] = $array[$key2];
        } else {
            $ret[$key] = $value;
        }
    }
    return $ret;
}

$their_name = array_swap_key_value($their_name, 'Jim', 'Jill');
// -> array(
//      'Jill'  => 'dad', 
//      'Josh'  => 'son', 
//      'Jamie' => 'mom', 
//      'Jane'  => 'daughter', 
//      'Jim'   => 'daughter'
// );

答案 1 :(得分:1)

首先,根据我的经验,如果没有数字索引,那么依赖数组的顺序是不明智的。没有工具(纠正我,如果我错了)切换键的位置或更改键本身。它必须是丑陋的黑客。重命名密钥的唯一方法是将其删除并正确放回。但这扰乱了数组的顺序。你真的必须从头开始重建阵列,这是Yanick建议的最简单的方法,如果你真的想做你想做的事情。但是有更简洁的解决方案。您可以创建一个带有编号索引的数组,您将不会触及它。这样,它将保持秩序。然后在每个值中放入简单的小数组:

array('name'=> 'Jill, 'relationship'=>'daughter);

这样,你可以完全控制索引的顺序(多亏了编号的索引),你只需要交换值,这很容易。

或者,您可以省略这些单词,并在任何地方为其指定编号索引。这样你就会少写,但你必须记住哪个是:

array('jill', 'daughter');

实际上与:

相同
array(0 => 'Jill', 1 => 'daughter');

所以这是代码..

<?php

function swap_first_and_last($their_name) {

    //get first and last keys
    reset($their_name); // resets the array pointer to beginning
    $k_first=key($their_name); // first key
    end($their_name);
    $k_last=key($their_name); // last key



    // swap first and last:
    $swap = $their_name[$k_first]['name'];
    $their_name[$k_first]['name']=$their_name[$k_last]['name'];
    $their_name[$k_last]['name']=$swap;

    // note: you can use [0] and [1], if you modify your array that way

    return $their_name;
}

// modified array
 $their_name = array(
 // note, you can omit those 0 => , 1 => ,2,... keys
                 0 => array('name' => 'Jim', 'relationship' => 'dad'),
                 1 => array('name' => 'Josh', 'relationship' => 'son'),
                 2 => array('name' => 'Jamie', 'relationship' => 'mom'),
                 3 => array('name' => 'Jane', 'relationship' => 'daughter'),
                 4 => array('name' => 'Jill', 'relationship' => 'daughter')
                 );



var_dump(swap_first_and_last($their_name));

结果是:

array (size=5)
    0 => 
        array (size=2)
            'name' => string 'Jill' (length=4)
            'relationship' => string 'dad' (length=3)
    1 => 
        array (size=2)
            'name' => string 'Josh' (length=4)
            'relationship' => string 'son' (length=3)
    2 => 
        array (size=2)
            'name' => string 'Jamie' (length=5)
            'relationship' => string 'mom' (length=3)
    3 => 
        array (size=2)
            'name' => string 'Jane' (length=4)
            'relationship' => string 'daughter' (length=8)
    4 => 
        array (size=2)
            'name' => string 'Jim' (length=3)
            'relationship' => string 'daughter' (length=8)