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

Categories

0 votes
759 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 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...