0

我觉得我非常接近在这个关联数组中成功切换 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));
 ?>

当前输出:

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

期望的输出:

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

2 回答 2

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'
// );
于 2013-01-05T07:12:46.630 回答
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)
于 2013-01-05T08:17:54.587 回答