Jump to content

Associative array frustrations


TechnoDiver
 Share

Recommended Posts

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

Link to comment
Share on other sites

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 by Barand
  • Great Answer 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.