/    Sign up×
Community /Pin to ProfileBookmark

Please help me pull a interger from a string

Hello

Im posting data to a online CRM DB.

I need to be able to pull the unique ID thats assigned to each submit.

(if i were using mysql it would be mysql_insert_id())

The string im receiving back from the post looks like this:

{“oid”:2654,”data”:”Saved oid 2654″,”result”:true}

So my questions is how do i extract the oid number (in this example its 2654) after firstly making sure result: true?

Im no good at this kind preg matching stuff.

Cheers for your time.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 14.2011 — It's a JSON object, which you can parse with json_decode():
[code=php]
<?php
$text = '{"oid":2654,"data":"Saved oid 2654","result":true}';
$data = json_decode($text);
echo $data->oid; // 2654
[/code]
Copy linkTweet thisAlerts:
@BelrickauthorSep 14.2011 — Dammit i should of known it was a json object since the post is json encoded.

DUH

Thankyou no dog for showing me for the noob i am.
Copy linkTweet thisAlerts:
@bsmbahamasSep 16.2011 — cool gotta learn json!

it seems rather simple and precises, i understand that is object notation,

i almost gave him some complex explode() and if() statements, cause pregmatch is crazy hard to learn, and beleive me i've read a 1000 tuts on that, lol.

guess i gotta put pregmatch on my things to learn list too, i understand about 70&#37; of it though, just haven't done much practice with it.
Copy linkTweet thisAlerts:
@BelrickauthorSep 16.2011 — cool gotta learn json!

it seems rather simple and precises, i understand that is object notation,

i almost gave him some complex explode() and if() statements, cause pregmatch is crazy hard to learn, and beleive me i've read a 1000 tuts on that, lol.

guess i gotta put pregmatch on my things to learn list too, i understand about 70&#37; of it though, just haven't done much practice with it.[/QUOTE]




That was what i was planning on doing to!

I have to admit, im really struggling with JSON.

If i get a json return thats in the companies error format then i just cant read it.

Below is what im getting in a var_dumb(json_decode()) //object mode decode

[code=php]
object(stdClass)[3]
public 'error' =>
object(stdClass)[4]
public 'Failed to save' =>
array
0 => string 'A field is invalid.' (length=19)
1 => string 'Not a valid email address: f' (length=28)
public 'result' => boolean false



[/code]


Can someone please help me pull the two error strings off this?

Main reason im struggling is that i dont know how to access the 'Failed to save' array seeing as it has spaces
Copy linkTweet thisAlerts:
@NogDogSep 17.2011 — You may find it easier if you set the (optional) 2nd param of json_decode() to true, so that you get an associative array to work with. Otherwise, you should be able to retrieve it via something like:
[code=php]
$result = json_decode($json_string);
foreach($result->error->{'Failed to save'} as $error_message) {
echo $error_message . "<br />";
}
[/code]

I'm guessing at the curly-brace "complex" notation to deal with the spaces in the 'Failed to save' property name. :rolleyes:
Copy linkTweet thisAlerts:
@BelrickauthorSep 17.2011 — 100% correct

The solution was in the use of {}

Id never heard of them before now, cheers for your help!
×

Success!

Help @Belrick spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...