timmah1 Posted March 20, 2012 Share Posted March 20, 2012 I have my array like so $inaque = array("'$dir'"=>"'$company'"); When I run print_r($inaque); It shows like Array ( ['share_what_you_want'] => 'Share What You Want' ) How can I get this array to print out like array ( 'share_what_you_want' => 'Share What You Want' ) Without the [ ] This is the format that is needed for this code that has already been written by somebody else. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/259339-array-format/ Share on other sites More sharing options...
Muddy_Funster Posted March 20, 2012 Share Posted March 20, 2012 that's just what print_r does, I can't fathom why anyone would write code to check what print_r outputs rather than just checking against the original array, that's just crazy. You're either going to have to pass the print_r result to a sting and then do a regular expression replace on it, or can the code that was written by a crazy person and find something sane to do the job. Link to comment https://forums.phpfreaks.com/topic/259339-array-format/#findComment-1329467 Share on other sites More sharing options...
seanlim Posted March 20, 2012 Share Posted March 20, 2012 var_export seems to fit your requirements quite well Link to comment https://forums.phpfreaks.com/topic/259339-array-format/#findComment-1329468 Share on other sites More sharing options...
timmah1 Posted March 20, 2012 Author Share Posted March 20, 2012 Thank you seanlim, it's what we needed. Now, I am still learning about arrays, so I know some of my questions may be minor. This is only pulling one from the database, when in fact, there is 2 right now, but the array is only showing one. How can I put all of the values into the array? include($_SERVER['DOCUMENT_ROOT']."/inc/config.db.php"); $path = $_SERVER['DOCUMENT_ROOT']."/panel/testing/connectors/sample/projects/"; foreach(glob($path . '*', GLOB_ONLYDIR) as $dir) { $dir = basename($dir); //echo $dir."<br />"; $companyName = "SELECT * FROM app_queue WHERE company LIKE '%$dir%' AND status = '1'"; $compResult = mysql_query($companyName); while($getComp = mysql_fetch_array($compResult)){ $status = $getComp['status']; $company = $getComp['company']; $inaque = array($dir=>$company); } }; return $inaque; Link to comment https://forums.phpfreaks.com/topic/259339-array-format/#findComment-1329509 Share on other sites More sharing options...
PFMaBiSmAd Posted March 20, 2012 Share Posted March 20, 2012 This is the format that is needed for this code that has already been written by somebody else. I can just about bet that the code is expecting an actual php array variable with specific key/value pairs. Not the print-out/print_r/var_dump/var_export (which was already suggested in your previous thread on this) of an array and not a string that looks like an array. What is this code you are trying to supply data to? Link to comment https://forums.phpfreaks.com/topic/259339-array-format/#findComment-1329514 Share on other sites More sharing options...
timmah1 Posted March 20, 2012 Author Share Posted March 20, 2012 This is the format that is needed for this code that has already been written by somebody else. What is this code you are trying to supply data to? It's for building apps dynamically. Right now, we build them manually, what this code does is Scans a directory, checks the db for a company name that matches the $dir variable, then puts items into the array. That part works fine now, the problem I'm having now is that the array is only populated with 1 item, and the array should show all items. Right now, there are 2 in thh db with a status of 1, but the array is only pulling 1 of them Link to comment https://forums.phpfreaks.com/topic/259339-array-format/#findComment-1329517 Share on other sites More sharing options...
PFMaBiSmAd Posted March 20, 2012 Share Posted March 20, 2012 I bet you are getting the last data? $inaque = array($dir=>$company); ^^^ That line of code overwrites the $inaque variable on each iteration of the loop. You only end up with the last set of data after the end of the loop. If your goal (which you haven't actually shown) is to build an array of arrays, you would use - $inaque[] = array($dir=>$company); Link to comment https://forums.phpfreaks.com/topic/259339-array-format/#findComment-1329520 Share on other sites More sharing options...
timmah1 Posted March 20, 2012 Author Share Posted March 20, 2012 ok, I'm obviously lost. I need t query the db with each $dir variable, and then return the result. I can query the db and pull exactly what I need with one $dir, but I have no idea how to query the db foreach $dir variable. This is the code, can somebody please help me out here? <?php include($_SERVER['DOCUMENT_ROOT']."/inc/config.db.php"); $path = $_SERVER['DOCUMENT_ROOT']."/panel/testing/connectors/sample/projects/"; foreach(glob($path . '*', GLOB_ONLYDIR) as $dir) { $dir = basename($dir); //echo $dir."<br />"; $companyName = "SELECT * FROM app_queue WHERE company LIKE '%$dir%' AND status = '1'"; $compResult = mysql_query($companyName); while($getComp = mysql_fetch_array($compResult)){ $status = $getComp['status']; $company = $getComp['company']; $inaque = array($dir=>$company); } }; return $inaque; ?> Link to comment https://forums.phpfreaks.com/topic/259339-array-format/#findComment-1329561 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.