1

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?

4
  • 1
    Then show us your last attempt Commented Nov 12, 2019 at 10:16
  • 1
    Also if you show a var_export() of your array people can check their answer easier as that output is easy to pop into a piece of test code Commented Nov 12, 2019 at 10:17
  • @RiggsFolly, tried t.yctin.com/en/php/array-from-vardump-output and it works :^ ) Commented Nov 12, 2019 at 10:51
  • 2
    @sectus I didn't know about that site :) Still easier (one step) to copy and paste the output from a simple var_export($array) Commented Nov 12, 2019 at 10:54

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.