2个数组替换的有关问题哦

WBOY
Release: 2016-06-13 13:39:00
Original
848 people have browsed it

2个数组替换的问题哦
1.
怎么样将数组
array('1234', '123');
变成
array('\'1234\'', '\'123\'');
这样的

2.
$find= array('\'1234\'', '\'123\'');
$shuju= array ('1234','12345','123456','1234567','123');
$tihuan= array('\'南风\'', '\'西风\'');
$q = str_replace($find, $tihuan, var_export($shuju, true)); 
这个$q输出的是array(0=>..省略..)

但是我想要的结果是
南风
12345
123456
1234567
西风

2个问题哦,,一并帮我看看,谢谢了

------解决方案--------------------
1:

PHP code
$arr=array('1234', '123');
function foo($p){
     return "\'$p\'";
}
$ar=array_map('foo',$arr);
print_r($ar);
<br><font color="#e78608">------解决方案--------------------</font><br>运行结果<br>array ( 0 => '南风', 1 => '12345', 2 => '123456', 3 => '1234567', 4 => '西风', )
<br><font color="#e78608">------解决方案--------------------</font><br>1、
Copy after login
PHP code
function foo($v) {
  return "'$v'";
}
print_r(array_map('foo', array('1234', '123')));
<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨

你还是没有安踏的要求做哦
他要的是从 var_export($shuju, true) 的结果中做字符串替换

其实他的两问都是有问题的

------解决方案--------------------
PHP code
$shuju= array ('1234','12345','123456','1234567','123');//原始数据
foreach($shuju as &$value) // 给每个单元两边加引号
     $value = "'$value'";
// $shuju被修改了已经
$find= array('\'1234\'', '\'123\'');
$tihuan= array('\'南风\'', '\'西风\'');//替换成的

$q = str_replace($find, $tihuan, $shuju);

foreach($q as $value)
{
    echo substr($value, 1, -1);
    echo "<br>\n";
}
<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
PHP code
$ar=array_combine($find,$tihuan);

function foo($p){
    global $ar;
    if($ar[$p]) return $ar[$p];
    else return $p;
}

foreach(array_map('foo',$shuju) as $value) { 
  echo $value;
   echo "<br>"; 
} <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