Unable to get number of likes in an array php - php

I am using the following code to get the number of likes on a page.
<?php
$site="http://graph.facebook.com/?ids=http%3a%2f%2fXXXXXXXX/svce.php";
$graph= file_get_contents($site);
$json_string=$graph;
$array = json_decode($json_string, true);
//echo "<pre>";
//print_r($array);
$var = $array['shares'];
echo $var;
?>
But whenever i try to echo out the following code. I always get an unidentified index Notice which is as following: Notice: Undefined index: shares in C:\xampp\htdocs\graphapi.php on line 19
Where am i going wrong?
Here's the print_r output:
Array
(
[http://xxxxxxxxx/svce.php] => Array
(
[id] => http://xxxxxxxxx/svce.php
[shares] => 7
[comments] => 3
)
)

According your print out looks like there is an array more in $array. Try this;
echo $array['http://xxxxxxxxx/svce.php']['shares'];

You have to use the site-name as an key before.
Structure:
- http://example.com
- id
- shares
This means in PHP:
$array["http://example.com/path/to/site"]["shares"];

Related

Stuck in echoing data out of variable CodeIgniter

I try to get some information out of my database to my webpage. Everything seems to be fine but there is one thing that doesn't want to go right. I put all my information out my database into $data. When i do this
print_r($data);
My webpage gives me this:
(
[0] => stdClass Object
(
[reparatie_id] => 19
[customer_id] => 4
[medewerker] => 4
[name] => Joost
)
)
Everything seems to be good but when i try to do this:
echo $data->voornaam;
I keep getting this error
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: reparaties/cases.php
Line Number: 7
Backtrace:
File: C:\Ampps\www\beco\application\views\reparaties\cases.php
Line: 7
Function: _error_handler
File: C:\Ampps\www\beco\application\controllers\Reparaties.php
Line: 57
Function: view
File: C:\Ampps\www\beco\public\index.php
Line: 315
Function: require_once
Since your $data is a single-dimensional-array,so it need to be-
$data[0]->reparatie_id;
$data[0]->customer_id;
$data[0]->medewerker;
$data[0]->name;//so on for other indexes
Actually the $data array has an object at 0 position. So you need to any property of object. do like this:
<?php
$data[0]->reparatie_id;
$data[0]->customer_id;
$data[0]->medewerker;
$data[0]->name; ?>
output will be:
19, 4, 4, joost
First of all, your $data array does not have the voornaam element in it. So, assuming you want to echo out the elements that are inside the array, you would use the foreach array, like this:
foreach($data as $value) {
echo $value->name;
echo $value->voorname; //if it exists
}
But, if you just want to access that single element from the array then you would do this:
echo $data[0]->name;

numeric Json decode not work

i have below json code
{"1":1,"5":1}
when i decode the above json i got object array using the below php statement.
$array_val = (array)json_decode($price);
i got a below array.
Array
(
[1] => 1
[5] => 1
)
but the below statement does not work
echo $array_val[1];
the below error occurred.
Undefined offset: 1
How to resolve this issue?
try this DEMO
PHP
$json = '{"1":1,"5":1}';
$array_val=json_decode($price, true);
echo $array_val[1];
OUTPUT :
1
Note that json_decode($string) returns an object, not an array (which is why your code doesn't behave).
To return an array instead, use:
$arr = json_decode($string, true);
See also http://php.net/manual/en/function.json-decode.php
You can get this using below code
$array_val=json_decode($price);
echo $array_val->{1}
OR
$array_val=json_decode($price,true);
echo $array_val[1]

JSON decode, Parse JSON response and Undefined Index

Trying to use some decoded json data, but I'm unable to extract it to use. I've looked at other examples that should work, but haven't worked for me.
What am I missing?
(I'm trying to do what is in the first answer of How to parse json response from CURL )
Raw JSON
{"CustomerOriginId":123456}
JSON Decode:
$result = json_decode($head, true);
Print_R results (print_r($result);):
Array ( [CustomerOriginId] => 123456 )
Var_Dump results (var_dump($result);):
array(1) { ["CustomerOriginId"]=> int(123456) }
My attempts to extract the data for use:
Attempt 1
Attempt 1 Code:
$test45 = $result["CustomerOriginID"];
echo $test45;
Attempt 1 Error:
Notice: Undefined index: CustomerOriginID
Attempt 2
Attempt 2 Code:
$test46 = $result['CustomerOriginID'];
echo $test46;
Attempt 2 Result:
Notice: Undefined index: CustomerOriginID
Attempt 3
Attempt 3 Code:
$test47 = $result[0]['CustomerOriginID'];
echo $test47;
Attempt 3 Result:
Notice: Undefined offset: 0
Attempt 4
Attempt 4 Code:
$test48 = $result[1]['CustomerOriginID'];
echo $test48;
Attempt 4 Result:
Notice: Undefined offset: 1
I'm sure it's something small, but I haven't found an answer as of yet.
Cheers!
Undefined Index usually means you are accessing the array value the wrong way.
The index must match CustomerOriginId or it will be undefined.
Try this:
$json='{ "CustomerOriginId" : 123456 }';
$result = json_decode($json, true);
$CustomerOriginId = $result['CustomerOriginId'];
echo 'CustomerOriginId = '.$CustomerOriginId;
or without associative array
$json='{ "CustomerOriginId" : 123456 }';
$result = json_decode($json);
$CustomerOriginId = $result->CustomerOriginId;
echo 'CustomerOriginId = '.$CustomerOriginId;
The index for your array is "CustomerOriginId", not "CustomerOriginID" (note the case).
$json = '{"CustomerOriginId":123456}';
$array = json_decode($json, true);
print $array['CustomerOriginId']; // outputs 123456
this works for me:
$x = json_decode('{"CustomerOriginId":123456}', true);
print_r($x);
print $x['CustomerOriginId'];
output:
Array
(
[CustomerOriginId] => 123456
)
123456

Cannot use string offset as an array in

I have an array $aMethods whose print_r output is this:
Array
(
[0] => Array
(
[pattern] =>
[return_media] => 1
[return_name] =>
)
)
I'm trying to access 'return_media' with this code:
$iReturnMedia = $aMethods[0]->return_media;
echo $iReturnMedia;
Also, when I tried this:
$iReturnMedia = $aMethods[0]['return_media'];
I get an error stating: Cannot use string offset as an array in...
But it's not working, $iReturnMedia comes back as blank. Could someone tell me what I'm doing wrong here?
EDIT: $aMethods is set in a foreach loop as such:
foreach ($aMethodList as $sMethodGroup => $aMethods) { //insert code from above }
You need to use:
$iReturnMedia = $aMethods[0]['return_media'];
The operation -> is for accessing object properties. Since you're just dealing with nested arrays, you need to index them with [].
Access the array value by key.
$iReturnMedia = $aMethods[0]['return_media'];
echo $iReturnMedia;
Your accessing it as if it was an object in an array, you do it like:
$iReturnMedia = $aMethods[0]['return_media'];
echo $iReturnMedia;
Try this,
$iReturnMedia = $aMethodList[$sMethodGroup][0]['return_media'];
echo $iReturnMedia;
Try to var_dump($aMethods) . It will be give exactly idea of that array...
find below the code to access the array values -
foreach ($aMethodList as $sMethodGroup => $aMethods) {
echo $aMethods[0]['return_media'];
}

a stupid 'important' question about php $_SESSION array

I have 2 files that put something in the $_SESSION array.
file1.php
<?php
session_start();
$_SESSION[] = 'Hi';
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
That prints
Array
(
[0] => Hi
)
And file2.php that is similar to file1
<?php
session_start();
$_SESSION[] = 'There!';
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
I suppose to go to file1 at first and then move to file2.
Printing $_SESSION in file2 should output
Array
(
[0] => Hi
[1] => There!
)
am I wrong?
I've to mention that I get the notice: Unknown skipping numeric key 0 in Unknown on line 0.
And the register_globals in my php.ini is set to Off.
As I see in the comments for someone of you file2 prints an array of 2 items and for someone else (like me) the 'hi' items get lost. This seems to happen, but not for Marc B, only if we use a number as index of the session array, not with a string.
For Marc B the session behaves as I expected. Can you post your php.ini here? So I can compare yours with mine?
Superglobals like $_SESSION are not normal arrays. You should store an array inside $_SESSION, like so:
file 1: $_SESSION['foo'][] = 'Hi!';
file 2: $_SESSION['foo'][] = 'there';
You aren't giving $_SESSION the appropriate key.
No, that should work. In fact, if you just kept reloading file1, you'd just get a series of "Hi", "Hi", "Hi", etc.. array entries.
Is there a particular reason why you did
$_SESSION[]='Hi' instead of $_SESSION["Greet"]='Hi'?
I have tested your code. when i started file1.php i have the following:
Array
(
[0] => hi
)
with the following notice: Unknown skipping numeric key 0 in Unknown on line 0
and after that i went on to file2.php i have the following:
Array
(
[0] => there!
)
with the same notice. simply put to answer your question you are wrong :).
If you added the indexes ("greet" and "meet" respectively) to the session variable this would be the output on page 1:
Array
(
[greet]=> hi
)
and when you go on file2.php you would have:
Array
(
[greet] => hi
[meet] => there!
)
file1:
<?php
session_start();
$_SESSION['0'] = 'Hi';
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
That prints
Array
(
[0] => Hi
)
And file2.php that is similar to file1 but different session index
<?php
session_start();
$_SESSION['1'] = 'There!';
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
Now this prints
Array
(
[0] => Hi
[1] => There!
)

Categories