Home > Backend Development > PHP Tutorial > 数组有关问题 求大侠给个方法

数组有关问题 求大侠给个方法

WBOY
Release: 2016-06-13 12:54:40
Original
811 people have browsed it

数组问题 求大侠给个方法
$a = Array
(
    [0] => 3
    [1] => 1
    [2] => 2
    [3] => 7
)

$b = Array
(
    [1] => Emross War
    [2] => Roman Empire
    [3] => Avalon Wars
    [7] => 掌上三国
    [9] => 罗马(91)
    [10] => Space Dominions
    [11] => 腾讯三国
    [12] => Vampire War
    [15] => 三国英雄传
    [17] => 掌上水浒
)
$a数组中的值是$b 的键值

那我如何得到
$c = Array
(
    [1] => Emross War
    [2] => Roman Empire
    [3] => Avalon Wars
    [7] => 掌上三国
)

求大侠给个答案

php 数组
------解决方案--------------------
$a = Array<br />
(<br />
    0 => 3,<br />
    1 => 1,<br />
    2 => 2,<br />
    3 => 7<br />
);<br />
<br />
$b = Array<br />
(<br />
    1 => 'Emross War',<br />
    2 => 'Roman Empire',<br />
    3 => 'Avalon Wars',<br />
    7 => '掌上三国',<br />
    9 => '罗马(91)',<br />
    10 =>'Space Dominions',<br />
    11 =>'腾讯三国',<br />
    12 =>'Vampire War',<br />
    15 =>'三国英雄传',<br />
    17 =>'掌上水浒'<br />
);<br />
foreach($a as $k){<br />
     $c[$k]=$b[$k];<br />
}<br />
ksort($c);<br />
print_r($c);
Copy after login

------解决方案--------------------
sort($a); // 将数组 $a 按值升序排列<br />
$c = array(); //定义一个结果数组<br />
foreach($a as $k) { // 依次从数组 $b 中取值<br />
  $c[$k] = $b[$k];<br />
}
Copy after login

------解决方案--------------------
<br>
<br>
$a = Array<br>
(<br>
    '0' => 3,<br>
    '1' => 1,<br>
    '2' => 2,<br>
    '3' => 7<br>
);<br>
<br>
$b = Array<br>
(<br>
    '1' => 'Emross War',<br>
    '2' => 'Roman Empire',<br>
    '3' => 'Avalon Wars',<br>
    '7' => '掌上三国',<br>
    '9' => '罗马(91)',<br>
    '10' => 'Space Dominions',<br>
    '11' => '腾讯三国',<br>
    '12' => 'Vampire War',<br>
    '15' => '三国英雄传',<br>
    '17' => '掌上水浒',<br>
);<br>
$c = array_flip($a);				//array_flip -- 交换数组中的键和值 <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template