多阵列与一个输出 [英] Multi-array with one output

查看:119
本文介绍了多阵列与一个输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code,它目前还无法正常工作。我怎样才能使它工作吗?我的愿望是使输出像一根弦(当然我知道如何转换数组字符串):


  

字修改,添加,删除,并使其


code:

 < PHP标题(内容类型:text / html的;字符集= utf-8');
$文字=爆炸(,用strip_tags(是否已发生变化添加和删除,使之词));
$堆栈=阵列();$字=阵列(改变,补充,东西);
的foreach($词作为关键词$){
   $检查= array_search($关键字,$文字);
   如果($检查>( - 1)){
     $替换=,$文字[$支票]。
   $结果= str_replace函数($文字[$支票],$替换,​​$文字);
    array_push($栈,$结果);
   }
}的print_r($堆栈);
?>

输出:

 阵列

    [0] =>排列
        (
            [0] =>话
            [1] =>改变,
            [2] =>添加
            [3] =>和
            [4] =>去除
            [5] =>至
            [6] =>使
            [7] =>它
        )    [1] =>排列
        (
            [0] =>话
            [1] =>改变
            [2] =>添加,
            [3] =>和
            [4] =>去除
            [5] =>至
            [6] =>使
            [7] =>它
        )


解决方案

如果没有更多的解释,它是像这样简单:

  $文字=用strip_tags(是否已发生变化添加和删除,使之词);
$字=阵列(改变,补充,东西);$结果= $文字;
的foreach($词作为$字){
    $结果= str_replace函数($词,$字,$结果);
}


  • 请不要炸掉你的源字符串

  • 循环的话并用一词取代,并添加逗号

或放弃循环方式:

  $文字=用strip_tags(是否已发生变化添加和删除,使之词);
$字=阵列(改变,补充,东西);$结果= preg_replace('/('爆('|',$字)')/','$ 1',$文本);


  • 通过交替爆的话创建模式(OR)运算符 |

  • 字替换找到字 $ 1 和一个逗号

Here is my code, which currently does not working properly. How can I make it working? My wish is to make output like one string (of course I know how to "convert" array to string):

words altered, added, and removed to make it

Code:

<?php

header('Content-Type: text/html; charset=utf-8');
$text = explode(" ", strip_tags("words altered added and removed to make it")); 
$stack = array();

$words = array("altered", "added", "something"); 
foreach($words as $keywords){
   $check = array_search($keywords, $text);
   if($check>(-1)){
     $replace = " ".$text[$check].","; 
   $result = str_replace($text[$check], $replace, $text);
    array_push($stack, $result);
   }
}

print_r($stack);
?>

Output:

Array
(
    [0] => Array
        (
            [0] => words
            [1] =>  altered,
            [2] => added
            [3] => and
            [4] => removed
            [5] => to
            [6] => make
            [7] => it
        )

    [1] => Array
        (
            [0] => words
            [1] => altered
            [2] =>  added,
            [3] => and
            [4] => removed
            [5] => to
            [6] => make
            [7] => it
        )
)

解决方案

Without more explanation it's as simple as this:

$text  = strip_tags("words altered added and removed to make it"); 
$words = array("altered", "added", "something"); 

$result = $text;
foreach($words as $word) {
    $result = str_replace($word, "$word,", $result);
}

  • Don't explode your source string
  • Loop the words and replace the word with the word and added comma

Or abandoning the loop approach:

$text   = strip_tags("words altered added and removed to make it"); 
$words  = array("altered", "added", "something"); 

$result = preg_replace('/('.implode('|', $words).')/', '$1,', $text);

  • Create a pattern by imploding the words on the alternation (OR) operator |
  • Replace found word with the word $1 and a comma

这篇关于多阵列与一个输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆