Error trying to load image from JSON file to php [duplicate]
This question already has an answer here:
How do I extract data from JSON with PHP?
3 answers
So, im trying to display the images that are in my JSON file on my website by calling the json file into my php but i get this notice "Array to string conversion". Any ideas on how i should solve this problem?
JSON
[
{
"name" : "match numbers 1",
"template" : "matching",
"data" : [
[
"one",
"Images/Number1.jpg"
],
[
"four",
"Images/Number4.jpg"
],
[
"nine",
"Images/Number9.jpg"
]
]
},
{
"name" : "match numbers 2",
"template" : "matching",
"data" : [
[
"six",
"Images/Number6.jpg"
],
[
"eight",
"Images/Number8.jpg"
],
[
"nine",
"Images/Number9.jpg"
]
]
}
]
php code
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
printf('<img src="'.$value["data"].'" />');
}
?>
php json
marked as duplicate by Paul
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 21:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How do I extract data from JSON with PHP?
3 answers
So, im trying to display the images that are in my JSON file on my website by calling the json file into my php but i get this notice "Array to string conversion". Any ideas on how i should solve this problem?
JSON
[
{
"name" : "match numbers 1",
"template" : "matching",
"data" : [
[
"one",
"Images/Number1.jpg"
],
[
"four",
"Images/Number4.jpg"
],
[
"nine",
"Images/Number9.jpg"
]
]
},
{
"name" : "match numbers 2",
"template" : "matching",
"data" : [
[
"six",
"Images/Number6.jpg"
],
[
"eight",
"Images/Number8.jpg"
],
[
"nine",
"Images/Number9.jpg"
]
]
}
]
php code
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
printf('<img src="'.$value["data"].'" />');
}
?>
php json
marked as duplicate by Paul
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 21:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
here data is array so try something : printf('<img src="'.$value["data"][0][1].'" />');
– suresh bambhaniya
Nov 14 '18 at 16:08
3
$value["data"]
is an array, it contains more than one image. You'll need a nested loop.
– Alex Howansky
Nov 14 '18 at 16:11
add a comment |
This question already has an answer here:
How do I extract data from JSON with PHP?
3 answers
So, im trying to display the images that are in my JSON file on my website by calling the json file into my php but i get this notice "Array to string conversion". Any ideas on how i should solve this problem?
JSON
[
{
"name" : "match numbers 1",
"template" : "matching",
"data" : [
[
"one",
"Images/Number1.jpg"
],
[
"four",
"Images/Number4.jpg"
],
[
"nine",
"Images/Number9.jpg"
]
]
},
{
"name" : "match numbers 2",
"template" : "matching",
"data" : [
[
"six",
"Images/Number6.jpg"
],
[
"eight",
"Images/Number8.jpg"
],
[
"nine",
"Images/Number9.jpg"
]
]
}
]
php code
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
printf('<img src="'.$value["data"].'" />');
}
?>
php json
This question already has an answer here:
How do I extract data from JSON with PHP?
3 answers
So, im trying to display the images that are in my JSON file on my website by calling the json file into my php but i get this notice "Array to string conversion". Any ideas on how i should solve this problem?
JSON
[
{
"name" : "match numbers 1",
"template" : "matching",
"data" : [
[
"one",
"Images/Number1.jpg"
],
[
"four",
"Images/Number4.jpg"
],
[
"nine",
"Images/Number9.jpg"
]
]
},
{
"name" : "match numbers 2",
"template" : "matching",
"data" : [
[
"six",
"Images/Number6.jpg"
],
[
"eight",
"Images/Number8.jpg"
],
[
"nine",
"Images/Number9.jpg"
]
]
}
]
php code
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
printf('<img src="'.$value["data"].'" />');
}
?>
This question already has an answer here:
How do I extract data from JSON with PHP?
3 answers
php json
php json
asked Nov 14 '18 at 16:06
Noob ProgrammerNoob Programmer
206
206
marked as duplicate by Paul
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 21:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Paul
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 21:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
here data is array so try something : printf('<img src="'.$value["data"][0][1].'" />');
– suresh bambhaniya
Nov 14 '18 at 16:08
3
$value["data"]
is an array, it contains more than one image. You'll need a nested loop.
– Alex Howansky
Nov 14 '18 at 16:11
add a comment |
here data is array so try something : printf('<img src="'.$value["data"][0][1].'" />');
– suresh bambhaniya
Nov 14 '18 at 16:08
3
$value["data"]
is an array, it contains more than one image. You'll need a nested loop.
– Alex Howansky
Nov 14 '18 at 16:11
here data is array so try something : printf('<img src="'.$value["data"][0][1].'" />');
– suresh bambhaniya
Nov 14 '18 at 16:08
here data is array so try something : printf('<img src="'.$value["data"][0][1].'" />');
– suresh bambhaniya
Nov 14 '18 at 16:08
3
3
$value["data"]
is an array, it contains more than one image. You'll need a nested loop.– Alex Howansky
Nov 14 '18 at 16:11
$value["data"]
is an array, it contains more than one image. You'll need a nested loop.– Alex Howansky
Nov 14 '18 at 16:11
add a comment |
2 Answers
2
active
oldest
votes
because $value["data"] is array so you can iterate and get all images
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
foreach($value["data"] as $val){
printf('<img src="'.$val[1].'" />'); //$val is also array, i assumed second is image url
}
}
?>
1
Thank you for the help! it worked
– Noob Programmer
Nov 14 '18 at 16:28
If solution is works then please mark as correct/upvote then future user easily find out correct answer
– suresh bambhaniya
Nov 15 '18 at 4:27
add a comment |
The "Array to string conversion" error is happening because your $value["data"] is an array. You will also want to iterate through that array as well. Something like this.
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
foreach($value["data"] as $data){
printf('<img src="'.$data[1].'" />'); //<- I assume this index
}
}
?>
Whenever I see the pesky Array to string conversion. var_dump
is my best friend to debug the formatting.
Thanks a lot! I appreciate it
– Noob Programmer
Nov 14 '18 at 16:28
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
because $value["data"] is array so you can iterate and get all images
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
foreach($value["data"] as $val){
printf('<img src="'.$val[1].'" />'); //$val is also array, i assumed second is image url
}
}
?>
1
Thank you for the help! it worked
– Noob Programmer
Nov 14 '18 at 16:28
If solution is works then please mark as correct/upvote then future user easily find out correct answer
– suresh bambhaniya
Nov 15 '18 at 4:27
add a comment |
because $value["data"] is array so you can iterate and get all images
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
foreach($value["data"] as $val){
printf('<img src="'.$val[1].'" />'); //$val is also array, i assumed second is image url
}
}
?>
1
Thank you for the help! it worked
– Noob Programmer
Nov 14 '18 at 16:28
If solution is works then please mark as correct/upvote then future user easily find out correct answer
– suresh bambhaniya
Nov 15 '18 at 4:27
add a comment |
because $value["data"] is array so you can iterate and get all images
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
foreach($value["data"] as $val){
printf('<img src="'.$val[1].'" />'); //$val is also array, i assumed second is image url
}
}
?>
because $value["data"] is array so you can iterate and get all images
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
foreach($value["data"] as $val){
printf('<img src="'.$val[1].'" />'); //$val is also array, i assumed second is image url
}
}
?>
edited Nov 14 '18 at 16:19
answered Nov 14 '18 at 16:12
suresh bambhaniyasuresh bambhaniya
928214
928214
1
Thank you for the help! it worked
– Noob Programmer
Nov 14 '18 at 16:28
If solution is works then please mark as correct/upvote then future user easily find out correct answer
– suresh bambhaniya
Nov 15 '18 at 4:27
add a comment |
1
Thank you for the help! it worked
– Noob Programmer
Nov 14 '18 at 16:28
If solution is works then please mark as correct/upvote then future user easily find out correct answer
– suresh bambhaniya
Nov 15 '18 at 4:27
1
1
Thank you for the help! it worked
– Noob Programmer
Nov 14 '18 at 16:28
Thank you for the help! it worked
– Noob Programmer
Nov 14 '18 at 16:28
If solution is works then please mark as correct/upvote then future user easily find out correct answer
– suresh bambhaniya
Nov 15 '18 at 4:27
If solution is works then please mark as correct/upvote then future user easily find out correct answer
– suresh bambhaniya
Nov 15 '18 at 4:27
add a comment |
The "Array to string conversion" error is happening because your $value["data"] is an array. You will also want to iterate through that array as well. Something like this.
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
foreach($value["data"] as $data){
printf('<img src="'.$data[1].'" />'); //<- I assume this index
}
}
?>
Whenever I see the pesky Array to string conversion. var_dump
is my best friend to debug the formatting.
Thanks a lot! I appreciate it
– Noob Programmer
Nov 14 '18 at 16:28
add a comment |
The "Array to string conversion" error is happening because your $value["data"] is an array. You will also want to iterate through that array as well. Something like this.
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
foreach($value["data"] as $data){
printf('<img src="'.$data[1].'" />'); //<- I assume this index
}
}
?>
Whenever I see the pesky Array to string conversion. var_dump
is my best friend to debug the formatting.
Thanks a lot! I appreciate it
– Noob Programmer
Nov 14 '18 at 16:28
add a comment |
The "Array to string conversion" error is happening because your $value["data"] is an array. You will also want to iterate through that array as well. Something like this.
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
foreach($value["data"] as $data){
printf('<img src="'.$data[1].'" />'); //<- I assume this index
}
}
?>
Whenever I see the pesky Array to string conversion. var_dump
is my best friend to debug the formatting.
The "Array to string conversion" error is happening because your $value["data"] is an array. You will also want to iterate through that array as well. Something like this.
<?php
$json_var = file_get_contents("template.json");
$json_var = json_decode($json_var, true);
foreach($json_var as $value)
{
printf($value["name"]);
printf($value["template"]);
foreach($value["data"] as $data){
printf('<img src="'.$data[1].'" />'); //<- I assume this index
}
}
?>
Whenever I see the pesky Array to string conversion. var_dump
is my best friend to debug the formatting.
answered Nov 14 '18 at 16:17
Christopher JohnstonChristopher Johnston
967
967
Thanks a lot! I appreciate it
– Noob Programmer
Nov 14 '18 at 16:28
add a comment |
Thanks a lot! I appreciate it
– Noob Programmer
Nov 14 '18 at 16:28
Thanks a lot! I appreciate it
– Noob Programmer
Nov 14 '18 at 16:28
Thanks a lot! I appreciate it
– Noob Programmer
Nov 14 '18 at 16:28
add a comment |
here data is array so try something : printf('<img src="'.$value["data"][0][1].'" />');
– suresh bambhaniya
Nov 14 '18 at 16:08
3
$value["data"]
is an array, it contains more than one image. You'll need a nested loop.– Alex Howansky
Nov 14 '18 at 16:11