[英]How to get values from multidimensional array in php?
i have multi dimensional array like below, 我有如下的多维数组,
Array
(
[14289] => Array
(
[0] => Ability:B,Itemname:Session #3: Tues June 28th - Fri July 8th (9-2:00PM)#1 only: 2pm
[1] => Ability:B+,Itemname:Session #3: Tues June 28th - Fri July 8th (9-2:00PM)#1 only: 2pm
[2] => Ability:B++,Itemname:Session #3: Tues June 28th - Fri July 8th (9-2:00PM)#1 only: 2pm
)
[14279] => Array
(
[0] => Ability:N/S,Itemname:Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pm
[1] => Ability:N/S+,Itemname:Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pm
[2] => Ability:N/S++,Itemname:Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pm
)
[14288] => Array
(
[0] => Ability:N/S,Itemname:Session #3: Tues June 28th - Fri July 8th (9-2:00PM)#1 only: 1:30pm
)
[14291] => Array
(
[0] => Ability:N/S+,Itemname:Session #4: Tues July 12th - Fri July 22nd (9-2:00PM)#1 only: 1pm
)
[14284] => Array
(
[0] => Ability:N/S++,Itemname:Session #2: Tues June 14th - Fri June 24th (9-2:00PM)#1 only: 1:30pm
)
)
I need to get the values from this array and explode the values into ability and itemname. 我需要从该数组中获取值,并将值分解为能力和项名。
How to i do this?. 我该怎么做?
Assuming $array
contains your data, the following code will do it. 假设$array
包含您的数据,下面的代码将执行此操作。
$result = array();
foreach($array as $a){
foreach($a as $l){
list($ab, $it) = explode(",", $l, 2);
$ab = substr($ab, strlen("Ability:"));
$it = substr($it, strlen("Itemname:"));
$result[] = array(
'Ability' => $ab,
'Itemname' => $it
);
}
}
Try this, it works for me. 试试这个,对我有用。 I match the desired strings with a regex 我用正则表达式匹配所需的字符串
<?php
$arr[][] = 'Ability:B,Itemname:Session #3: Tues June 28th - Fri July 8th (9-2:00PM)#1 only: 2pm';
$arr[][] = 'Ability:B+,Itemname:Session #3: Tues June 28th - Fri July 8th (9-2:00PM)#1 only: 2pm';
$arr[][] = 'Ability:B++,Itemname:Session #3: Tues June 28th - Fri July 8th (9-2:00PM)#1 only: 2pm';
$pattern = '/Ability:(.*),Itemname:(.*)$/m';
for ($i = 0; $i < count($arr); $i++) {
for ($j = 0; $j < count($arr[$i]); $j++) {
preg_match_all($pattern, $arr[$i][$j], $matchResult);
echo 'Original string: '. $arr[$i][$j].'<br>';
echo 'Ability: '.$matchResult[1][0].'<br>';
echo 'Itemname: '.$matchResult[2][0].'<br>';
echo '<br><hr><br>';
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.