Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP数组中删除不同条目的特定部分?_Php_Arrays - Fatal编程技术网

从PHP数组中删除不同条目的特定部分?

从PHP数组中删除不同条目的特定部分?,php,arrays,Php,Arrays,我有一个脚本,它生成一个数组,如下所示: [0] => 1_Result1 [1] => 2_Result2 [2] => 3_Result3 [0] => Result1 [1] => Result2 [2] => Result3 但我希望它是这样的: [0] => 1_Result1 [1] => 2_Result2 [2] => 3_Result3 [0] => Result1 [1] =&g

我有一个脚本,它生成一个数组,如下所示:

[0] => 1_Result1  
[1] => 2_Result2  
[2] => 3_Result3 
[0] => Result1  
[1] => Result2  
[2] => Result3
但我希望它是这样的:

[0] => 1_Result1  
[1] => 2_Result2  
[2] => 3_Result3 
[0] => Result1  
[1] => Result2  
[2] => Result3
如何做到这一点

foreach ($array as $key => $item) {
    //Cut 2 characters off the start of the string
    $array[$key] = substr($item, 2); 
}
或者,如果您想变得更花哨,与
\uuuu
隔绝:

foreach ($array as $key => $item) {
    //Find position of _ and cut off characters starting from that point
    $array[$key] = substr($item, strpos($item, "_")); 
}
这将在PHP4和PHP5的任何版本中工作

或者,如果您想变得更花哨,与
\uuuu
隔绝:

foreach ($array as $key => $item) {
    //Find position of _ and cut off characters starting from that point
    $array[$key] = substr($item, strpos($item, "_")); 
}

这将适用于任何版本的PHP 4和5。

了解有关如何过滤数组以及如何形成数组的更具体规则可能会有所帮助,但要回答您的具体问题:

PHP 5.4:

array_map(function ($elem) { return explode('_', $elem)[1]; }, $arr)
PHP 5.3:

array_map(function ($elem) {
    $elem = explode('_', $elem);
    return $elem[1];
}, $arr);

了解更多关于如何过滤数组以及数组如何形成的具体规则可能会有所帮助,但要回答您的具体问题:

PHP 5.4:

array_map(function ($elem) { return explode('_', $elem)[1]; }, $arr)
PHP 5.3:

array_map(function ($elem) {
    $elem = explode('_', $elem);
    return $elem[1];
}, $arr);
在这里:

警告:仅当您知道要删除的前缀的大小(在本例中为2)时,此操作才起作用。

此处:

<?php

    $results = array( "1_result1", "2_result2", "3_result3", "4_reslut4");
    $fixed_results = array();
    foreach ($results as $result) 
        {
                $fixed_results[]= substr($result, 2);
        }

    print_r($fixed_results);
?>

警告:仅当您知道要删除的前缀的大小(在本例中为2)时,此操作才起作用伙计,用户1477388对于这个简单的解决方案,我感激不尽!但你可以感谢我:向上投票!非常欢迎:)
$var=end(explode(“'u',$result))伙计,用户1477388对于这个简单的解决方案,我感激不尽!但你可以感谢我:向上投票!非常欢迎:)