-1

I'm trying to append some JSON I'm getting from an API response to an email and I wish to output the JSON into a HTML table. The problem is the JSON response doesn't look like anything I've worked with before.

$vs_getClickPath = wp_remote_get('url');
$clickPath_body = wp_remote_retrieve_body( $vs_getClickPath );
$notification['message'] .= $clickPath_body;

This gives me the below:

[
  ["date\/time", "page name visited", "city", "region", "country", "company name\/isp", "identity", "search\/referral"],
  ["2016-05-17 10:56:05", "Home Page", "Buffalo", "New York", "United States", "I-evolve Technology Services", "", ""]
]

All of the keys come first in one array then in another array the values show up. Trying to work with indexes just gets the individual characters, foreach doesn't work. I've tried json_decode, but it doesn't output anything with print_r or var_dump.

Is there anyway I will be able to loop through this and output the data into a HTML table? The first array would be the table headers and every array after that would be the table rows.

WillardSolutions
  • 2,316
  • 4
  • 28
  • 38
user985952
  • 97
  • 2
  • 15

2 Answers2

1

I used json_decode($str, true) and it returned an array that can be worked with:

$str = '[["date\/time","page name visited","city","region","country","company name\/isp","identity","search\/referral"],["2016-05-17 10:56:05","Home Page","Buffalo","New York","United States","I-evolve Technology Services","",""]]';
$json = json_decode($str, true);
echo '<pre>';
print_r($json);

returns

Array
(
    [0] => Array
        (
            [0] => date/time
            [1] => page name visited
            [2] => city
            [3] => region
            [4] => country
            [5] => company name/isp
            [6] => identity
            [7] => search/referral
        )

    [1] => Array
        (
            [0] => 2016-05-17 10:56:05
            [1] => Home Page
            [2] => Buffalo
            [3] => New York
            [4] => United States
            [5] => I-evolve Technology Services
            [6] => 
            [7] => 
        )

)

Setting true in the call to json_decode() makes the function return an array. If you do not want an array, you can return an abject by leaving the option out of the call.

PHP's json_decode()

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • Oh yeah.. that's what I was trying before, but I just kept getting "1" back as my output. It seems it is something with Gravity Forms (the email plugin i'm using). When I try to use print_r in the email message it goes bonkers. Sorry should have caught that before posting – user985952 May 17 '16 at 16:17
  • @user985952: How are you trying to use `print_r`? Its second parameter controls whether it returns a string or just echos it out. – gen_Eric May 17 '16 at 16:21
  • @Rocket Hazmat I am trying this: $clickPath_body = wp_remote_retrieve_body( $vs_getClickPath ); $json = json_decode($clickPath_body, true); $notification['message'] .= print_r($json); – user985952 May 17 '16 at 16:22
  • Ah perfect, thank you @Rocket Hazmat. Using true as second param in print_r works :) – user985952 May 17 '16 at 16:25
  • 1
    @user985952: There's your issue! You need to do `print_r($json, true)`. Otherwise it *echos* the string and returns `true` (which converts to `1`). – gen_Eric May 17 '16 at 16:25
0

from your output, the result is an array of arrays

[
[
"date\/time", "page name visited", "city", "region", "country", "company name\/isp", "identity", "search\/referral" ], [
"2016-05-17 10:56:05", "Home Page", "Buffalo", "New York", "United States", "I-evolve Technology Services", "", "" ] ]

you cand find the values with a foreach inside a foreach.

foreach ($clickPath_body as $array) { foreach ($array as $string) { echo $string; } echo "<br>"; }