How to parse information in PHP? - php

I have the following text below, what is the easiest way to parse through it?
if(typeof sa_inst.apiCB == 'function') sa_inst.apiCB({"AS":{"Query":"g","FullResults":1,"Results":[{"Type":"AS","Suggests":[{"Txt":"g<strong>oogle</strong>","Type":"AS","Sk":""},{"Txt":"g<strong>mail</strong>","Type":"AS","Sk":"AS1"},{"Txt":"g<strong>oogle</strong> <strong>maps</strong>","Type":"AS","Sk":"AS2"},{"Txt":"g<strong>oogle</strong> <strong>earth</strong>","Type":"AS","Sk":"AS3"},{"Txt":"g<strong>ames</strong>","Type":"AS","Sk":"AS4"},{"Txt":"g<strong>oogle</strong> <strong>images</strong>","Type":"AS","Sk":"AS5"},{"Txt":"g<strong>amestop</strong>","Type":"AS","Sk":"AS6"},{"Txt":"g<strong>rainger</strong>","Type":"AS","Sk":"AS7"}]}]}} /* pageview_candidate */);
I would like to only get what is after ("Txt":) and not including the <strong> or anything else. For example, form the text above I would like to get something like:
google, gmail, google maps, google images, etc
in an array. Help please?

The text is JSON. Use json_decode to parse it.
$str = '{"AS":{"Query":"g","FullResults":1,"Results":[{"Type":"AS","Suggests":[{"Txt":"g<strong>oogle</strong>","Type":"AS","Sk":""},{"Txt":"g<strong>mail</strong>","Type":"AS","Sk":"AS1"},{"Txt":"g<strong>oogle</strong> <strong>maps</strong>","Type":"AS","Sk":"AS2"},{"Txt":"g<strong>oogle</strong> <strong>earth</strong>","Type":"AS","Sk":"AS3"},{"Txt":"g<strong>ames</strong>","Type":"AS","Sk":"AS4"},{"Txt":"g<strong>oogle</strong> <strong>images</strong>","Type":"AS","Sk":"AS5"},{"Txt":"g<strong>amestop</strong>","Type":"AS","Sk":"AS6"},{"Txt":"g<strong>rainger</strong>","Type":"AS","Sk":"AS7"}]}]}}';
$result = json_decode($str);
//Show all 'Txt' values
foreach($result->AS->Results[0]->Suggests as $suggests)
echo "Txt contains: ".htmlspecialchars($suggests->Txt). "<br/>";
//Show full contents of result
echo "<pre>";
print_r($result);
This outputs values of Txt first:
Txt contains: g<strong>mail</strong>
Txt contains: g<strong>oogle</strong> <strong>maps</strong>
Txt contains: g<strong>oogle</strong> <strong>earth</strong>
Txt contains: g<strong>ames</strong>
Txt contains: g<strong>oogle</strong> <strong>images</strong>
Txt contains: g<strong>amestop</strong>
Txt contains: g<strong>rainger</strong>
And then the complete JSON data too:
stdClass Object
(
[AS] => stdClass Object
(
[Query] => g
[FullResults] => 1
[Results] => Array
(
[0] => stdClass Object
(
[Type] => AS
[Suggests] => Array
(
[0] => stdClass Object
(
[Txt] => google
[Type] => AS
[Sk] =>
)
[1] => stdClass Object
(
[Txt] => gmail
[Type] => AS
[Sk] => AS1
)
[2] => stdClass Object
(
[Txt] => google maps
[Type] => AS
[Sk] => AS2
)
[3] => stdClass Object
(
[Txt] => google earth
[Type] => AS
[Sk] => AS3
)
[4] => stdClass Object
(
[Txt] => games
[Type] => AS
[Sk] => AS4
)
[5] => stdClass Object
(
[Txt] => google images
[Type] => AS
[Sk] => AS5
)
[6] => stdClass Object
(
[Txt] => gamestop
[Type] => AS
[Sk] => AS6
)
[7] => stdClass Object
(
[Txt] => grainger
[Type] => AS
[Sk] => AS7
)
)
)
)
)
)

This will work, but I removed ' from 'function'
<?php
echo $text = 'if(typeof sa_inst.apiCB == function sa_inst.apiCB({"AS":{"Query":"g","FullResults":1,"Results":[{"Type":"AS","Suggests":[{"Txt":"g<strong>oogle</strong>","Type":"AS","Sk":""},{"Txt":"g<strong>mail</strong>","Type":"AS","Sk":"AS1"},{"Txt":"g<strong>oogle</strong> <strong>maps</strong>","Type":"AS","Sk":"AS2"},{"Txt":"g<strong>oogle</strong> <strong>earth</strong>","Type":"AS","Sk":"AS3"},{"Txt":"g<strong>ames</strong>","Type":"AS","Sk":"AS4"},{"Txt":"g<strong>oogle</strong> <strong>images</strong>","Type":"AS","Sk":"AS5"},{"Txt":"g<strong>amestop</strong>","Type":"AS","Sk":"AS6"},{"Txt":"g<strong>rainger</strong>","Type":"AS","Sk":"AS7"}]}]}} /* pageview_candidate */);';
echo '<br />';
$text = strip_tags($text); // remove html tags
$a = explode('"Txt":"', $text);
$app = array();
foreach ($a as $c) {
$b = explode('"', $c);
$app = $b[0];
}
unset($app[0]);
var_dump($app);
?>

Related

PHP stdClass Object - foreach

I'm using an API to get domain DNS details. The results printed show like this:
stdClass Object
(
[test.co.uk] => stdClass Object
(
[records] => Array
(
[0] => stdClass Object
(
[mname] => ns1.test.com.
[rname] => hostmaster.test.com.
[serial] => 12345678
[refresh] => 1800
[retry] => 900
[expire] => 1209600
[minimum-ttl] => 300
[ref] =>
[host] => test.co.uk
[type] => SOA
)
[1] => stdClass Object
(
[target] => ns1.test.com
[ref] =>
[host] => test.co.uk
[type] => NS
)
[2] => stdClass Object
(
[target] => ns2.test.com
[ref] =>
[host] => test.co.uk
[type] => NS
)
How can I turn each of the records ([0], [1], [2] etc) into variables? I am wanting to show them in a table
I have tried the below, but no result is shown
$Test=$PackageDNSInfo->test.co.uk->records[0]->mname;
echo $Test;
Just use foreach on it, just like you would on an array:
foreach ($PackageDNSInfo as $url => $data) {
foreach ($data->records as $record) {
$type = $record->type;
$host = $record->host;
// etc.
}
}
If you want to access a given URL's data directly, use curly braces:
echo $PackageDNSInfo->{'test.co.uk'}->records[0]->mname;

Loop through JSON arrays using PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I'm trying to loop through an array of moves for a Pokemon website. I'm using the API PokeApi (https://pokeapi.co/).
My question is how can I access the moves in these arrays using plain PHP.
I've tried using this just to call 1 move. But I don't know how to access data that's in arrays. Like "move->version-group-details".
$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data);
echo $pokemon->moves[0];
Thanks in advance :)
So you have two methods here you can do so when I run
$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data);
print_r($pokemon->moves[0]);
I get the result:
stdClass Object
(
[move] => stdClass Object
(
[name] => razor-wind
[url] => https://pokeapi.co/api/v2/move/13/
)
[version_group_details] => Array
(
[0] => stdClass Object
(
[level_learned_at] => 0
[move_learn_method] => stdClass Object
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => stdClass Object
(
[name] => crystal
[url] => https://pokeapi.co/api/v2/version-group/4/
)
)
[1] => stdClass Object
(
[level_learned_at] => 0
[move_learn_method] => stdClass Object
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => stdClass Object
(
[name] => gold-silver
[url] => https://pokeapi.co/api/v2/version-group/3/
)
)
)
)
If you want to access a moves name you will have to run $pokemon->moves[0]->move->name since we are getting an object returned. If you want to get the name inside the move_learn_method of version_group_details you will have to run
$pokemon->moves[0]->version_group_details[0]-> move_learn_method->name
Alternatively, if you want to return all arrays instead of objects just run this
$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data, true);
print_r($pokemon['moves'][0]);
This will now return
Array
(
[move] => Array
(
[name] => razor-wind
[url] => https://pokeapi.co/api/v2/move/13/
)
[version_group_details] => Array
(
[0] => Array
(
[level_learned_at] => 0
[move_learn_method] => Array
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => Array
(
[name] => crystal
[url] => https://pokeapi.co/api/v2/version-group/4/
)
)
[1] => Array
(
[level_learned_at] => 0
[move_learn_method] => Array
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => Array
(
[name] => gold-silver
[url] => https://pokeapi.co/api/v2/version-group/3/
)
)
)
)
So instead of having to use the object accessor -> you can access the data by using array notation so instead of
$pokemon->moves[0]->version_group_details[0]-> move_learn_method->name
you can now use:
$pokemon['moves']['version_group_details'][0]['move_learn_method']['name']
Hope that helped.

Parsing JSON Array Data

I'm trying to get data out of some JSON DATA. I'm using the following lines to decode it right now
$json_array = (array)(json_decode($response));
When I print my JSON Decoded array, I have the following data below:
I would like to get the details from the details section, where there is multiple sets of from/to_date's, and up/down numbers. Nothing I seem to do works though to get me to that data. I can print out the data from other areas like usage, but, I can't get into the details.
Array (
[resp_code] => SUCCESS
[caller_ref] => 2017092002282130006180
[server_ref] => 2017092002282169760291
[data] => stdClass Object (
[type] => monthly
[group_id] => 19
[device_id] => 32
[sn] => sn1234
[usages] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 22370
[down] => 119217
[ts] => 2017-09-01T00:00:00
)
)
[details] => stdClass Object (
[3] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 5522
[down] => 40301
[ts] => 2017-09-01T00:00:00
)
)
[2] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 6905
[down] => 32029
[ts] => 2017-09-01T00:00:00
)
)
)
)
)
Whats wrong with objects?
$obj = json_decode($response);
echo $obj->data->details[0]->from_date;
Or to loop it:
foreach ($obj->data->details as $item) {
echo $item->from_date;
// same goes for: to_date, up etc.
}
Simple and sexy!
Update:
It looks as if $item would be an array as well, so if you have problems try:
foreach ($obj->data->details as $item) {
echo $item[0]->from_date;
// same goes for: to_date, up etc.
}

HubSpot api json decode

I am trying to parse some data out of the Hubspot API response. The response looks like this json_decoded:
stdClass Object(
[addedAt] => 1411052909604
[vid] => 24
[canonical-vid] => 24
[merged-vids] => Array
(
)
[portal-id] => XXXXX
[is-contact] => 1
[profile-token] => AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[profile-url] => https://app.hubspot.com/contacts/XXXXX/lists/public/contact/_AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[properties] => stdClass Object
(
[lastname] => stdClass Object
(
[value] => testtt
)
[firstname] => stdClass Object
(
[value] => test
)
[lastmodifieddate] => stdClass Object
(
[value] => 1411052906670
)
)
[form-submissions] => Array
(
[0] => stdClass Object
(
[conversion-id] => 85d24dd2-9ee9-4d47-b8f3-3035acbd8f3b
[timestamp] => 1411052834097
[form-id] => fb16efd9-23cc-4511-889c-204fc8b41dba
[portal-id] => 401824
[page-url] => http://wbl-1.hs-sites.com/test
[canonical-url] => http://wbl-1.hs-sites.com/test
[content-type] => landing-page
[page-title] => test
[page-id] => 1570433242
[title] => Default Form (Sample)
[first-visit-url] => http://wbl-1.hs-sites.com/test
[first-visit-timestamp] => 1411052722970
[meta-data] => Array
(
)
)
)
[list-memberships] => Array
(
)
[identity-profiles] => Array
(
[0] => stdClass Object
(
[vid] => 24
[identities] => Array
(
[0] => stdClass Object
(
[type] => EMAIL
[value] => test#user.com
[timestamp] => 1411052834097
)
[1] => stdClass Object
(
[type] => LEAD_GUID
[value] => 0b6acf21-6cee-4c7b-b664-e65c11ee2d8e
[timestamp] => 1411052834201
)
)
)
)
[merge-audits] => Array
(
)
)
I'm looking specifically to try to dig out an email inside the indentities-profile area.
I've tried to do the following:
echo $results->contacts[0]->identity-profiles;
But it just gives me a value of 0
Then I try to go further into the array by doing:
echo $results->contacts[0]->identity-profiles[0];
But at that point - I get a parse error:
Parse error: syntax error, unexpected '['
What am I doing wrong? And how can I dig all the way down to
identity-profiles[0]->identities->[0]->value
which should equal: test#user.com
What am I missing?
As mentioned in the comment I would suggest to decode the JSON to an associative array by passing true as second parameter to json_decode. Example: json_decode($data, true) Than you could access your identity profiles by:
$results['contacts'][0]['identitiy-profiles']
If you still want to get the results as an object you have to access the properties the following way because they contain a -:
$results->contacts[0]->{'identity-profiles'}

How can I merge or search an object?

Here's my issue:
I have an object filled with arrays that look like this.
[376339] => Array
(
[0] => 1f422730-f54b-4e4d-9289-10258ce74446
[1] => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
I need to search another object to find objects with the matching IDs, like below. Is there a way of doing this without a foreach? There are SEVERAL and I would like to not have to loop over the entire object every time.
stdClass Object
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
stdClass Object
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
I need it to end up looking like this.
[376339] => Array
(
[0] => Array
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
[1] => Array
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
)
I'm not sure if this makes any sense, that's why I had my two inital outputs I need to have merged into one somehow. This is all coming from one huge json object and I'm just using json_decode($jsonStuff) to decode it.
Would this be easier if I added true in the decode function? If I could just search for it like I could in python, that would be neat. But as it is, I'm at a loss as to how to get the output I need.
Note: Input json CANNOT be changed, I have no affiliation with the people that created it.
First loop over your input array and create an array with the key as the id
$input = json_decode($json_input);
$output = array();
foreach($input as $obj){
$output[$obj->id] = $obj;
}
then you can build your other array by searching the id on the array key
$massive_search_array = array(376339 => array
(
0 => 1f422730-f54b-4e4d-9289-10258ce74446,
1 => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
);
$final_output = array();
foreach($massive_search_array as $index => $searches){
foreach($searches as $search){
if(isset($output[$search])){
$final_output[$index][] = $output[$search];
}
}
}

Categories