Looping through multidimensional php array

GeekyOwl

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?

Alive to Die

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

PHP: Is there a better function to loop through multidimensional array

分類Dev

Looping Multidimensional array to generate table columns and rows using php

分類Dev

Parsing and looping through a javascript array of objects in PHP Laravel

分類Dev

Looping through objects in an array JS

分類Dev

R problems looping through array

分類Dev

MATLAB: Blanked Plots when Looping on a Multidimensional Array

分類Dev

Looping through XML Object PHP

分類Dev

PHP: String to multidimensional array

分類Dev

Sort a multidimensional array in PHP

分類Dev

Php include with multidimensional array

分類Dev

Looping through an array with g.drawString()

分類Dev

Looping through an array of strings - Type mismatch

分類Dev

Nicer syntax for looping through array of arrays in Ruby

分類Dev

Error while looping backwards through an array

分類Dev

Looping through JSON with PHP to get children

分類Dev

Looping through column fields in MySQL with PHP

分類Dev

looping array value with function php

分類Dev

PHP multidimensional array to flatten WITH KEYS

分類Dev

Multidimensional array JSON PHP iteration

分類Dev

NodeJS - Looping through Array Sequentially with Timeout between each Element in Array

分類Dev

multidimensional array find the given value in array in php

分類Dev

Generating and Looping through random numbers array with Next button

分類Dev

Looping through a 3-d array and creating subset vectors in R

分類Dev

Looping through an Array and performing a different action after the first match?

分類Dev

PHP function not returning a value from multidimensional array

分類Dev

PHP - How to get the sum of a multidimensional array?

分類Dev

Find Common most values in multidimensional array in PHP

分類Dev

PHP grouping content of multidimensional array with new structure

分類Dev

PHP Recursive Loop using UASORT on Multidimensional Array

Related 関連記事

  1. 1

    PHP: Is there a better function to loop through multidimensional array

  2. 2

    Looping Multidimensional array to generate table columns and rows using php

  3. 3

    Parsing and looping through a javascript array of objects in PHP Laravel

  4. 4

    Looping through objects in an array JS

  5. 5

    R problems looping through array

  6. 6

    MATLAB: Blanked Plots when Looping on a Multidimensional Array

  7. 7

    Looping through XML Object PHP

  8. 8

    PHP: String to multidimensional array

  9. 9

    Sort a multidimensional array in PHP

  10. 10

    Php include with multidimensional array

  11. 11

    Looping through an array with g.drawString()

  12. 12

    Looping through an array of strings - Type mismatch

  13. 13

    Nicer syntax for looping through array of arrays in Ruby

  14. 14

    Error while looping backwards through an array

  15. 15

    Looping through JSON with PHP to get children

  16. 16

    Looping through column fields in MySQL with PHP

  17. 17

    looping array value with function php

  18. 18

    PHP multidimensional array to flatten WITH KEYS

  19. 19

    Multidimensional array JSON PHP iteration

  20. 20

    NodeJS - Looping through Array Sequentially with Timeout between each Element in Array

  21. 21

    multidimensional array find the given value in array in php

  22. 22

    Generating and Looping through random numbers array with Next button

  23. 23

    Looping through a 3-d array and creating subset vectors in R

  24. 24

    Looping through an Array and performing a different action after the first match?

  25. 25

    PHP function not returning a value from multidimensional array

  26. 26

    PHP - How to get the sum of a multidimensional array?

  27. 27

    Find Common most values in multidimensional array in PHP

  28. 28

    PHP grouping content of multidimensional array with new structure

  29. 29

    PHP Recursive Loop using UASORT on Multidimensional Array

ホットタグ

アーカイブ