Ricardo
Ricardo

Reputation: 1670

How to access values inside an object?

{
   "http://url.com": {
      "id": "http://url.com",
      "shares": 11111
   }
}

I need to access 'id' and 'shares' and seems like the normal

($results->shares)

doesn't work here, any suggestions? thanks.

EDIT with full code:

<?php 

include __DIR__ . '/config.php';

function do_curl($start_index, $stop_index) {

    // Do query here to get all pages with ids between start index and stop index

$sql = "SELECT * FROM mmakers WHERE id >= $start_index and id <= $stop_index";
$result = mysql_query($sql) or die(mysql_error());
    while ($row = mysql_fetch_array($result))
    {
        $fanpages_query[] = 'https://graph.facebook.com/?ids='.$row['domain'];
    }
    return $fanpages_query;

}


$fanpages = do_curl($_GET['start_index'], $_GET['stop_index']);
$fanpages_count = count($fanpages);


$curl_arr = array();
$master = curl_multi_init();

for($i = 0; $i < $fanpages_count; $i++)
{
    $url = $fanpages[$i];
    $curl_arr[$i] = curl_init($url);
    curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($master, $curl_arr[$i]);
}
do {
    curl_multi_exec($master,$running);
} while($running > 0);


for($i = 0; $i < $fanpages_count; $i++)
{
    $results = json_decode(curl_multi_getcontent($curl_arr[$i]));

    $sql_mm = "SELECT * FROM mmakers WHERE domain='".($results->id)."'";
    $result_mm = mysql_query($sql_mm) or die(mysql_error());
    $row_mm = mysql_fetch_array($result_mm);

    if ($row_mm['type'] == 'auto') {
    $sql_d = "SELECT domain FROM domains_list WHERE used='no' AND directory='".$row_mm['directory']."' AND type='auto' ORDER BY rand() LIMIT 1";
    $result_d = mysql_query($sql_d) or die(mysql_error());
    $row_d = mysql_fetch_array($result_d);
    } else {
    $sql_d = "SELECT domain FROM domains_list WHERE used='no' AND directory='".$row_mm['directory']."' AND type='jaa' ORDER BY rand() LIMIT 1";
    $result_d = mysql_query($sql_d) or die(mysql_error());
    $row_d = mysql_fetch_array($result_d);
    }

    mysql_query("UPDATE mmakers SET shares = '".($results->shares)."' WHERE id='".$row_mm['id']."'") or die(mysql_error());

}

?>

Upvotes: 0

Views: 106

Answers (6)

binaryLV
binaryLV

Reputation: 9122

When decoding JSON, set $assoc (second argument of json_decode()) to true:

$jsonData = json_decode($jsonText, true);
$url = 'http://url.com';
var_dump($jsonData[$url]['id']);
var_dump($jsonData[$url]['shares']);

Upvotes: 0

RobertPitt
RobertPitt

Reputation: 57278

$content = '{
   "http://url.com": {
      "id": "http://url.com",
      "shares": 11111
   }
}';

$object = json_decode($content);
$object->{'http://url.com'}->shares; //11111

Upvotes: 0

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

You can also do something like this...

$results = json_decode($json, true);
echo $results[0]["shares"];

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 817208

I suggest to decode the JSON as an array:

$results = json_decode($json, true);

Then assuming you don't know the URL, you have to loop over the array:

foreach($results as $url => $data) {
    echo $url, ' shares: ', $data['shares'];
}

If you know the URL, you can do

$results['http://url.com']['shares'];

Upvotes: 3

KingCrunch
KingCrunch

Reputation: 132071

You forgot one property.

echo $results->{"http://url.com"}->shares;

Upvotes: 1

Ēriks Daliba
Ēriks Daliba

Reputation: 718

Try this one:

$results->{"http://url.com"}->shares

Upvotes: 1

Related Questions