Php 数组操作

Php 数组操作,php,arrays,string,explode,Php,Arrays,String,Explode,我有以下字符串(仅为示例): 和代码重新生成此字符串 $string=explode(",", $string); $i = 0; $ile=count($string); while ($i < $ile) { $a = explode("-", $string[$i]); if($a[2]==0) $animal .= $a[0].' '.$a[1]; elseif($a[2]==1) $thing .= $a[0].' '.$a[1]; $i++; }

我有以下字符串(仅为示例):

和代码重新生成此字符串

$string=explode(",", $string);
$i = 0;
$ile=count($string);
while ($i < $ile) {
    $a = explode("-", $string[$i]);
    if($a[2]==0) $animal .= $a[0].' '.$a[1];
    elseif($a[2]==1) $thing .= $a[0].' '.$a[1];
    $i++;
}
$string=explode(“,”,$string);
$i=0;
$ile=计数($string);
而($i<$ile){
$a=分解(“-”,$string[$i]);
如果($a[2]==0)$animal.=$a[0].'.$a[1];
elseif($a[2]==1)$thing.=$a[0].'.'.$a[1];
$i++;
}
我需要搜索这个数组并分组:$animal和$thing
我知道为什么这个代码不起作用,但我不知道怎么做。请帮助我:)

这应该可以完成任务:

$string = 'cat-cat.png-0,dog-dog.jpg-0,phone-nokia.png-1,horse-xyz.png-0';
$array  = explode(',', $string);
$animal = array();
$thing  = array();
foreach ($array as $item)
{
    $a = explode('-', $item);
    if( $a[2] == 1 )
    {
        $thing[] = $a[0] . ' ' . $a[1];
    }
    else
    {
        $animal[] = $a[0] . ' ' . $a[1];
    }
}

然后,如果需要将这些数组作为字符串,可以从中对其进行内爆。

这应该可以完成以下任务:

$string = 'cat-cat.png-0,dog-dog.jpg-0,phone-nokia.png-1,horse-xyz.png-0';
$array  = explode(',', $string);
$animal = array();
$thing  = array();
foreach ($array as $item)
{
    $a = explode('-', $item);
    if( $a[2] == 1 )
    {
        $thing[] = $a[0] . ' ' . $a[1];
    }
    else
    {
        $animal[] = $a[0] . ' ' . $a[1];
    }
}

然后,如果需要将这些数组作为字符串,您可以从中对其进行内爆。

如果您给出代码应输出的内容或希望实现的结果数组的示例,可能会有所帮助

如果您完全无法使用输入字符串格式,那么可以使用正则表达式使其更加健壮。比如:

/([^,]*)-(\d+)/

将匹配文件名和类型,假设类型始终为数字

例如:

preg_match_all("/([^,]*)-(\d+)/", "cat-cat.png-0,dog-dog.jpg-0,phone-nokia.png-1,horse-xyz.png-0", $matches); var_dump($matches);
$items = array();
foreach ($matches[2] as $key=>$type) {
    if (empty($items[$type])) {
        $items[$type] = array();
    }
    $items[$type][] = $matches[1][$key];
}
var_dump($items);

理想情况下,您希望添加更多的错误检查,例如检查是否按预期返回匹配项。

如果您给出代码应输出的内容或希望实现的结果数组的示例,可能会有所帮助

如果您完全无法使用输入字符串格式,那么可以使用正则表达式使其更加健壮。比如:

/([^,]*)-(\d+)/

将匹配文件名和类型,假设类型始终为数字

例如:

preg_match_all("/([^,]*)-(\d+)/", "cat-cat.png-0,dog-dog.jpg-0,phone-nokia.png-1,horse-xyz.png-0", $matches); var_dump($matches);
$items = array();
foreach ($matches[2] as $key=>$type) {
    if (empty($items[$type])) {
        $items[$type] = array();
    }
    $items[$type][] = $matches[1][$key];
}
var_dump($items);
理想情况下,您希望添加更多的错误检查,例如检查是否按预期返回匹配项。

您可以尝试

$string = 'cat-cat.png-0,dog-dog.jpg-0,phone-nokia.png-1,horse-xyz.png-0';
$array = explode(",", $string);
$list = array();

foreach ( $array as $info ) {
    list($prifix, $name, $id) = explode("-", $info);
    $list[$id][] = $prifix . "-" . $name;
}

var_dump($list);
输出

array
  0 => 
    array
      0 => string 'cat-cat.png' (length=11)
      1 => string 'dog-dog.jpg' (length=11)
      2 => string 'horse-xyz.png' (length=13)
  1 => 
    array
      0 => string 'phone-nokia.png' (length=15)
你可以试试

$string = 'cat-cat.png-0,dog-dog.jpg-0,phone-nokia.png-1,horse-xyz.png-0';
$array = explode(",", $string);
$list = array();

foreach ( $array as $info ) {
    list($prifix, $name, $id) = explode("-", $info);
    $list[$id][] = $prifix . "-" . $name;
}

var_dump($list);
输出

array
  0 => 
    array
      0 => string 'cat-cat.png' (length=11)
      1 => string 'dog-dog.jpg' (length=11)
      2 => string 'horse-xyz.png' (length=13)
  1 => 
    array
      0 => string 'phone-nokia.png' (length=15)

如果图像名称包含其他字符,如额外的
-
或数字,您还可以使用
preg\u match
来实现此功能,例如:

$string = 'cat-cat.png-0,dog-snoopy-dog.jpg-0,phone-nokia-6310.png-1,rabbit-bugs-bunny-eating-carrot.png-0,ignore-me';

$tmp = explode(',', $string);

$animals = $things = array();

foreach($tmp as $value)
{
    preg_match('/(?P<name>[A-Za-z]+)-(?P<file>[A-Za-z0-9\.-]+)-(?P<number>\d+)/', $value, $matches);

    if($matches)
    {
        switch($matches['number'])
        {
            case '0':
                array_push($animals, sprintf('%s %s', $matches['name'], $matches['file']));
                break;
            case '1':
                array_push($things, sprintf('%s %s', $matches['name'], $matches['file']));
                break;
        }
    }
}

如果图像名称包含其他字符,如额外的
-
或数字,您还可以使用
preg\u match
来实现此功能,例如:

$string = 'cat-cat.png-0,dog-snoopy-dog.jpg-0,phone-nokia-6310.png-1,rabbit-bugs-bunny-eating-carrot.png-0,ignore-me';

$tmp = explode(',', $string);

$animals = $things = array();

foreach($tmp as $value)
{
    preg_match('/(?P<name>[A-Za-z]+)-(?P<file>[A-Za-z0-9\.-]+)-(?P<number>\d+)/', $value, $matches);

    if($matches)
    {
        switch($matches['number'])
        {
            case '0':
                array_push($animals, sprintf('%s %s', $matches['name'], $matches['file']));
                break;
            case '1':
                array_push($things, sprintf('%s %s', $matches['name'], $matches['file']));
                break;
        }
    }
}
请尝试下面的代码

// Remove all things and all numbers with preceding hyphens
$animal = trim(preg_replace('/([a-zA-Z\-\.]+\-1,?)|(\-\d+)/', '', $string), ',');
// gives 'cat-cat.png,dog-dog.jpg,horse-xyz.png'

// Remove all animals and all numbers with preceding hyphens
$thing = trim(preg_replace('/([a-zA-Z\-\.]+\-0,?)|(\-\d+)/', '', $string), ',');
// gives 'phone-nokia.png'
请尝试下面的代码

// Remove all things and all numbers with preceding hyphens
$animal = trim(preg_replace('/([a-zA-Z\-\.]+\-1,?)|(\-\d+)/', '', $string), ',');
// gives 'cat-cat.png,dog-dog.jpg,horse-xyz.png'

// Remove all animals and all numbers with preceding hyphens
$thing = trim(preg_replace('/([a-zA-Z\-\.]+\-0,?)|(\-\d+)/', '', $string), ',');
// gives 'phone-nokia.png'

你应该考虑使用更多的自我解释变量名。这种编码方式并不是让你自己更容易。预期的输出是基本的需要给你如何做这个的想法。有趣的是,我从回答中选择了一个解决方案,你应该考虑使用更多的自我解释变量名。这种编码方式并没有让你自己变得更容易。预期输出是让你知道如何做到这一点的基本需要。THX出于兴趣,我从答案中选择了一种解决方案