php数组在多个数组中选择一个键值并更改它们
is there a simple solution I can put inside a function to resolve the following:
I have multiple arrays as follows:
Array
(
[0] => A
[1] => 100_1
[2] => 1
[3] => 1184
[4] => 0
)
Array
(
[0] => A
[1] => 100_2
[2] => 2
[3] => 1185
[4] => 0
)
Array
(
[0] => A
[1] => 100_3
[2] => 3
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101_2
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)
What I'd like to do is to select all the values which have _1,2,3 only, and remove the _1,2,3 so the desired result would be:
Array
(
[0] => A
[1] => 100
[2] => 1
[3] => 1184
[4] => 0
)
Array
(
[0] => A
[1] => 100
[2] => 2
[3] => 1185
[4] => 0
)
Array
(
[0] => A
[1] => 100
[2] => 3
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)
Notice only values which contain _1,2,3 have been changed. Many thanks for your help it's much appreciated.
The code I have so far is:
foreach($data as $line) {
if(substr($line,0,1)=="A") {
if(!$first) {
$parts = explode(chr(9), $line);
list($num1, $num2) = explode('_', $parts[1]); //this code needs to come first
$parts[2] = isset($num2) ? $num2 : $parts[2]; //it replaces key[2] with _* (1,2,3)
//echo "<pre>"; print_r($parts); echo "</pre>";
//the code to remove _* needs to go here.
$pos = strpos($parts[1], '_'); // this was all it needed a simple two liner
if($pos !== false) $parts[1] = substr($parts[1], 0, $pos);
有一个简单的解决方案我可以放在一个函数中来解决以下问题: p>
我有多个数组如下: p>
Array
(
[0] =&gt; A
[1] =&gt; 100_1
[2 ] =&gt; 1
[3] =&gt; 1184
[4] =&gt; 0
)
Array
(
[0] =&gt; A
[1] =&gt; 100_2
[2] =&gt; 2
[3] =&gt; 1185
[4] =&gt; 0
)
Array
(
[0] =&gt; A
[ 1] =&gt; 100_3
[2] =&gt; 3
[3] =&gt; 1186
[4] =&gt; 0
)
Array
(
[0] =&gt; ; A
[1] =&gt; 101_2
[2] =&gt; 2
[3] =&gt; 1188
[4] =&gt; 0
)
Array
(
[0] =&gt; A
[1] =&gt; 302
[2] =&gt; 0
[3] =&gt; 1161
[4] =&gt; 0
)
code> pre>
我想要做的是选择只有_1,2,3的所有值,
并删除_1,2,3以便得到所需的结果 : p>
Array
(
[0] =&gt; A
[1] =&gt; 100
[2] =&gt; 1
[3 ] =&gt; 1184
[4] =&gt; 0
)
Array
(
[0] =&gt; A
[1] =&gt; 100
[2] =&gt; 2
[3] =&gt; 1185
[4] =&gt; 0
)
Array
(
[0] =&gt; A
[1] =&gt; 100
[ 2] =&gt; 3
[3] =&gt; 1186
[4] =&gt; 0
)
Array
(
[0] =&gt; A
[1] =&gt; 101
[2] =&gt; 2
[3] =&gt; 1188
[ 4] =&gt; 0
)
Array
(
[0] =&gt; A
[1] =&gt; 302
[2] =&gt; 0
[3] =&gt; ; 1161
[4] =&gt; 0
)
code> pre>
请注意,只有包含_1,2,3的值已被更改。
非常感谢您的支持 非常感谢。 p>
到目前为止我的代码是: p>
foreach($ data as $ line){
if (substr($ line,0,1)==“A”){
if(!$ first){
$ parts = explode(chr(9),$ line);
list($ num1,$ num2)= explode('_',$ parts [1]); //这个代码需要先来
$ parts [2] = isset($ num2)? $ num2:$ parts [2]; //它用_ *(1,2,3)
// echo“&lt; pre&gt;”替换键[2]; 的print_r($部分); echo“&lt; / pre&gt;”;
//删除_ *的代码需要转到此处。
$ pos = strpos($ parts [1],'_'); //这就是它需要一个简单的两个衬里
if($ pos!== false)$ parts [1] = substr($ parts [1],0,$ pos);
code> pre>
div>
Assuming the _1
etc. always happens in array[1]
foreach($array as $row) {
$pos = strpos($row[1], '_');
if($pos !== false) $row[1] = substr($row[1], 0, $pos);
}
you can use the following:
function formatArray($array)
{
if(is_array($array))
{
foreach($array as $key=>$value)
{
$array[$key] = str_replace('_1', '', $value);
$array[$key] = str_replace('_2', '', $value);
$array[$key] = str_replace('_3', '', $value);
}
return $array;
}
return false;
}
Call this function with every array as parameter or you can assign the complete array to and parent array and then add one more foreach
to loop through it.
Just write a function which uses preg_replace to alter your data:
<?php
$test_data = array("A", "100_1", "0", "1184", "0");
function removeValuesWithUnderScore(array $arr){
//Loop over array
foreach($arr as $key => $value){
$arr[$key] = preg_replace('/_[1-3]/', '', $value);
}
//Return altered array
return $arr;
}
$test_data = removeValuesWithUnderScore($test_data);
Try this :
$array = array("A", "100_1", 0, 1184, 0);
$array = array_map(
function($str) {
return preg_replace('/\_[\d]+/', '', $str);
},
$array
);
print_r($array)
Edit : If OP needs only _1, _2 and _3 to be removed :
$array = array("A", "100_3", 0, 1184, 0);
$array = array_map(
function($str) {
return preg_replace('/\_[1|2|3]/', '', $str);
},
$array
);
print_r($array)
presuming you want any value with _xxx appended to be replaced:
foreach($array as &$element){
$element = strstr($element, '_', true)?:$element;
}