Error trying to load image from JSON file to php [duplicate]












0
















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"].'" />');
}
?>









share|improve this question













marked as duplicate by Paul php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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
















0
















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"].'" />');
}
?>









share|improve this question













marked as duplicate by Paul php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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














0












0








0









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"].'" />');
}
?>









share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 16:06









Noob ProgrammerNoob Programmer

206




206




marked as duplicate by Paul php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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 php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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



















  • 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












2 Answers
2






active

oldest

votes


















2














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
}
}
?>





share|improve this answer





















  • 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



















0














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.






share|improve this answer
























  • Thanks a lot! I appreciate it

    – Noob Programmer
    Nov 14 '18 at 16:28


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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
}
}
?>





share|improve this answer





















  • 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
















2














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
}
}
?>





share|improve this answer





















  • 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














2












2








2







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
}
}
?>





share|improve this answer















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
}
}
?>






share|improve this answer














share|improve this answer



share|improve this answer








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














  • 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













0














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.






share|improve this answer
























  • Thanks a lot! I appreciate it

    – Noob Programmer
    Nov 14 '18 at 16:28
















0














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.






share|improve this answer
























  • Thanks a lot! I appreciate it

    – Noob Programmer
    Nov 14 '18 at 16:28














0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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



Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python