Wednesday, 15 September 2010

php - Warning: array_values() expects parameter 1 to be array, null given -



php - Warning: array_values() expects parameter 1 to be array, null given -

there 3 arrays in php, $console, $model, , $game.

here code way:

<?php $console = array(); $model = array(); $game = array(); $gamequery = "select * consolegame"; $gameresult = mysql_query($gamequery) or die(mysql_error()); while ($row = mysql_fetch_assoc($gameresult)) { if(!is_array($game[$row['modelid']])) { $game[$row['modelid']] = array(); } $game[$row['modelid']][$row['gameid']] = array( 'game name' => $row['gamename'], 'game id' => $row['gameid']); } $modelquery = "select * consolemodel"; $modelresult = mysql_query($modelquery) or die(mysql_error()); while ($row = mysql_fetch_assoc($modelresult)) { if (!is_array($model[$row['consoleid']])) { $model[$row['consoleid']] = array(); } $model[$row['consoleid']][$row['modelid']] = array( 'model name' => $row['modelname'], 'model id' => array_values($game[$row['modelid']])); //this warning way. } $consolequery = "select * consoleconsole"; $consoleresult = mysql_query($consolequery) or die(mysql_error()); while ($row = mysql_fetch_assoc($consoleresult)) { if (!is_array($console[$row['consoleid']])) { $console[$row['consoleid']] = array(); } $console[$row['consoleid']] = array( 'console name' => $row['consolename'], 'console id' => array_values($model[$row['consoleid']])); } $console = array_values($console); echo json_encode($console); ?>

as can see in code, have added array_values $console , $model without hitch. lucky then. when added array_values $game, creates warning. possible prepare this?

additional information declared $console, $model , $game array(). have no thought why it's not shown above $gamequery.

you're using 3 different queries results database, , although 'check' if key nowadays 'modelid' in $game array, possible no game exists specific model;

this line guarantees key added $game array if modelid not yet present

if(!is_array($game[$row['modelid']])) { $game[$row['modelid']] = array(); }

however, in sec loop, results select * consolemodel loading all models, perchance have no games in database, there won't key modelid in $game array.

for example:

consolegame

gamename | gameid | modelid gamea | 1 | 1 gameb | 2 | 1

consolemodel

modelname| modelid modela | 1 modelb | 2

this have 2 games modela, none modelb.

you may work around issue adding within $modelresult loop too

if (!is_array($model[$row['consoleid']])) { $game[$row['modelid']] = array(); } some ideas

the way you're collecting info doesn't utilize 'power' of database. databases designed retrieve info and related data (hence name 'relational database')

for example, collect consolemodels, including games model (if any) utilize this;

select consolemodel.modelid, consolemodel.modelname, consolegame.gameid, consolegame.gamename consolemodel -- using 'left' bring together consolemodels without games returned left bring together consolegame on consolegame.modelid = consolemodel.modelid

which return;

modelid | modelname | gameid | gamename 1 | modela | 1 | gamea 1 | modela | 1 | gamea 2 | modelb | null | null

although duplicate modelname rows, may create things easier you, won't have run 3 separate loops.

mode optimized options possible (e.g. loop through consolemodels in php , collect related games within loop), leave seek , learn

php mysql arrays

No comments:

Post a Comment