jedeye Posted June 6, 2011 Share Posted June 6, 2011 i have tried many different built in functions from the php manual to no avail... i have a class with a function to return the values from a query in an array...when it returns it returns as a multidimensional array function code: function getModelSpecs() { $array_of_specs = array(); $i = 0; $result = pg_query($this->dbConnection,self::stringGetModelSpecsSQL); while( $rows = pg_fetch_row($result) ){ $specs = new ModelSpecsDTO(); $specs->spec_id = $rows[0]; $specs->spec_name = $rows[1]; $specs->spec_description = $rows[2]; $specs->css_category_name = $rows[3]; $specs->spec_index = $rows[4]; $array_of_specs[$i] = $specs; $i++; } return $array_of_specs; } my return values from "var_dump" array(4) { [0]=> object(ModelSpecsDTO)#2 (5) { ["spec_id"]=> string(1) "1" ["spec_name"]=> string(5) "Width" ["spec_description"]=> string(13) "Product Width" ["css_category_name"]=> NULL ["spec_index"]=> string(1) "0" } [1]=> object(ModelSpecsDTO)#3 (5) { ["spec_id"]=> string(1) "2" ["spec_name"]=> string(6) "Height" ["spec_description"]=> string(14) "Product Height" ["css_category_name"]=> NULL ["spec_index"]=> string(1) "1" } [2]=> object(ModelSpecsDTO)#4 (5) { ["spec_id"]=> string(1) "3" ["spec_name"]=> string(5) "Depth" ["spec_description"]=> string(13) "Product Depth" ["css_category_name"]=> NULL ["spec_index"]=> string(1) "2" } [3]=> object(ModelSpecsDTO)#5 (5) { ["spec_id"]=> string(2) "-1" ["spec_name"]=> string(4) "Misc" ["spec_description"]=> string(13) "LSI Misc Data" ["css_category_name"]=> NULL ["spec_index"]=> string(1) "3" } } The problem is... All i want to is the spec_name value from each array, but having a hell of a time trying to figure out the algorithm to step through the array and pull only that value for each array. Any help is greatly appreciated... Quote Link to comment https://forums.phpfreaks.com/topic/238594-stepping-through-multidimension-array/ Share on other sites More sharing options...
Psycho Posted June 6, 2011 Share Posted June 6, 2011 It makes it hard to analyze the array when you put it all in one line like that. Could you do a print_r() and post the results. Also, either get the output directly in the HTML source (which will have the line breaks) or put the output inside PRE tags so it will display with line-breaks in the output. Quote Link to comment https://forums.phpfreaks.com/topic/238594-stepping-through-multidimension-array/#findComment-1226119 Share on other sites More sharing options...
wildteen88 Posted June 6, 2011 Share Posted June 6, 2011 You'd loop through the specs array. Example foreach($array_of_specs as $specs) { echo $specs->spec_name . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/238594-stepping-through-multidimension-array/#findComment-1226120 Share on other sites More sharing options...
jedeye Posted June 6, 2011 Author Share Posted June 6, 2011 @mj> that was a print_r @wild> OMG.. i feel real dumb, thank you. i was totally trying to over complicate it by trying to slice the array n such I also tried that exact step... however, i put it outside a loop for testing, just to see if it would spit out what i needed... $connectDAO = daoFactory::getModelSpecs(); $model_specs = $connectDAO->getModelSpecs(); $model_specs->spec_name; thanks again.. Quote Link to comment https://forums.phpfreaks.com/topic/238594-stepping-through-multidimension-array/#findComment-1226125 Share on other sites More sharing options...
Psycho Posted June 6, 2011 Share Posted June 6, 2011 @mj> that was a print_r my return values from "var_dump" Quote Link to comment https://forums.phpfreaks.com/topic/238594-stepping-through-multidimension-array/#findComment-1226126 Share on other sites More sharing options...
jedeye Posted June 6, 2011 Author Share Posted June 6, 2011 @mj> that was a print_r my return values from "var_dump" sorry, you're right that was var_dump() sorry.. prior to everything i was using print_r() Quote Link to comment https://forums.phpfreaks.com/topic/238594-stepping-through-multidimension-array/#findComment-1226129 Share on other sites More sharing options...
jcbones Posted June 6, 2011 Share Posted June 6, 2011 When you use pring_r() or var_dump(), inclose them inside <pre> tags, to get the desired output. echo '<pre>' . print_r($var,true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/238594-stepping-through-multidimension-array/#findComment-1226141 Share on other sites More sharing options...
jedeye Posted June 6, 2011 Author Share Posted June 6, 2011 When you use pring_r() or var_dump(), inclose them inside <pre> tags, to get the desired output. echo '<pre>' . print_r($var,true) . '</pre>'; Good tip, thanks Quote Link to comment https://forums.phpfreaks.com/topic/238594-stepping-through-multidimension-array/#findComment-1226152 Share on other sites More sharing options...
Psycho Posted June 6, 2011 Share Posted June 6, 2011 When you use pring_r() or var_dump(), inclose them inside <pre> tags, to get the desired output. Or just view the output in the HTML source code (i.e. right-click View Page Source). Quote Link to comment https://forums.phpfreaks.com/topic/238594-stepping-through-multidimension-array/#findComment-1226175 Share on other sites More sharing options...
jcbones Posted June 7, 2011 Share Posted June 7, 2011 When you use pring_r() or var_dump(), inclose them inside <pre> tags, to get the desired output. Or just view the output in the HTML source code (i.e. right-click View Page Source). That seems to soar over the heads of newbies!!! Quote Link to comment https://forums.phpfreaks.com/topic/238594-stepping-through-multidimension-array/#findComment-1226181 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.