Jump to content

Hello.

 

So I got this json string that can be what ever big.

 

{"profiles":[{"profileId":"9c1b46db-652f-4f4c-8879-0032dae374fe","userId":"9c1b46db-652f-4f4c-8879-0032dae374fe","platformType":"uplay","idOnPlatform":"9C1B46DB-652F-4F4C-8879-0032DAE374FE","nameOnPlatform":"Anfi."},{"profileId":"9c6c974b-3bf9-4424-b718-33787d5ea887","userId":"9c6c974b-3bf9-4424-b718-33787d5ea887","platformType":"uplay","idOnPlatform":"9C6C974B-3BF9-4424-B718-33787D5EA887","nameOnPlatform":"joveice"}]}

 

Formated for easyer reading

 

Object
	profiles:Array[2]
		0:Object
			idOnPlatform:"9C1B46DB-652F-4F4C-8879-0032DAE374FE"
			nameOnPlatform:"Anfi."
			platformType:"uplay"
			profileId:"9c1b46db-652f-4f4c-8879-0032dae374fe"
			userId:"9c1b46db-652f-4f4c-8879-0032dae374fe"
		1:Object
			idOnPlatform:"9C6C974B-3BF9-4424-B718-33787D5EA887"
			nameOnPlatform:"joveice"
			platformType:"uplay"
			profileId:"9c6c974b-3bf9-4424-b718-33787d5ea887"
			userId:"9c6c974b-3bf9-4424-b718-33787d5ea887"

 

How do I get this into a database? so I know how the sql stuff works, but I need this thing "$obj->stuff->stuff->stuff" As I don't know how it will look for this and how to do a foreach or for with calculation

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/787486-php-json_decode-to-database/
Share on other sites

Link to post
Share on other sites

8 minutes ago, Joveice said:

Hello.

 

So I got this json string that can be what ever big.

 


{"profiles":[{"profileId":"9c1b46db-652f-4f4c-8879-0032dae374fe","userId":"9c1b46db-652f-4f4c-8879-0032dae374fe","platformType":"uplay","idOnPlatform":"9C1B46DB-652F-4F4C-8879-0032DAE374FE","nameOnPlatform":"Anfi."},{"profileId":"9c6c974b-3bf9-4424-b718-33787d5ea887","userId":"9c6c974b-3bf9-4424-b718-33787d5ea887","platformType":"uplay","idOnPlatform":"9C6C974B-3BF9-4424-B718-33787D5EA887","nameOnPlatform":"joveice"}]}

 

Formated for easyer reading

 


Object
	profiles:Array[2]
		0:Object
			idOnPlatform:"9C1B46DB-652F-4F4C-8879-0032DAE374FE"
			nameOnPlatform:"Anfi."
			platformType:"uplay"
			profileId:"9c1b46db-652f-4f4c-8879-0032dae374fe"
			userId:"9c1b46db-652f-4f4c-8879-0032dae374fe"
		1:Object
			idOnPlatform:"9C6C974B-3BF9-4424-B718-33787D5EA887"
			nameOnPlatform:"joveice"
			platformType:"uplay"
			profileId:"9c6c974b-3bf9-4424-b718-33787d5ea887"
			userId:"9c6c974b-3bf9-4424-b718-33787d5ea887"

 

How do I get this into a database? so I know how the sql stuff works, but I need this thing "$obj->stuff->stuff->stuff" As I don't know how it will look for this and how to do a foreach or for with calculation

foreach ($array->{'profiles'} as $value) {
    echo $value->{'idOnPlatform'};
}

 

This works, if there is a better way please let me know, or I'll gonna use this.

Back-end developer, electronics "hacker"

Link to post
Share on other sites

32 minutes ago, Joveice said:

foreach ($array->{'profiles'} as $value) {
    echo $value->{'idOnPlatform'};
}

 

This works, if there is a better way please let me know, or I'll gonna use this.

 

I don't really get your question at all... you want to have it as an array??

 

I would use json as an associative array personally

$json = '{"key":"value"}';

$obj = json_decode($json);
/* Object */

$array = json_decode($json,true);
/* --> ['key'=>'value'] */

 

Also I remember you are using Laravel right? Depending on the data, I would consider working with collections.

Depending on the data you have.

 

Edited by leodaniel
Added note for Laravel Collections

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to post
Share on other sites

Just now, leodaniel said:

 

I don't really get your question at all... you want to have it as an array??

 

I would use json as an associative array personally


$json = '{"key":"value"}';

$obj = json_decode($json);
/* Object */

$array = json_decode($json,true);
/* --> ['key'=>'value'] */

 

This is what I wanted

 

foreach ($array->{'profiles'} as $value) {
    echo $value->{'idOnPlatform'};
}

With this I can insert stuff to the database in a foreach.

Back-end developer, electronics "hacker"

Link to post
Share on other sites

1 minute ago, Joveice said:

This is what I wanted

 


foreach ($array->{'profiles'} as $value) {
    echo $value->{'idOnPlatform'};
}

With this I can insert stuff to the database in a foreach.

Okey I would still use it as Array, for me it looks cleaner

foreach(json_decode($json,true) as $profile_data){
    /* Insert your stuff */
    $profile->save();
}

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to post
Share on other sites

15 hours ago, leodaniel said:

Okey I would still use it as Array, for me it looks cleaner


foreach(json_decode($json,true) as $profile_data){
    /* Insert your stuff */
    $profile->save();
}

 

So this would be $profile->idOnPlatform instead of $value->{'idOnPlatform'} ?

Back-end developer, electronics "hacker"

Link to post
Share on other sites

4 minutes ago, Joveice said:

So this would be $profile->idOnPlatform instead of $value->{'idOnPlatform'} ?

No it's an array

foreach(json_decode(... ,true) as $profile){
    $profile['idOnPlatform'] ...
}

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×