Перебор и слияние массива

Discussion in 'PHP' started by Seganapa, 23 Jul 2012.

  1. Seganapa

    Seganapa New Member

    Joined:
    8 Jun 2012
    Messages:
    74
    Likes Received:
    0
    Reputations:
    0
    Всем Добрый день!

    Имеется массив $arr:
    PHP:
    Array
    (
        [
    0] => Array
            (
                [
    0] => Array
                    (
                        [
    x] => 668
                        
    [y] => 693
                        
    [id] => 1143766
                        
    [type] => 45
                        
    [res] => Si
                        
    [neighbours] => Array
                            (
                                [
    0] => 43123377
                                
    [1] => 43975343
                                
    [2] => 43844283
                                
    [3] => 44040883
                                
    [4] => 44434103
                            
    )

                    )

                [
    1] => Array
                    (
                        [
    x] => 658
                        
    [y] => 677
                        
    [id] => 1141366
                        
    [type] => 47
                        
    [res] => Si
                        
    [neighbours] => Array
                            (
                                [
    0] => 43451044
                                
    [1] => 43451049
                            
    )

                    )

        [
    1] => Array
            (
                [
    0] => Array
                    (
                        [
    x] => 676
                        
    [y] => 677
                        
    [id] => 1141416
                        
    [type] => 58
                        
    [res] => Iw
                        
    [neighbours] => Array
                            (
                                [
    0] => 43975334
                                
    [1] => 43909792
                            
    )

                    )

                [
    1] => Array
                    (
                        [
    x] => 696
                        
    [y] => 678
                        
    [id] => 1141463
                        
    [type] => 37
                        
    [res] => Iw
                        
    [neighbours] => Array
                            (
                                [
    0] => 45482664
                                
    [1] => 46072481
                                
    [2] => 45023916
                                
    [3] => 45679275
                                
    [4] => 46138025
                                
    [5] => 46138028
                            
    )

                    )

                [
    2] => Array
                    (
                        [
    x] => 678
                        
    [y] => 695
                        
    [id] => 1143767
                        
    [type] => 45
                        
    [res] => Wi
                        
    [neighbours] => Array
                            (
                                [
    0] => 43844283
                                
    [1] => 43778741
                                
    [2] => 44040883
                                
    [3] => 44761776
                            
    )

                    )

                [
    3] => Array
                    (
                        [
    x] => 676
                        
    [y] => 685
                        
    [id] => 1143783
                        
    [type] => 37
                        
    [res] => Ws
                        
    [neighbours] => Array
                            (
                                [
    0] => 43975334
                                
    [1] => 43975343
                                
    [2] => 44040883
                                
    [3] => 44761776
                            
    )

                    )

        [
    2] => Array
            (
                [
    0] => Array
                    (
                        [
    x] => 657
                        
    [y] => 716
                        
    [id] => 1146107
                        
    [type] => 49
                        
    [res] => Wi
                        
    [neighbours] => Array
                            (
                                [
    0] => 42664649
                                
    [1] => 43582149
                                
    [2] => 42926801
                            
    )

                    )

                [
    1] => Array
                    (
                        [
    x] => 655
                        
    [y] => 704
                        
    [id] => 1146108
                        
    [type] => 59
                        
    [res] => Ws
                        
    [neighbours] => Array
                            (
                                [
    0] => 42664634
                                
    [1] => 42926780
                                
    [2] => 43123394
                                
    [3] => 43582149
                            
    )

                    )

                [
    2] => Array
                    (
                        [
    x] => 658
                        
    [y] => 706
                        
    [id] => 1146110
                        
    [type] => 40
                        
    [res] => Sw
                        
    [neighbours] => Array
                            (
                                [
    0] => 43647680
                                
    [1] => 42926780
                                
    [2] => 42664649
                                
    [3] => 43582149
                            
    )

                    )
    Мне нужно произвести его слияние, но значения n могут быть различными... ($arr[n][n]).

    На выходе нужно получить:
    PHP:
    Array
    (
                 [
    0] => Array
                    (
                        [
    x] => 668
                        
    [y] => 693
                        
    [id] => 1143766
                        
    [type] => 45
                        
    [res] => Si
                        
    [neighbours] => Array
                            (
                                [
    0] => 43123377
                                
    [1] => 43975343
                                
    [2] => 43844283
                                
    [3] => 44040883
                                
    [4] => 44434103
                            
    )

                    )

                [
    1] => Array
                    (
                        [
    x] => 658
                        
    [y] => 677
                        
    [id] => 1141366
                        
    [type] => 47
                        
    [res] => Si
                        
    [neighbours] => Array
                            (
                                [
    0] => 43451044
                                
    [1] => 43451049
                            
    )

                    )

                [
    2] => Array
                    (
                        [
    x] => 676
                        
    [y] => 677
                        
    [id] => 1141416
                        
    [type] => 58
                        
    [res] => Iw
                        
    [neighbours] => Array
                            (
                                [
    0] => 43975334
                                
    [1] => 43909792
                            
    )

                    )

                [
    3] => Array
                    (
                        [
    x] => 696
                        
    [y] => 678
                        
    [id] => 1141463
                        
    [type] => 37
                        
    [res] => Iw
                        
    [neighbours] => Array
                            (
                                [
    0] => 45482664
                                
    [1] => 46072481
                                
    [2] => 45023916
                                
    [3] => 45679275
                                
    [4] => 46138025
                                
    [5] => 46138028
                            
    )

                    )

                [
    4] => Array
                    (
                        [
    x] => 678
                        
    [y] => 695
                        
    [id] => 1143767
                        
    [type] => 45
                        
    [res] => Wi
                        
    [neighbours] => Array
                            (
                                [
    0] => 43844283
                                
    [1] => 43778741
                                
    [2] => 44040883
                                
    [3] => 44761776
                            
    )

                    )

                [
    5] => Array
                    (
                        [
    x] => 676
                        
    [y] => 685
                        
    [id] => 1143783
                        
    [type] => 37
                        
    [res] => Ws
                        
    [neighbours] => Array
                            (
                                [
    0] => 43975334
                                
    [1] => 43975343
                                
    [2] => 44040883
                                
    [3] => 44761776
                            
    )

                    )

                [
    6] => Array
                    (
                        [
    x] => 657
                        
    [y] => 716
                        
    [id] => 1146107
                        
    [type] => 49
                        
    [res] => Wi
                        
    [neighbours] => Array
                            (
                                [
    0] => 42664649
                                
    [1] => 43582149
                                
    [2] => 42926801
                            
    )

                    )

                [
    7] => Array
                    (
                        [
    x] => 655
                        
    [y] => 704
                        
    [id] => 1146108
                        
    [type] => 59
                        
    [res] => Ws
                        
    [neighbours] => Array
                            (
                                [
    0] => 42664634
                                
    [1] => 42926780
                                
    [2] => 43123394
                                
    [3] => 43582149
                            
    )

                    )

                [
    8] => Array
                    (
                        [
    x] => 658
                        
    [y] => 706
                        
    [id] => 1146110
                        
    [type] => 40
                        
    [res] => Sw
                        
    [neighbours] => Array
                            (
                                [
    0] => 43647680
                                
    [1] => 42926780
                                
    [2] => 42664649
                                
    [3] => 43582149
                            
    )
                    )
    Делаю перебор массива
    PHP:
    for($i=0$i<=count($arr); $i++)
    //Пытаюсь сделать слияние, но как это сделать в цикле не пойму...
    $result array_merge($arr[$i],???????);
    print_r($result);
     
    #1 Seganapa, 23 Jul 2012
    Last edited: 23 Jul 2012
  2. masterprof

    masterprof New Member

    Joined:
    3 Nov 2011
    Messages:
    0
    Likes Received:
    1
    Reputations:
    0
    PHP:
    $arr_res = array();
    foreach (
    $arr as $subarr) {
        foreach (
    $subarr as $val$arr_res[] = $val;
    }
    $arr $arr_res;
     
  3. Seganapa

    Seganapa New Member

    Joined:
    8 Jun 2012
    Messages:
    74
    Likes Received:
    0
    Reputations:
    0
    Спасибо, теперь понял!
     
  4. Seganapa

    Seganapa New Member

    Joined:
    8 Jun 2012
    Messages:
    74
    Likes Received:
    0
    Reputations:
    0
    А как теперь из полученного массива удалить [neighbours] со значениями?
     
  5. Seganapa

    Seganapa New Member

    Joined:
    8 Jun 2012
    Messages:
    74
    Likes Received:
    0
    Reputations:
    0
    Сделал так:
    PHP:
    $arr1 = array();
    foreach (
    $arr as $a)
    {
    unset(
    $a['neighbours']);
    $arr1[] = $a;
    }
    print_r ($arr1);
    Работает!
     
  6. masterprof

    masterprof New Member

    Joined:
    3 Nov 2011
    Messages:
    0
    Likes Received:
    1
    Reputations:
    0
    можно проще

    PHP:
    foreach ($arr as &$a) unset($a['neighbours']);
    print_r ($arr);
     
    1 person likes this.