DeanWhitehouse Posted July 23, 2009 Share Posted July 23, 2009 I have data stored in my database as such (this is only one table, one field in the whole db) array("left" => array("article_1" => array("id" => 1,"pos" => 2),"article_2" => array("id" => 2,"pos" => 1),"article_3" => array("id" => 3,"pos" => 3))) when i try and extract it doing <?php $newspaper = mysql_fetch_assoc($newspaper); ?> <h1>American Gangsters Newspaper - <?=Secure($newspaper['title'])?></h1> <table align="center" width="100%"> <tr> <td colspan="2" align="center" style="border-bottom:thin solid grey;"> <h2>Issue Number: <?=number_format($newspaper['id'])?>, Published <?=ConvertTimeToWords($newspaper['time'])?></h2> </td> </tr> <tr> <td colspan="2" align="left" style="border-bottom:thin solid grey;"> Editor <?=CreateUserLink($newspaper['editor_id']);?><br> Description:<br> <?=Secure($newspaper['desc'])?> </td> </tr> <?php $left = $newspaper['articles']['left']; $right = $newspaper['articles']['right']; ?> <tr> <td align="left" width="50%" style="border-right:thin solid grey;"> <?php print_r($newspaper['articles']); echo $left;?> <h3></h3> Article OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle One <hr> <h3>Article Title</h3> Article OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle One </td> <td align="left" width="50%"> Article Two </td> </tr> <tr> <td colspan="2" align="left" style="border-top:thin solid grey;"> <h2>Adverts</h2> <?=Secure($newspaper['advert'])?> </td> </tr> </table> I get an unexpected result, the print_r($newspaper['articles']); prints the array as expected but echo $left; prints the letter a :s Same if i try print_r($left); Any ideas? Thanks, Blade Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/ Share on other sites More sharing options...
9three Posted July 23, 2009 Share Posted July 23, 2009 Shouldn't it be <?php $left = $newspaper['left']['articles']; $right = $newspaper['right']['articles']; ?> ? Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881499 Share on other sites More sharing options...
DeanWhitehouse Posted July 23, 2009 Author Share Posted July 23, 2009 No the first index is articles then i am trying to access the next index inside of that . Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881500 Share on other sites More sharing options...
DeanWhitehouse Posted July 24, 2009 Author Share Posted July 24, 2009 No one? Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881870 Share on other sites More sharing options...
dgoosens Posted July 24, 2009 Share Posted July 24, 2009 if I get this right $left is an array and you can not echo arrays try var_dump($left) and check out the result. Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881874 Share on other sites More sharing options...
DeanWhitehouse Posted July 24, 2009 Author Share Posted July 24, 2009 Please read my orginal post, i tried print_r and it printed a string "a" not an array. Also echoing arrays will print "Array" not "a" Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881888 Share on other sites More sharing options...
haku Posted July 24, 2009 Share Posted July 24, 2009 Show us what you get with: print_r($newspaper['articles']); Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881895 Share on other sites More sharing options...
DeanWhitehouse Posted July 24, 2009 Author Share Posted July 24, 2009 array("left" => array("article_1" => array("id" => 1,"pos" => 2),"article_2" => array("id" => 2,"pos" => 1),"article_3" => array("id" => 3,"pos" => 3))) Thats what print_r($newspaper['articles']); prints The data stored in the db is array("articles" => array("left" => array("article_1" => array("id" => 1,"pos" => 2),"article_2" => array("id" => 2,"pos" => 1),"article_3" => array("id" => 3,"pos" => 3))),array("right" => array(""))) Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881907 Share on other sites More sharing options...
haku Posted July 24, 2009 Share Posted July 24, 2009 array("left" => array("article_1" => array("id" => 1,"pos" => 2),"article_2" => array("id" => 2,"pos" => 1),"article_3" => array("id" => 3,"pos" => 3))) Thats what print_r($newspaper['articles']); prints No, I don't think it is. That's not the format that print_r() uses when displaying it's output. Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881918 Share on other sites More sharing options...
DeanWhitehouse Posted July 24, 2009 Author Share Posted July 24, 2009 it is, it is printing as a string not an array Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881923 Share on other sites More sharing options...
haku Posted July 24, 2009 Share Posted July 24, 2009 Ahhh, I didn't read the first post properly. Try this: $left = eval($newspaper); $left = $left['articles']['left']; Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881926 Share on other sites More sharing options...
DeanWhitehouse Posted July 24, 2009 Author Share Posted July 24, 2009 I have tried using eval already it created a wierd error Using your way it creates these errors 08:14 am: Notice: Array to string conversion in /home/vheissu/public_html/paper.php on line 42 08:14 am: Parse error: syntax error, unexpected $end, expecting '(' in /home/vheissu/public_html/paper.php(42) : eval()'d code on line 1 trying $left = eval($newspaper['articles']); $left = $left['left']; I get 08:14 am: Parse error: syntax error, unexpected $end, expecting '(' in /home/vheissu/public_html/paper.php(42) : eval()'d code on line 1 Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881930 Share on other sites More sharing options...
sasa Posted July 24, 2009 Share Posted July 24, 2009 try <?php eval('$x = '. $newspaper['articles'].';'); print_r($x['articles']['left']); ?> Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881953 Share on other sites More sharing options...
DeanWhitehouse Posted July 24, 2009 Author Share Posted July 24, 2009 Thanks that did it, i think there should be some more documentation/better documentation on the eval function. Link to comment https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881958 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.