-2

I'm using jQuery plugin which generates following array:

    array:4 [
  0 => array:1 [
    "page_id" => "1"
  ]
  1 => array:2 [
    "page_id" => "2"
    "children" => array:1 [
      0 => array:2 [
        0 => array:2 [
          "page_id" => "10"
          "children" => array:1 [
            0 => array:2 [
              0 => array:1 [
                "page_id" => "12"
              ]
              1 => array:1 [
                "page_id" => "13"
              ]
            ]
          ]
        ]
        1 => array:1 [
          "page_id" => "11"
        ]
      ]
    ]
  ]
  2 => array:1 [
    "page_id" => "4"
  ]
  3 => array:1 [
    "page_id" => "3"
  ]
]

This array is dynamic so there can be more nested arrays. I need to clean array little bit. As you can see under page_id => 2 there is children. It contains one useless array (0 => array: 2).

Is it possible to remove this useless arrays? I really need it to regenerate my menu items positions... Any ideas?

general666
  • 1,001
  • 2
  • 16
  • 31

2 Answers2

1

You can use a recursive algorithm:

function removeUselessArrays($array) {
    $newArray = [];

    foreach ($array as $key => $value) {
        if (is_array($value)) {
            if (array_keys($value) === [ 0 ]) {
                $newArray[$key] = removeUselessArrays($value[0]);
            } else {
                $newArray[$key] = removeUselessArrays($value);
            }
        } else {
            $newArray[$key] = $value;
        }
    }

    return $newArray;
}

See the code working here.

Phylogenesis
  • 7,775
  • 19
  • 27
  • Thank you but look closer to my array. There is like one more nested level: "children" => array:1 [ 0 => array:2 [ 0 => array:2 [ You didn't use it in your example. I tried it but it doesn't remove array: 1 from children – general666 Aug 19 '16 at 11:12
0

You could do that from within a loop like so:

ALGORITHM:

<?php

    foreach($arr as $key=>$val){
        foreach($val as $index=>$item){
            if( is_array($item) ){
                $children               = ["page_id"=>$item[0]["page_id"]];
                $children               = array_merge($children, $item[0][$index]);
                $arr[$key][$index]      = $children;
            }
        }
    }

TEST

    <?php

    // LOOP THROUGH THE ARRAY CONTAINING UNWANTED ELEMENT(S)
    foreach($arr as $key=>$val){
        foreach($val as $index=>$item){
            // CHECK IF ONE OF THE SUB-ELEMENTS IS AN ARRAY & HAS KEY: children
            if( is_array($item) ){
                // IF SO; GET THE CHILDREN ELEMENT OFF THAT ITEM
                // AND STORE IT IN A VARIABLE...
                $children               = $item[0][$index];
                // NOW SET THE SUB-ELEMENT TO THE $children
                $children               = ["page_id"=>$item[0]["page_id"]];
                $children               = array_merge($children, $item[0][$index]);
                $arr[$key][$index]      = $children;
            }
        }
    }

    // DUMP-OUT THE OUTPUT:
    var_dump($arr);

    // PRODUCES:::  
    array (size=4)
      0 =>
        array (size=1)
          'page_id' => string '1' (length=1)
      1 =>
        array (size=2)
          'page_id' => string '2' (length=1)
          'children' =>
             array (size=3)
              'page_id' => string '10' (length=1)
               0 =>
                array (size=1)
                  'page_id' => string '12' (length=1)
               1 =>
                array (size=1)
                  'page_id' => string '13' (length=1)
      2 =>
        array (size=1)
          'page_id' => string '4' (length=1)
      3 =>
        array (size=1)
          'page_id' => string '3' (length=1)

Quick Test is found Here.

Poiz
  • 7,611
  • 2
  • 15
  • 17
  • Thank you but please look at my new answer... I think you didn't understood me. And of course like I said my array is dynamic so there can be more nested levels. Your code looks like it works only with 2 levels – general666 Aug 19 '16 at 11:20
  • thank you but I get error: Undefined index: children – general666 Aug 19 '16 at 12:03
  • @general666 Sure, it'll work now... with the updated code.. give it a go... test: https://eval.in/625814 – Poiz Aug 19 '16 at 12:16
  • still getting same error on this row: $children = $item[0][$index]; I don't understand because it's working in eval.in test. I'm using Laravel but I think that's not problem – general666 Aug 19 '16 at 12:33
  • Problem is solved. Solution from @Phylogenesis is working great. Big thank you for your help!!! – general666 Aug 19 '16 at 13:00