Вытащить значения из json

Статус
В этой теме нельзя размещать новые ответы.

yaski

web3
Регистрация
21 Фев 2010
Сообщения
658
Реакции
500
Приходит вот такая строка json
$tmp = json_decode($_POST["checks"], true);

Код:
string(5927) "[ { "address": "0xf2221792ae03576605a946f8a62b677138c5c1d9", "blockHash": "0x09eae34963e8a197a814e934cb0263c5692366fcbb2ec7074ecaaacfbd8f8c25", "blockNumber": 4105402, "data": "0x", "logIndex": 38, "removed": false, "topics": [ "0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f", "0x0000000000000000000000000000000000000000000000000000000000000001" ], "transactionHash": "0x5a3b92c6a6bca48dd103b88f43b5889af6853fac92f2bfec57a5108451b409fd", "transactionIndex": 35 }, { "address": "0xf2221792ae03576605a946f8a62b677138c5c1d9", "blockHash": "0x45c26545cd0ed04128537e68c38797fdd16e57f1728daec4341f9ac47358ecf3", "blockNumber": 4124532, "data": "0x", "logIndex": 54, "removed": false, "topics": [ "0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f", "0x0000000000000000000000000000000000000000000000000000000000000002" ], "transactionHash": "0x58bc22a296b3c7c8d3f253d6621548571e9be621d3cbf5fd793099f8346ba486", "transactionIndex": 41 }, { "address": "0xf2221792ae03576605a946f8a62b677138c5c1d9", "blockHash": "0x6d7d772776650a6a375bbdbe2ef8f2b84bbed84fbe7ac3ff85c6aec53cf77ddc", "blockNumber": 4124817, "data": "0x", "logIndex": 58, "removed": false, "topics": [ "0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f", "0x0000000000000000000000000000000000000000000000000000000000000002" ], "transactionHash": "0x21b59fd3542c9b30eb6ac280e894a0b7381c970be294e7554e6404560976f42c", "transactionIndex": 53 } ]

Красивый вывод

Код:
[
  {
    "address": "0xf2221792ae03576605a946f8a62b677138c5c1d9",
    "blockHash": "0x09eae34963e8a197a814e934cb0263c5692366fcbb2ec7074ecaaacfbd8f8c25",
    "blockNumber": 4105402,
    "data": "0x",
    "logIndex": 38,
    "removed": false,
    "topics": [
      "0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f",
      "0x0000000000000000000000000000000000000000000000000000000000000001"
    ],
    "transactionHash": "0x5a3b92c6a6bca48dd103b88f43b5889af6853fac92f2bfec57a5108451b409fd",
    "transactionIndex": 35
  },
  {
    "address": "0xf2221792ae03576605a946f8a62b677138c5c1d9",
    "blockHash": "0x45c26545cd0ed04128537e68c38797fdd16e57f1728daec4341f9ac47358ecf3",
    "blockNumber": 4124532,
    "data": "0x",
    "logIndex": 54,
    "removed": false,
    "topics": [
      "0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f",
      "0x0000000000000000000000000000000000000000000000000000000000000002"
    ],
    "transactionHash": "0x58bc22a296b3c7c8d3f253d6621548571e9be621d3cbf5fd793099f8346ba486",
    "transactionIndex": 41
  },
  {
    "address": "0xf2221792ae03576605a946f8a62b677138c5c1d9",
    "blockHash": "0x6d7d772776650a6a375bbdbe2ef8f2b84bbed84fbe7ac3ff85c6aec53cf77ddc",
    "blockNumber": 4124817,
    "data": "0x",
    "logIndex": 58,
    "removed": false,
    "topics": [
      "0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f",
      "0x0000000000000000000000000000000000000000000000000000000000000002"
    ],
    "transactionHash": "0x21b59fd3542c9b30eb6ac280e894a0b7381c970be294e7554e6404560976f42c",
    "transactionIndex": 53
  }
]

Помогите с циклом foreach. Нужно вытащить в переменные $blockNumber и $topics[1] соответствующие значения.
 
PHP:
// $jsonArray - массив данных

foreach ($jsonArray as $obj) {
    echo sprintf ("Block Number : %d\r\nTopic : %s\r\n-----\r\n", $obj->blockNumber, $obj->topics[1]);
}
 
PHP:
// $jsonArray - массив данных

foreach ($jsonArray as $obj) {
    echo sprintf ("Block Number : %d\r\nTopic : %s\r\n-----\r\n", $obj->blockNumber, $obj->topics[1]);
}
варнинг выдает
Код:
Invalid argument supplied for foreach() in
в строке
Код:
foreach ($tmp as $obj) {
 
Возможно в каких-то вариантах выполнения скрипта переменная $tmp не является массивом. Протестил у себя, у меня все работает.

PHP:
<?php
$tmp = json_decode ('[ { "address": "0xf2221792ae03576605a946f8a62b677138c5c1d9", "blockHash": "0x09eae34963e8a197a814e934cb0263c5692366fcbb2ec7074ecaaacfbd8f8c25", "blockNumber": 4105402, "data": "0x", "logIndex": 38, "removed": false, "topics": [ "0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f", "0x0000000000000000000000000000000000000000000000000000000000000001" ], "transactionHash": "0x5a3b92c6a6bca48dd103b88f43b5889af6853fac92f2bfec57a5108451b409fd", "transactionIndex": 35 }, { "address": "0xf2221792ae03576605a946f8a62b677138c5c1d9", "blockHash": "0x45c26545cd0ed04128537e68c38797fdd16e57f1728daec4341f9ac47358ecf3", "blockNumber": 4124532, "data": "0x", "logIndex": 54, "removed": false, "topics": [ "0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f", "0x0000000000000000000000000000000000000000000000000000000000000002" ], "transactionHash": "0x58bc22a296b3c7c8d3f253d6621548571e9be621d3cbf5fd793099f8346ba486", "transactionIndex": 41 }, { "address": "0xf2221792ae03576605a946f8a62b677138c5c1d9", "blockHash": "0x6d7d772776650a6a375bbdbe2ef8f2b84bbed84fbe7ac3ff85c6aec53cf77ddc", "blockNumber": 4124817, "data": "0x", "logIndex": 58, "removed": false, "topics": [ "0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f", "0x0000000000000000000000000000000000000000000000000000000000000002" ], "transactionHash": "0x21b59fd3542c9b30eb6ac280e894a0b7381c970be294e7554e6404560976f42c", "transactionIndex": 53 } ]');

echo '<pre>';
foreach ($tmp as $obj) {
    echo sprintf ("Block Number : %d\r\nTopic : %s\r\n-----\r\n", $obj->blockNumber, $obj->topics[1]);
}


Выдача:
Код:
Block Number : 4105402
Topic : 0x0000000000000000000000000000000000000000000000000000000000000001
-----
Block Number : 4124532
Topic : 0x0000000000000000000000000000000000000000000000000000000000000002
-----
Block Number : 4124817
Topic : 0x0000000000000000000000000000000000000000000000000000000000000002
-----
 
Если присваиваю строку переменной напрямую, то да, работает, а если записываю из POST, то не работает.
PHP:
$tmp = json_decode($_POST["checks"], true);
$tmp_array = explode("\n", $_POST["checks"]);

echo is_array($_POST["checks"]) ? 'Массив' : 'Не массив';
echo is_object($_POST["checks"]) ? 'Объект' : 'Не объект';

foreach ($tmp_array as $obj) {
  echo sprintf ("Block Number : %d\r\nTopic : %s\r\n-----\r\n", $obj->blockNumber, $obj->topics[1]);
}

$_POST["checks" - Не массив Не объект
$tmp - Не массив Не объект
$tmp_array - Массив Не объект
 
Бро, вывод очевиден. У тебя методом post не передаются никакие данные. Эта переменная по определению - массив. Если она не является массивом, то, по всей видимости, она пуста.

UPD. Попробуй сделать var_dump ($_POST), чтобы иметь представление, что у тебя там творится.
 
var_dump($_POST);
array(1) { ["checks"]=> string(6414) ""[\n {\n \"address\": \"0xf2221792ae03576605a946f8a62b677138c5c1d9\",\n \"blockHash\": \"0x09eae34963e8a197a814e934cb0263c5692366fcbb2ec7074ecaaacfbd8f8c25\",\n \"blockNumber\": 4105402,\n \"data\": \"0x\",\n \"logIndex\": 38,\n \"removed\": false,\n \"topics\": [\n \"0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f\",\n \"0x0000000000000000000000000000000000000000000000000000000000000001\"\n ],\n \"transactionHash\": \"0x5a3b92c6a6bca48dd103b88f43b5889af6853fac92f2bfec57a5108451b409fd\",\n \"transactionIndex\": 35\n },\n {\n \"address\": \"0xf2221792ae03576605a946f8a62b677138c5c1d9\",\n \"blockHash\": \"0x45c26545cd0ed04128537e68c38797fdd16e57f1728daec4341f9ac47358ecf3\",\n \"blockNumber\": 4124532,\n \"data\": \"0x\",\n \"logIndex\": 54,\n \"removed\": false,\n \"topics\": [\n \"0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f\",\n \"0x0000000000000000000000000000000000000000000000000000000000000002\"\n ],\n \"transactionHash\": \"0x58bc22a296b3c7c8d3f253d6621548571e9be621d3cbf5fd793099f8346ba486\",\n \"transactionIndex\": 41\n },\n {\n \"address\": \"0xf2221792ae03576605a946f8a62b677138c5c1d9\",\n \"blockHash\": \"0x6d7d772776650a6a375bbdbe2ef8f2b84bbed84fbe7ac3ff85c6aec53cf77ddc\",\n \"blockNumber\": 4124817,\n \"data\": \"0x\",\n \"logIndex\": 58,\n \"removed\": false,\n \"topics\": [\n \"0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f\",\n \"0x0000000000000000000000000000000000000000000000000000000000000002\"\n ],\n \"transactionHash\": \"0x21b59fd3542c9b30eb6ac280e894a0b7381c970be294e7554e6404560976f42c\",\n \"transactionIndex\": 53\n },\n {\n \"address\": \"0xf2221792ae03576605a946f8a62b677138c5c1d9\",\n \"blockHash\": \"0x512b5543f56d77753ee94eb2c1c070a4ea6a447b265b3be73a552627e813e31b\",\n \"blockNumber\": 4124911,\n \"data\": \"0x\",\n \"logIndex\": 25,\n \"removed\": false,\n \"topics\": [\n \"0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f\",\n \"0x0000000000000000000000000000000000000000000000000000000000000002\"\n ],\n \"transactionHash\": \"0x9e2dbd9f91b822509918734c71483693d7e556546f31fe19468d9b39ecbfa3eb\",\n \"transactionIndex\": 31\n },\n {\n \"address\": \"0xf2221792ae03576605a946f8a62b677138c5c1d9\",\n \"blockHash\": \"0x27cad5db758ad1249fc02ce4f3cbe68ce3c767106a26ebf346bb502f55d3d246\",\n \"blockNumber\": 4124967,\n \"data\": \"0x\",\n \"logIndex\": 52,\n \"removed\": false,\n \"topics\": [\n \"0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f\",\n \"0x0000000000000000000000000000000000000000000000000000000000000001\"\n ],\n \"transactionHash\": \"0xce5ab4b4046f52f2c1c2be7f1539cf3e860026c2bed104605ea88f7d16b1f394\",\n \"transactionIndex\": 37\n },\n {\n \"address\": \"0xf2221792ae03576605a946f8a62b677138c5c1d9\",\n \"blockHash\": \"0x7b6e659e4e566452a4c9fd9bcca0dd8b0f090374db035cea5e717eda923653c8\",\n \"blockNumber\": 4129494,\n \"data\": \"0x\",\n \"logIndex\": 71,\n \"removed\": false,\n \"topics\": [\n \"0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f\",\n \"0x0000000000000000000000000000000000000000000000000000000000000003\"\n ],\n \"transactionHash\": \"0x61388b3a1025c40e9cc10dbb238de67c18eb2dc5700668aa8df55e7fd3be22cd\",\n \"transactionIndex\": 32\n },\n {\n \"address\": \"0xf2221792ae03576605a946f8a62b677138c5c1d9\",\n \"blockHash\": \"0xa27134c8e4aedb1e9971f420318d24192b09f005d61cec67cac98eaa20c043b2\",\n \"blockNumber\": 4129517,\n \"data\": \"0x\",\n \"logIndex\": 72,\n \"removed\": false,\n \"topics\": [\n \"0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f\",\n \"0x0000000000000000000000000000000000000000000000000000000000000002\"\n ],\n \"transactionHash\": \"0x103a469833f101839d7b153c36d3200b048aaffae0827e47037062cf5e29b379\",\n \"transactionIndex\": 42\n },\n {\n \"address\": \"0xf2221792ae03576605a946f8a62b677138c5c1d9\",\n \"blockHash\": \"0x08963e1dc36064ec085864737f1ad3e1b0d5766318335556520fb583cb0605b7\",\n \"blockNumber\": 4130619,\n \"data\": \"0x\",\n \"logIndex\": 43,\n \"removed\": false,\n \"topics\": [\n \"0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f\",\n \"0x0000000000000000000000000000000000000000000000000000000000000004\"\n ],\n \"transactionHash\": \"0xc7dea01e1f6fbc63d6140d31644ded7c6b5001cfc964cc5d0830556327ce528a\",\n \"transactionIndex\": 63\n },\n {\n \"address\": \"0xf2221792ae03576605a946f8a62b677138c5c1d9\",\n \"blockHash\": \"0x6be6db8bae2103515edc2382efe5d0a9b0b33f9ebba155c7be05e126691d9e1d\",\n \"blockNumber\": 4130636,\n \"data\": \"0x\",\n \"logIndex\": 27,\n \"removed\": false,\n \"topics\": [\n \"0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f\",\n \"0x0000000000000000000000000000000000000000000000000000000000000003\"\n ],\n \"transactionHash\": \"0x915e062c81e61df2fd85a50aa1a48aef1c82460702e36210de3caea4db809cc4\",\n \"transactionIndex\": 28\n },\n {\n \"address\": \"0xf2221792ae03576605a946f8a62b677138c5c1d9\",\n \"blockHash\": \"0xfc7ea4157cd1d644934134127ddcab8f03e3f6c89f3ea4e20920aacf5df2b2fd\",\n \"blockNumber\": 4144609,\n \"data\": \"0x\",\n \"logIndex\": 2,\n \"removed\": false,\n \"topics\": [\n \"0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f\",\n \"0x0000000000000000000000000000000000000000000000000000000000000005\"\n ],\n \"transactionHash\": \"0x718c4a55d1892965e65b276927ea7233fee16b12e6a3f45cf4546120a2ab9921\",\n \"transactionIndex\": 7\n },\n {\n \"address\": \"0xf2221792ae03576605a946f8a62b677138c5c1d9\",\n \"blockHash\": \"0xfc7ea4157cd1d644934134127ddcab8f03e3f6c89f3ea4e20920aacf5df2b2fd\",\n \"blockNumber\": 4144609,\n \"data\": \"0x\",\n \"logIndex\": 3,\n \"removed\": false,\n \"topics\": [\n \"0xa4aee4c40a9346000546ce1743f64d90b90bd769fa0f9e02381dbbb95cb8533f\",\n \"0x0000000000000000000000000000000000000000000000000000000000000006\"\n ],\n \"transactionHash\": \"0xf1bc0cd720bfc23cbcd5ec089f1aacec5694af0f08f1c5805576a0ea9b6788aa\",\n \"transactionIndex\": 8\n }\n]"" }

Вот что приходит
 
Ну вот, берем последний приведенный тобой скрипт. Подставляем значение $tmp в цикл: foreach ($tmp as $obj). И еще момент. Вижу, ты упорно пытаешься представить результат json_decode, как ассоциативный массив, потому в foreach нужно выбирать именно элементы массива, а не объекта. Короче говоря...

Первый вариант:
PHP:
$tmp = json_decode ($_POST['checks'], true);
echo '<pre>';
foreach ($tmp as $obj) {
    echo sprintf ("Block Number : %d\r\nTopic : %s\r\n-----\r\n", $obj['blockNumber'], $obj['topics'][1]);
}

Второй вариант:
PHP:
$tmp = json_decode ($_POST['checks']);
echo '<pre>';
foreach ($tmp as $obj) {
    echo sprintf ("Block Number : %d\r\nTopic : %s\r\n-----\r\n", $obj->blockNumber, $obj->topics[1]);
}
 
PHP:
if (!empty ($_POST['checks'])) {
    if (false === ($tmp = json_decode ($_POST['checks']))) {
        die ('Invalid conversion from string to JSON-based value.');
    }

    echo '<pre>';

    if (!is_array ($tmp)) {
        echo "Unnable to process value\r\n";
        echo var_export ($_POST['checks']) . "\r\n";
        die (var_export ($tmp . "\r\n</pre>"));
    } else {
        foreach ($tmp as $obj) {
            echo sprintf ("Block Number : %d\r\nTopic : %s\r\n-----\r\n", $obj->blockNumber, $obj->topics[1]);
        }
    }
} else {
    die ('There are no data in POST variable "checks"');
}

Попробуй так. Тут уже учтено все, что только можно было.
 
Статус
В этой теме нельзя размещать новые ответы.
Назад
Сверху