0

I parsed a JSON file with json_decode, and the result is a long multidimensional array like the following:

Array
(
[Basic] => Array
    (
        [0] => Array
            (
                [text] => Taunt.
                [playerClass] => Shaman
                [locale] => enUS
                [mechanics] => Array
                    (
                        [0] => Array
                            (
                                [name] => Taunt
                            )
                    )
            )
    )

[Classic] => Array
    (
        [0] => Array
            (
                [cardId] => CS2_188o
                [name] => 'Inspired'
                [mechanics] => Array
                    (
                        [0] => Array
                            (
                                [name] => OneTurnEffect
                            )
                    )
            )
    )
)

I want to use a foreach to insert the data into a datatable but I can't make it work with this multidimensional array. How would I do that?

4
  • What is your question and what is your problem? Commented Oct 31, 2015 at 19:16
  • My problem is that i cant build the foreach code for this multidimensional arrary. Commented Oct 31, 2015 at 19:17
  • Yes, all the community allow you to do that. Read docs to know how to do it: php.net/manual/en/control-structures.foreach.php Commented Oct 31, 2015 at 19:26
  • Welcome to SO. I went through your code and removed a lot of the entries that had a similar structure. This way, your question is much faster to read, and any solution that works with the remaining data will allow you to read the original data (I also removed several excess blank lines that only spaced things out). It would really help if you could edit in the datatable you want to put this data into. (For example, how would you process an individual piece of info without using foreach?) Commented Nov 2, 2015 at 20:02

2 Answers 2

1

You must use recursive array to do it like this

function build($fullArray)
{
    foreach ($fullArray as $item) {
          if (is_array($item)){
            build($item);
          }
          else{
              echo $item["cardId"];
              echo $item["name"];
              ....
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use this:

foreach ($items as $item) {
  if (is_array($item)){
    foreach ($item as $it) {
      echo $it["name"];
    }
  }
  echo $item["cardId"];
  echo $item["name"];
  ....
}

3 Comments

This working with a single arrary correct, but here we have a litle big arrary as you can see Arrary -> Arrary -> Array.
I changed my answer: check if current $item is an array.
i change it a litle bit to include some arrarys over the second foreach. Thank you for your help. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.