php PHP爆炸数组然后遍历值并输出到变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10650938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:43:32  来源:igfitidea点击:

PHP explode array then loop through values and output to variable

phparrays

提问by Marc Sanders

The string I am trying to split is $item['category_names']which for example contains Hair, Fashion, News

我试图拆分的字符串是$item['category_names']例如包含Hair, Fashion, News

I currently have the following code:

我目前有以下代码:

$cats = explode(", ", $item['category_names']);
foreach($cats as $cat) {
    $categories = "<category>" . $cat . "</category>\n";
}

I want the outcome of $categoriesto be like the following so I can echo it out later somewhere.

我希望的结果$categories如下所示,以便我稍后可以在某个地方将其回显。

<category>Hair</category>\n
<category>Fashion</category>\n
<category>News</category>\n

Not sure if I am going the right way about this?

不确定我是否正确地解决了这个问题?

回答by Maxim Krizhanovsky

In your code you are overwritting the $categories variable in each iteration. The correct code would look like:

在您的代码中,您将在每次迭代中覆盖 $categories 变量。正确的代码如下所示:

$categories = '';
$cats = explode(",", $item['category_names']);
foreach($cats as $cat) {
    $cat = trim($cat);
    $categories .= "<category>" . $cat . "</category>\n";
}

update: as @Nanne suggested, explode only on ','

更新:正如@Nanne 建议的那样,只在“,”上爆炸

回答by Aaron W.

Without a for loop

没有 for 循环

$item['category_names'] = "Hair,   Fashion,   News";
$categories = "<category>".
        implode("</category>\n<category>", 
        array_map('trim', explode(",", $item['category_names']))) . 
        "</category>\n";
echo $categories;

回答by divya sekar

PHP explode array by loopdemo: http://sandbox.onlinephpfunctions.com/code/086402c33678fe20c4fbae6f2f5c18e77cb3fbc2

PHP通过循环演示分解数组http: //sandbox.onlinephpfunctions.com/code/086402c33678fe20c4fbae6f2f5c18e77cb3fbc2

its works for me

它对我有用

<?php

$str = "name:john,hone:12345,ebsite:www.23.com";

$array=explode(",",$str);

if(count($array)!=0)
{
foreach($array as $value)
{

    $data=explode(":",$value);

      echo $data[0]."=".$data[1];


    echo ' ';

}
}
?>

回答by Wampie Driessen

if you use this:

如果你使用这个:

$cats = explode(", ", $item['category_names']);
foreach($cats as $cat) {
$categories = "<category>" . $cat . "</category>\n";
}

the $categories string is overwritten each time, so "hair" and "fasion" are lost..

$categories 字符串每次都会被覆盖,因此“头发”和“时尚”丢失了..

if you however add a dot before the equal sign in the for loop, like so:

但是,如果您在 for 循环中的等号前添加一个点,如下所示:

$cats = explode(", ", $item['category_names']);
foreach($cats as $cat) {
$categories .= "<category>" . $cat . "</category>\n";
}

the $catergories string will consist of all three values :)

$catergories 字符串将包含所有三个值:)

回答by AD7six

The error in your code is this:

您的代码中的错误是这样的:

$categories = "<category>" . $cat . "</category>\n";

You are overwriting $categoriesat each iteration, it should be:

$categories在每次迭代时都覆盖,它应该是:

$categories .= "<category>" . $cat . "</category>\n";

Not sure if I am going the right way about this?

不确定我是否正确地解决了这个问题?

find and replace isn't what explode is for. If you just want to correct the code error - see above.

查找和替换不是爆炸的目的。如果您只想更正代码错误 - 请参见上文。

This is more efficient:

这是更有效的:

$categories = "<category>" .
    str_replace(', ', "</category>\n<category>", $input) . 
    "</category>\n";

And this also accounts for variable whitespace:

这也说明了可变空格:

$categories = "<category>" . 
    preg_replace('@\s*,\s*@', "</category>\n<category>", $input) . 
    "</category>\n";