Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

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?.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming $array contains your data, the following code will do it.

$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
        );
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...