GeekyOwl
GeekyOwl

Reputation: 79

Looping through multidimensional php array

I have a multidimensional array-like below how can I convert

array(2) {
  ["category"]=>
  array(3) {
    [0]=>
    array(0) {
    }
    [1]=>
    array(0) {
    }
    [2]=>
    array(0) {
    }      
  }
  ["post_tag"]=>
  array(8) {
    [0]=>
    array(0) {
    }
    [1]=>
    array(2) {
      [0]=>
      string(1) "9"
      [1]=>
      string(1) "5"
    }
    [2]=>
    array(2) {
      [0]=>
      string(1) "6"
      [1]=>
      string(2) "11"
    }
    [3]=>
    array(0) {
    }
    [4]=>
    array(0) {
    }
    [5]=>
    array(0) {
    }
    [6]=>
    array(2) {
      [0]=>
      string(1) "9"
      [1]=>
      string(1) "5"
    }
    [7]=>
    array(2) {
      [0]=>
      string(1) "6"
      [1]=>
      string(2) "11"
    }      
  }
}

To like this

  array(2){
    array(
    'taxonomy'=> 'category',
    'data'=> "", //empty because nothing in it
    )
    array(
     'taxonomy => 'post_tag',
     'data => array( 9,5,6,11), //duplicates removed
    )
    }

I have tried with multiple foreach() loop but couldn't do it. using array_filter() , array_merge() is not working out .

Is there any way to get to a format like that?

Upvotes: 1

Views: 70

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

A simple foreach() with some native function will do the job:

$array = [];

foreach($originalArray as $key=>$value){
    $array[$key]['taxonomy'] = $key; //assign key as texonomy

    $value= array_filter(array_map('array_filter', $value)); //remove empty child array from parent array
    if(count($value) > 0){ //check any child-data remaining
        foreach($value as $val){ //yes then loop
            if (is_array($val)) { // if array
                $array[$key]['data'] = (isset($array[$key]['data'])) ? array_merge($array[$key]['data'], $val) : $val;  // merge all child values in data index
            } 
        }
        $array[$key]['data'] = array_unique($array[$key]['data']);// remove duplicate
    }else{
        $array[$key]['data'] = ''; // after filter if parent array doesn't have child array then assign empty to data index
    }
}
print_r(array_values($array));// use array_values() to re-index array

Output:-https://3v4l.org/QLNmL

Reduced code approach:

$array = [];

foreach($originalArray as $key=>$value){
    $array[$key]['taxonomy'] = $key;
    foreach($value as $val){
        $array[$key]['data'] = (isset($array[$key]['data'])) ? array_merge($array[$key]['data'], $val) : $val;
    } 
    $array[$key]['data'] = array_unique(array_filter($array[$key]['data']));
    if( 1 > count($array[$key]['data'])){
        $array[$key]['data'] = '';
    }
}
print_r(array_values($array));

Output:-https://3v4l.org/J9U9H

Upvotes: 2

sectus
sectus

Reputation: 15464

You could try this, whitout foreach, only functional approach.

$result = array_map( // let's map array to new one
    function ($key, $value) {
        return array(
            'taxonomy'=> $key,
            'data'=> array_unique( // uniqualize values
                array_merge(...$value) // array_merge([], ['9','5'], ['6', '11']...)
            )?:'' 
        );
    },
    array_keys($array), // we need keys for mapping
    $array
);

https://3v4l.org/4up4d

Upvotes: 2

Related Questions