TechnoDiver Posted September 11, 2021 Share Posted September 11, 2021 Hi, Phreaks, I've made a number in inquiries into roughly the same problem. For that I apologize but I'm really confused and frustrated and can't work this seemingly simple problem out. Like before, I'm trying to make an associative array out of DB data and I can't work out some of the results I'm getting. If I use this code here -> <?php public function all($column = "") { $items = array(); foreach($this->_results as $item) { echo $item->id . " => " . $item->category . " "; } } I get this -> Quote 1 => public health 2 => environment 3 => global unrest 4 => military 6 => super powers 7 => technology 8 => human rights 60 => space race 67 => globalism 87 => government Excellent, so far But if I do this -> <?php public function all($column = "") { $items = array(); foreach($this->_results as $item) { $items [] = [$item->id => $item->category]; print_r($items); } } I get back a whole mound of data -> Quote Array ( [0] => Array ( [1] => public health ) ) Array ( [0] => Array ( [1] => public health ) [1] => Array ( [2] => environment ) ) Array ( [0] => Array ( [1] => public health ) [1] => Array ( [2] => environment ) [2] => Array ( [3] => global unrest ) ) Array ( [0] => Array ( [1] => public health ) [1] => Array ( [2] => environment ) [2] => Array ( [3] => global unrest ) [3] => Array ( [4] => military ) ) Array ( [0] => Array ( [1] => public health ) [1] => Array ( [2] => environment ) [2] => Array ( [3] => global unrest ) [3] => Array ( [4] => military ) [4] => Array ( [6] => super powers ) ) Array ( [0] => Array ( [1] => public health ) [1] => Array ( [2] => environment ) [2] => Array ( [3] => global unrest ) [3] => Array ( [4] => military ) [4] => Array ( [6] => super powers ) [5] => Array ( [7] => technology ) ) Array ( [0] => Array ( [1] => public health ) [1] => Array ( [2] => environment ) [2] => Array ( [3] => global unrest ) [3] => Array ( [4] => military ) [4] => Array ( [6] => super powers ) [5] => Array ( [7] => technology ) [6] => Array ( [8] => human rights ) ) Array ( [0] => Array ( [1] => public health ) [1] => Array ( [2] => environment ) [2] => Array ( [3] => global unrest ) [3] => Array ( [4] => military ) [4] => Array ( [6] => super powers ) [5] => Array ( [7] => technology ) [6] => Array ( [8] => human rights ) [7] => Array ( [60] => space race ) ) Array ( [0] => Array ( [1] => public health ) [1] => Array ( [2] => environment ) [2] => Array ( [3] => global unrest ) [3] => Array ( [4] => military ) [4] => Array ( [6] => super powers ) [5] => Array ( [7] => technology ) [6] => Array ( [8] => human rights ) [7] => Array ( [60] => space race ) [8] => Array ( [67] => globalism ) ) Array ( [0] => Array ( [1] => public health ) [1] => Array ( [2] => environment ) [2] => Array ( [3] => global unrest ) [3] => Array ( [4] => military ) [4] => Array ( [6] => super powers ) [5] => Array ( [7] => technology ) [6] => Array ( [8] => human rights ) [7] => Array ( [60] => space race ) [8] => Array ( [67] => globalism ) [9] => Array ( [87] => government ) ) Why? I don't get it. Why is building this assoc array with category_id as the key and category as the value so difficult. I don't understand why this second code isn't making that arrray Quote Link to comment https://forums.phpfreaks.com/topic/313724-associative-array-frustrations/ Share on other sites More sharing options...
Barand Posted September 11, 2021 Share Posted September 11, 2021 (edited) If you are using print_r(), put it between <pre>..</pre> tags to make it legible. echo '<pre>' . print_r($array, true) . '</pre>'; Try $items[$item->id] = $item->category; Try to limit the number of posts on the same question to 1. Edited September 11, 2021 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/313724-associative-array-frustrations/#findComment-1589838 Share on other sites More sharing options...
TechnoDiver Posted September 11, 2021 Author Share Posted September 11, 2021 1 minute ago, Barand said: $items[$item->id] = $item->category; I was just working on that and came here to give it as a solution. I'm really a dumb ass sometimes though because I had been messing around with building that method so long I had a bunch of commented out lines and didn't notice my print_r was also inside the foreach loop. Whereas the echo was meant to be inside it. I have a lot of work to do with getting good at formatting data with loops and other build-ins. It's very time consuming for me at the moment Quote Link to comment https://forums.phpfreaks.com/topic/313724-associative-array-frustrations/#findComment-1589839 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.