Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Aleksandr Antonov
15,876 PointsWarning: Illegal string offset php
Hello I'm working on a php web page with multi-dimentional array and I came across an issue, that I cant understand.
Here is the code,
foreach ($artists as $artists_id => $artist_each) {
if (isset($_GET["id"])) {
$artists_id = $_GET["id"];
if(isset($artists["$artists_id"])){
$artist_each = $artists[$artists_id];
}
}
if (!isset($artist_each)){
header("Location:".BASE_URL."artists/");
exit();
}
foreach ($artist_each as $artist => $artist_work) {
if (isset($_GET["id"])) {
$artist = $_GET["id"];
if(isset($artist_each["$artist"])){
$artist_work = $artists[$artists_id];
}
}
if (!isset($artist_each)){
header("Location:".BASE_URL."artists/");
exit();
}
echo "<pre>";
echo $artist_work["name"] . "<br/>";
echo $artist_work["title"];
echo "</pre>";
Here is the Error message, that i am getting. Note that after the error message it does display what I need.and also the extra A that is non existent in the array
Warning: Illegal string offset 'name' in C:\xampp\htdocs\artists\artists_each.php on line 34
A
Warning: Illegal string offset 'title' in C:\xampp\htdocs\artists\artists_each.php on line 35
A Alexander Anufriev "Angel with a Cup of Coffee" Alexander Anufriev "Angel with Flower"
And last but not least. Part of the array I am addressing. the whole array is done in such fashion
$artists = array(
$anufriev = array(
"name" => "Alexander Anufriev",
$anufriev[] = array(
"name" => "Alexander Anufriev",
"title" => '"'."Angel with a Cup of Coffee".'"',
"year" => 1998,
"dimentions" => "H:48 x W:40 in",
"media" => "Oil on canvas",
"availability" => $available,
"lg" => IMG_PATH . "alexander_anufriev" . LG_PATH ."Lg_Anufriev_Angel_with_a_Cup_of_Coffee_1992.jpg",
"thumb" => IMG_PATH . "alexander_anufriev" . THUMB_PATH . "Thumb_Anufriev_Angel_with_a_Cup_of_Coffee_1992.jpg"
),
$anufriev[] = array(
"name" => "Alexander Anufriev",
"title" => '"'."Angel with Flower".'"',
"year" => 2005,
"dimentions" => "H:12 x W:9",
"media" => "Oil on canvas",
"availability" => $available,
"lg" => IMG_PATH . "alexander_anufriev" . LG_PATH ."Lg_Anufriev_Green_Angel.jpg",
"thumb" => IMG_PATH . "alexander_anufriev" . THUMB_PATH . "Thumb_Anufriev_Green_Angel.jpg"
),
1 Answer
Rune Larsen
25,877 PointsThis may be an old question, and the author may be long past the issue, but since it have not had any replies, and I came across it now, two years after it was asked, maybe someone else will come after me with the same issue.
I came to the conclusion that there were the amount of digits in the string to cause the unexplained A. I had it with several different characters, and the thing they had in common was the first letter my case the "LastName", but in this case the "title" starting with "A", instead of the whole string. I am still trying to figure out how to get the whole string.