I am really struggling with arrays in general but I can't seem to do anything with this: https://www.easycron.com/document/list
I want to be able to access each "field". For example the [cron_job_name] for [2]. So in that example the result I'd be looking for is "Send newsletters".
I have been messing around all night long but I think the thing I am not getting past is that it has the [status] => success [cron_jobs] => Array at the top.
So far the code I have is this:
$json = file_get_contents("https://www.easycron.com/rest/list?token=[MY KEY]&sortby=cronId&order=desc");
$myArray = json_decode($json,true);
echo "<pre>";
print_r($myArray);
echo "</pre>";
All this really does though is gets me a copy of what is on that page.
if you are able to print_r the array response, then you can cycle
if($myArray['status'] ==='success') {
$crons = $myArray['cron_jobs'];
foreach($crons as $cron){
if($cron['cron_job_name'] == 'Send newsletters') {
// Do your proccessing
break;
}
}
}
Related
I'm having some problems getting the data through my PHP loop.
<?php
$url = 'https://www.fibalivestats.com/data/1653309/data.json';
$content = file_get_contents($url);
$json = json_decode($content, true);
?>
<?php
foreach($json['totallds']['sPoints'] as $item); {
echo $item;
}
?>
The error I'm getting is an array to string conversion error. What I'm trying to get is the data from the sPoints array that will give me a Top 5 points scorers for a basketball game.
I'll build a table for this in HTML later but for now, it's not displaying the data at all and I'm getting errors. I feel like I may have confused arrays and strings too. Any thoughts about what I'm doing wrong? JSON file can be found in the $url variable.
Also if it helps, here's the link to where I have gotten the data from and what context the Top 5 is from https://www.fibalivestats.com/u/NSS/1653309/lds.html
Thanks!
Your $item is an array, so you can't just echo it like that. You can, however, echo its columns, for example:
foreach($json['totallds']['sPoints'] as $item) {
echo $item['firstName'] . ' ' . $item['familyName'];
}
Notice the removed semicolon between the foreach () and {.
Well, array to string conversion error means you're trying to echo an array.
If you see the return of the url you are looking at, you can verify that the "sPoints" key returns an array with several objects.
Try to change echo to print_r or var_dump to view entire data or complete your business logic.
Try changing:
echo $item;
to:
var_dump($item);
I have a script which has output which looks like the following:
Which is something I've not experienced before. Normally the result would be the array and then the stdClass Object which I convert to an array using (array) $result. This one seems to be nested an extra 2 times however, so I have no idea how to access it so that I can turn each of those objects into an array.
So what I need to be able to achieve, for example, is that if I use the code echo $orders[0]->customer_id, the result would be 716'.
Could anybody please advise? My code is below if required. Thank you very much.
<?php
$client= new SoapClient('*Magento URL*');
$session_id = $client->login((object)array('username' => '*Magento Username*', 'apiKey' => '*Magento Password*'));
try {
$result = $client->salesOrderList((object)array('sessionId' => $session_id->result, 'filters' => null));
$orders = (array) $result;
echo '<pre>', print_r($orders), '</pre>';
} catch (SoapFault $e) {
echo 'Error: ', $e->getMessage(), '<hr>';
}
?>
$result is an object with a result property which is an object with a complexObjectArray property that is an integer indexed array of objects. So:
$orders = $result->result->complexObjectArray;
Then:
echo $orders[0]->customer_id;
If there is more than one item in the array then you would need to loop over it. If there is only ever one then include the 0 when defining $orders:
$orders = $result->result->complexObjectArray[0];
Then:
echo $orders->customer_id;
I'm trying to get the values of teams in the Json,I gave tried the code below but it returns NULL, Any ideas?
<?php
$url = 'http://bristolrugby.matchdaylive.com/tools/ajax/cache.php?';
$url.= 'type=Fixture&format=json&TeamId=25&Source=sfms&module=StatsRugbyMDS&CompSeason=2015&project=bristol';
$response = json_decode(file_get_contents($url));
$fixture_array = $response;
var_dump ($fixture_array->SoticFeed->Fixtures->Fixture[0]->Teams->Team);
echo '<br/><hr>';
$fix = $fixture_array->SoticFeed->Fixtures;
foreach($fix as $fix){
print $fix->Fixture->Teams->Team->TmnmDisplay . "<hr>";
}
?>
It seems like you got confused by the structure of the data. Your final loop is wrong, I think it should look like this:
$fix = $fixture_array->SoticFeed->Fixtures->Fixture;
foreach($fix as $fix_detail){
foreach($fix_detail->Teams->Team as $team){
print $team->TmnmDisplay . "<hr>";
}
}
This is assuming you want to display all team's names. If this is not what you intended please let me know.
hello your Json response return array in array in array and this mean you have to loop over each key , let me explain you more
if you
print_r($response["SoticFeed]")
You will get Array in Array , now again
print_r($response["SoticFeed"]["Fixtures"])
again You will get Array , now the last part
foreach($response["SoticFeed"]["Fixtures"]["Fixture"] as $key=>$value){
print_r($value)
}
I am requesting my output look like this:
Response
{
error_num: 0
error_details:
[
{
"zipcode": 98119
},
{
"zipcode": 98101
}
]
}
The values are irrelevant for this example.
My code looks like this:
$returndata = array('error_num' => $error_code);
$returndata['error_details'] = $error_msg;
$temp_data = array();
$temp_value = '';
foreach ($zipcodes_array as $value) {
//$temp_data['zipcode'] = $value;
//$temp_value .= json_encode($temp_data);
$temp_value .= '{"zipcode":$value},';
}
//$returndata['test'] = $temp_value;
$returndata['zipcodes'] = $temp_value;
echo json_encode($returndata);
My output varies depending on my different attempts (which you can see with the commented out things) but basically, I don't understand how the 3rd part (the part with the zipcodes) doesn't have a key or a definition before the first open bracket "["
Here is the output for the code above:
{"error_num":0,"error_details":"","zipcodes":"{\"zipcode\":11111},{\"zipcode\":11112},{\"zipcode\":11113},{\"zipcode\":22222},{\"zipcode\":33333},{\"zipcode\":77325},{\"zipcode\":77338},{\"zipcode\":77339},{\"zipcode\":77345},{\"zipcode\":77346},{\"zipcode\":77347},{\"zipcode\":77396},{\"zipcode\":81501},{\"zipcode\":81502},{\"zipcode\":81503},{\"zipcode\":81504},{\"zipcode\":81505},{\"zipcode\":81506},{\"zipcode\":81507},{\"zipcode\":81508},{\"zipcode\":81509},"}
Obviously i did not show the variables being filled/created because its through MySQL. The values are irrelevant. Its the format of the output i'm trying to get down. I don't understand how they have the "zipcode": part in between {} brackets inside another section which appears to be using JSON_ENCODE
It is close but see how it still has the "zipcodes": part in there defining what key those values are on? My question is, is the "response" above being requested by a partner actually in JSON_ENCODE format?? or is it in some custom format which I'll just have to make w/out using any json features of PHP? I can easily write that but based on the way it looks in the example above (the response) I was thinking it was JSON_ENCODE being used.
Also, it keeps putting the \'s in front of the " which isn't right either. I know it's probably doing that because i'm json_encode'ing a string. Hopefully you see what i'm trying to do.
If this is just custom stuff made to resemble JSON, I apologize. I've tried to ask the partner but I guess i'm not asking the right questions (or the right person). They can never seem to give me answers.
EDIT: notice my output also doesn't have any [ or ] in it. but some of my test JSON_ENCODE stuff has had those in it. I'm sure its just me failing here i just cant figure it out.
If you want your output to look like the JSON output at the very top of your question, write the code like this:
$returndata = array('error_num' => $error_code);
$returndata['error_details'] = array();
foreach ($zipcodes_array as $value) {
$returndata['error_details'][] = array('zipcode' => (int)$value);
}
echo 'Response ' . json_encode($returndata);
This will return JSON formatted like you requested above.
Response
{
error_num: 0
error_details:
[
{
"zipcode": 98119
},
{
"zipcode": 98101
}
]
}
Can you just use single ZipCode object as associative array, push all your small ZipCode objects to the ZipCodes array and encode whole data structure. Here is the code example:
$returndata = array(
'error_num' => $error_code,
'error_details' => array()
);
foreach ($zipcodes_array as $value) {
$returndata['error_details'][] = array('zipcode' => $value);
}
echo json_encode($returndata);
I been looking thru the posts here all day but can't figure out what I'm doing wrong. (I'm new to php and json)
Here is my code that work.
$json = '{"id":1234,"img":"1.jpg"}';
$data = json_decode($json, true);
echo $data["img"];
But when the json respond is this
$json = '{"demo1":[{"id":1234,"img":"1.jpg"}],"userId":1}';
it's a big harder for me. then img is a child of demo1? How to get it?
Thx. :)
Figuring out the array indices
As you're new to PHP, I'll explain how to figure out the array indces of the required array value. In PHP, there are many functions for debugging — print_r() and var_dump() are two of them. print_r() gives us a human-readable output of the supplied array, and var_dump() gives us a structured output along with the variable types and values.
In this case, print_r() should suffice:
$json = '{"demo1":[{"id":1234,"img":"1.jpg"}],"userId":1}';
$data = json_decode($json, true);
// wrap the output in <pre> tags to get a prettier output
echo '<pre>';
print_r($data);
echo '</pre>';
This will produce the following output:
Array
(
[demo1] => Array
(
[0] => Array
(
[id] => 1234
[img] => 1.jpg
)
)
[userId] => 1
)
From there, it should be pretty easy for you to figure out how to access the required vaule.
$data['demo1'][0]['img'];
Creating a helper function for ease of use
For ease of use, you can create a helper function to make this process easier. Whenever you want to view the contents of an array, you can simply call dump_array($array); and be done with it. No more messing around with <pre> tags or print_r().
Function code:
function dump_array($array) {
echo '<pre>' . print_r($array, TRUE) . '</pre>';
}
Usage example:
$arr = ['foo' => range('a','i'), 'bar' => range(1,9)];
dump_array($arr);
after decoding :
echo $data->demo[0]->img;
Basically, a { in JSON leads to a -> (it's an object).
And a [ to a [], it's an array.