更改数组的输出文本


Changing output text from array

我目前打印表列名,过滤,但需要更改每个文本。例如:

$row = mysql_fetch_assoc($query_columns);
echo implode(', ', array_keys( array_filter( $row )));

输出:

column_one, column_two, column_five

如果没有冗长的if语句,我无法实现的是完全控制输出的方法,例如更改每个…

预期输出:

This is column one, and Column two, but this is column five

任何帮助都非常感谢!

表结构:

Category
--------
user_id
live_out
live_in
short_term
weekday
weekend
unsure

当前输出(基于用户选择):

live_out, live_in, weekday

期望输出:

Yes as live-out, Yes as live-in, Yes for regular weekdays

您有echo implode(', ', array_keys( array_filter( $row )));implode()使用数组将键连接到一个字符串。

一个解决方案是在迭代数组时使用字符串连接:

$array = array_keys( array_filter( $row ))); // gets you the keys array
$string = 'This is ';
foreach($array as $item)
{
    $string .= $item . ', '; // append item to string with comma added
}
$string .= '. That's all.';
echo $string;

如果你想为每个条目定制文本,你需要在迭代时检测条目。您可以使用if和strpos(),如下所示:

$string = 'This is ';
foreach($array as $item)
{
    $string .= $item . ', '; // append item to string with comma added
    if(false !== strpos($item, 'Column One')) { // look for Column One in $item
        $string .= ' (found column one)';
    }    
}
$string .= ' That's all.';
echo $string;

替换:

$string = str_replace('Column One', 'Column XXX', $string);

也可以同时用于多个项目

$replace = array('Column One', 'Column Two');
$with = array('Column XX1', 'Column XX2');
$string = str_replace($replace, $with, $string);

关注$string =$string .=。第一个语句赋值,第二个语句追加。

基于你的表结构

$string = '';
foreach($array as $item)
{   
    if(false !== strpos($item, 'live_out')) { 
        $string .= 'Yes for '. $item . ', ';
    }
    if(false !== strpos($item, 'live_in')) { 
        $string .= 'Yes for '. $item . ', ';
    }
    if(false !== strpos($item, 'weekdays')) { 
        $string .= 'Yes for regular '. $item;
    }    
}   
echo $string;

你可以像这样改变数组中的每一项;

$column_names[0] = "Add this to the start of column one ". $column_names[0] . ", and this to the end of column one.";

0是数组中的第一项,1是第二项,依此类推。

请记住,在本例中,您需要对从array_keys获得的数组执行此操作。

否则你可以创建一个这样的函数;

function addStrToArrayElements($array, $str_start, $str_end, $array_keys=NULL){
    foreach($array as $key => $item){
        if(array_key_exists($key, $array_keys) ||  is_null($array_keys) ){
            $ret[$key] = $str_start.$item.$str_end;
        }
    }
    return $ret;
}

$array是你想改变元素的数组,$str_start是放在项目前面的字符串,$str_end是放在项目后面的on, $array_keys是你想改变元素的键的数组,如果你把它留空,它将对$array的所有元素做同样的事情。

对于上面的例子,你可以这样做:

$column_names = array_keys($row);
addStrToArrayElements($column_names, 'This is ', ',', array(0));
addStrToArrayElements($column_names, ' and ', ',', array(1));
addStrToArrayElements($column_names, ' but this is ', '', array(2));
$output = implode('', $column_names);
echo str_replace('column_', 'column ', $output);

应该输出

This is column one, and column two, but this is column five

假设下面有一个名为$array1的数组:

<?php
$array1 = array("hey","its","me","so","what");
$newarray = array();
$i=0;
foreach ($array1 as $value)
{
  $newarray[] = "<span id=" . "color$i" . ">" . $value . "</span>";
  $i++;
}
echo implode(",",$newarray);
?>

您可以使用该算法为使用创建的span的所有列设置不同的样式。例如,你可以在头部或外部样式表中设置样式,像这样:

<style>
#color0{ color:red;}
#color1{ color:blue;}
#color2{ color:yellow;}
</style>

我测试了一下,它似乎工作得很好!