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
934 views
in Technique[技术] by (71.8m points)

cakephp 2.x - How to void triple loop in this situation in php

I have these data: (called ArrayA)

ArrayA
(
    [0] => Array
        (
            [IdList] => Array
                (    
                    [0] => Array
                        (
                            [Id] => ID2
                            [A]  => A
                            [B]  => B
                            [C]  => C
                            ...
                        (
                    [1] => Array
                        (
                            [Id] => ID6
                            [A]  => A
                            [B]  => B
                            [C]  => C
                            ...
                        (
                (
        )

    [1] => Array
        (
            [IdList] => Array
                (    
                    [0] => Array
                        (
                            [Id] => ID3
                            [A]  => A
                            [B]  => B
                            [C]  => C
                            ...
                        (
                    [1] => Array
                        (
                            [Id] => ID9
                            [A]  => A
                            [B]  => B
                            [C]  => C
                            ...
                        (
                (
        )
    ...
)

and, there is value of IDs: (called ArrayB, key is ID number)

ArrayB
(
    [ID1] => foo
    [ID2] => bar
    [ID3] => baz
)

What I want to do is; If there is exact ID matched between ArrayA (ArrayA[i][IDList][j][Id]) and ArrayB's key(Id), I want to make data like this:
(I want to bring ArrayB value data into ArrayA with same index where matched ID is existed, and as inserted as new key such as Value)

Array
(
    [0] => Array
        (
            [IdList] => Array
                (    
                    [0] => Array
                        (
                            [Id] => ID2
                            [A]  => A
                            [B]  => B
                            [C]  => C
                            ...
                            [Value]  => bar
                        (
                    [1] => Array
                        (
                            [Id] => ID6
                            [A]  => A
                            [B]  => B
                            [C]  => C
                            ...
                        (
                (
        )

    [1] => Array
        (
            [IdList] => Array
                (    
                    [0] => Array
                        (
                            [Id] => ID3
                            [A]  => A
                            [B]  => B
                            [C]  => C
                            ...
                            [Value]  => baz
                        (
                    [1] => Array
                        (
                            [Id] => ID9
                            [A]  => A
                            [B]  => B
                            [C]  => C
                            ...
                        (
                (
        )
    ...
)

What I came up with easily is using triple loops like this,

foreach($arrayA as $i => $data) {
  foreach($arrayA[$i] as $j => $id) {
    foreach($arrayB as $key => $value) {
      if($id === $key) {
        arrayA[$i]['IdList'][$j]['Value'] = $value;
      }
    }
  }
}

I want to know if there is a better way to achieve this!

question from:https://stackoverflow.com/questions/65850591/how-to-void-triple-loop-in-this-situation-in-php

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

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

...