PHP从数组中提取值并将它们还原到另一个数组中
I have an Array of values pulled from a mysql table of the form:
$name_ids = EMP-646
EMP-545
EMP-12
CLIENT-36
CLIENT-43
CLIENT-5
I would like to identify EMP and CLIENT as separate data and then extract only the integer values and then store them into separate variables. For example:
$emp_id = 646
545
12
$client_id = 36
43
5
Here is my attempt at it but I am unable to print the desired results (not sure if my logic is correct):
$name_ids = array($_SESSION['INVITED_NAMES']);
foreach($name_ids[0] as $name_id){
if(stripos($name_id, 'EMP') !== false){
$emp_id = preg_replace("/[^0-9]/","",$name_id);
}
elseif(stripos($name_id, 'CLIENT') !== false){
$client_id = preg_replace("/[^0-9]/","",$name_id);
}
echo $emp_id.' '; //both results need to happen at this stage in the `foreach` loop.
echo $client_id.' ';
}
One thing to note is I need the results to appear where they are due to other code that is dependent on this location. With the present code this is the results I get:
646 545 12 36 12 43 12 5 12
The error appears at the first if statement($emp_id
), when a value is false it returns the last int of a true value until the loop is through. Any help is greatly appreciated.
我从表单的mysql表中提取了一个值数组: p>
我想将EMP和CLIENT识别为单独的数据,然后仅提取整数值,然后将它们存储到单独的变量中。 例如: p>
这是我的尝试,但我无法打印出所需的结果(不确定我的逻辑是否正确): p>
需要注意的一点是,我需要将结果显示在依赖于此位置的其他代码的位置。 使用当前代码,这是我得到的结果: p>
错误出现在第一个if语句( $ name_ids = EMP-646
EMP-545
EMP-12
CLIENT-36
CLIENT-43
CLIENT-5
code> pre>
$ emp_id = 646
545
12
$ client_id = 36
43
5
code> pre >
$ name_ids = array($ _ SESSION ['INVITED_NAMES']);
foreach($ name_ids [0] as $ name_id){
if(stripos($ name_id,'EMP')!== false){
$ emp_id = preg_replace(“/ [^ 0-9] /”,“”,$ name_id);
}
elseif(stripos($ name_id,'CLIENT')!== false){
$ client_id = preg_replace(“ / [^ 0-9] /“,”“,$ name_id);
}
echo $ emp_id。' “; //两个结果都需要在`foreach`循环的这个阶段发生。
echo $ client_id。' “;
}
code> pre>
646 545 12 36 12 43 12 5 12
code> pre>
$ emp_id code>),当值为false时,它返回true值的最后一个int,直到循环结束。 任何帮助是极大的赞赏。 p>
div>
Here it is working: https://eval.in/84445
Relevant code:
$emp_ids = array();
$client_ids = array();
$name_ids = array('EMP-646',
'EMP-545',
'EMP-12',
'CLIENT-36',
'CLIENT-43',
'CLIENT-5');
var_dump($name_ids);
foreach($name_ids as $name_id){
if(stripos($name_id, 'EMP') !== false){
$emp_id = preg_replace("/[^0-9]/","",$name_id);
array_push($emp_ids, $emp_id);
}
elseif(stripos($name_id, 'CLIENT') !== false){
$client_id = preg_replace("/[^0-9]/","",$name_id);
array_push($client_ids, $client_id);
}
}
echo "Emp_ids are: ";
var_dump($emp_ids);
echo "Client_ids are: ";
var_dump($client_ids);
I just created arrays to store the values, and pushed them in.