0

hoping someone here might be able to advise on what the syntax should be to get the data in the transactions array within the data object below

{
  "data": {
       "pages":10,
       "current":1,
       "token":"1234-1234-1234-1234",
       "transactions":[{"id"}]
   }
}

im trying to output the data

    <?php foreach ($someArray as $key => $value){
    echo "<tr><td>". $value[columns][tranid];
    echo "</td><td>". $value[recordtype];
    echo "</td><td>". $value[columns][trandate];
    echo "</td><td>". $value[columns][poref];
    echo "</td><td>". $value[columns][ref][name];
    echo "</td><td>$". number_format($value[columns][amount], 0.00, '.', ',');
    echo "</td><td><a class='woocommerce-button button' href='https://test.com/?a=print&token=". $pmToken ."&i=". base64_encode("PM-" + $value[columns][internalid][internalid]) ."' target='_new'>Print</a>";
    echo "</td></tr>";
    }
    ?>

Im so new to PHP Im not sure how to grab the data I assume in alot of other languages its just a case of

$someArray.transactions would be the way to do it, but I cant figure out how to get it in PHP

michaelitoh
  • 2,317
  • 14
  • 26
user125264
  • 1,809
  • 2
  • 27
  • 54
  • Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – user3942918 Aug 16 '18 at 05:10

3 Answers3

0

If you want to convert object into array, so you should use php function

$object = json_decode(json_encode($array), FALSE);

0

Lets say your object is:

$obj = '{
"data":
{
"pages":10,
"current":1,
"token":"1234-1234-1234-1234",
"transactions":[{"id"}]
}
}';


$arr = (array)$obj;

var_dump($arr); //This will be displayed as array

Working example: http://codepad.org/uCIB7kfh

Learner
  • 723
  • 3
  • 13
  • 36
0

Use json_decode()

In your case, put $s = '{"key":"value"}' $someArray = json_decode($s); before the for loop, you can then add var_dump($someArray); to view the structure and fetch the corresponding data value.

Marlon Chow
  • 1
  • 1
  • 1