1

I want to parse JSON string so that I can get it saved into two dimensional array like shown below. Only solution I come with consist of 3 or 4 foreach loops, and I don't think that's good.

 {
  "root": {
    "row": [
      {
        "Status": "Enabled",
        "Keyword": "Toaletna voda",
        "Campaign": "Lešenari",
        "Adgroup": "Lešenaris",
        "BidStrategyType": "InheritFromParent",
        "Bid": "0.05",
        "Matchtype": "Broad",
        "Clicks": "0",
        "Impr.": "0",
        "Conv.": "0"
      },
      {
        "Status": "Enabled",
        "Keyword": "lyžička",
        "Campaign": "Lešenari",
        "Adgroup": "Lešenaris",
        "BidStrategyType": "InheritFromParent",
        "Bid": "0.05",
        "Matchtype": "Broad",
        "Clicks": "0",
        "Impr.": "0",
        "Conv.": "0"
      },
      {
        "Status": "Search total",
        "Keyword": "-",
        "Campaign": "-",
        "Adgroup": "-",
        "BidStrategyType": "-",
        "Bid": "-",
        "Matchtype": "-",
        "Clicks": "0",
        "Impr.": "0",
        "Conv.": "0"
      },
      {
        "Status": "Content total",
        "Keyword": "-",
        "Campaign": "-",
        "Adgroup": "-",
        "BidStrategyType": "-",
        "Bid": "-",
        "Matchtype": "-",
        "Clicks": "0",
        "Impr.": "0",
        "Conv.": "0"
      },
      {
        "Status": "Deleted items total",
        "Keyword": "-",
        "Campaign": "-",
        "Adgroup": "-",
        "BidStrategyType": "-",
        "Bid": "-",
        "Matchtype": "-",
        "Clicks": "0",
        "Impr.": "0",
        "Conv.": "0"
      },
      {
        "Status": "Overall total",
        "Keyword": "-",
        "Campaign": "-",
        "Adgroup": "-",
        "BidStrategyType": "-",
        "Bid": "-",
        "Matchtype": "-",
        "Clicks": "0",
        "Impr.": "0",
        "Conv.": "0"
      }
    ]
  }
}

It Should return something like this

Keyword=>Toaletna voda
Keyword=>lyžička
Campaign=>Lešenari
Campaign=>Lešenari
Adgroup=>Lešenaris
Adgroup=>Lešenaris
Bid=>0.05
Bid=>0.05
Clicks=>0
Clicks=>0
Impr.=>0
Impr.=>0
Conv.=>0
Conv.=>0

It should do same thing as this function, it takes array of names, and find values that are append to it in that xml file

public function LoadXmlReport($adSystemColumns = array())
    {
        require "config.php";

        $xmlfile = simplexml_load_file(dirname(__FILE__) . "/xmlfile.xml");
        foreach ($xmlfile as $key => $value)
            foreach ($value as $columnName => $item) {
                if ($item == "-") {
                    break;
                } elseif (array_search($columnName, $bing) !== FALSE)
                    $this->report[$columnName][] = $item;
            }

        foreach ($this->report as $key => $value)
            foreach ($value as $index => $item)
                echo $key."=>".$item."<br/>";
    }

those are the values

$bing = array(
"Adgroup",
"Campaign",
"Keyword",
"Clicks",
"Impr.",
"Conv.",
"Bid",
"Adgroup"
);
4
  • How about php.net/manual/en/function.json-decode.php ? Commented Nov 14, 2016 at 23:46
  • Okay, the question is probably incorrectly formulated. I used json_decode, but then I got something that need like 3 or 4 nested foreach loops, and I need help with that. Commented Nov 14, 2016 at 23:49
  • Your desired output is still unclear. Is it dictionary with multiple same keys or arrays of pairs? Commented Nov 14, 2016 at 23:54
  • Probably Dictionary with multiple same keys. For example I have an excel file, one cell has name Keyword so I want value of every cell below assign to array["Keyword"][]. And actually, i have working method for csv format, but when i am looking on my xml method I am not sure it is possible, because if the xml file is nested one more time then I need another loop. Commented Nov 15, 2016 at 0:05

1 Answer 1

2
$json = json_decode($jsonString, true);
$result = [];
array_walk_recursive($json, function ($value, $key) use (&$result) {
    $result[$key][] = $value;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that looks like a interesting function, we will see If I can come with some working code.

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.