• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UGH! Arrays are giving me a headache :(

#1
[eluser]Kinsbane[/eluser]
So, I have a resultset that outputs this:
Code:
Array
(
    [0] => Array
        (
            [id] => 1
            [division_code] => CORP
            [name] => Corporate Office
            [description] => Employees stationed at the office.
        )

    [1] => Array
        (
            [id] => 2
            [division_code] => CPD
            [name] => Products
            [description] => Employees stationed at the 20520 office.
        )

    [2] => Array
        (
            [id] => 3
            [division_code] => BPD
            [name] => New Products
            [description] => Employees stationed at the 295 Foster office.
        )

    [3] => Array
        (
            [id] => 4
            [division_code] => YPD
            [name] => Old Products
            [description] => Employees stationed at the Yokneam office.
        )

    [4] => Array
        (
            [id] => 5
            [division_code] => USA
            [name] => MRV Americas
            [description] => Sales personnel stationed in the Americas.
        )

    [5] => Array
        (
            [id] => 6
            [division_code] => INT
            [name] => MRV International
            [description] => Sales personnel stationed internationally, outside of the Americas.
        )

)

I have my code grab this dataset with an object, and then I try to loop through the object with this:
Code:
foreach($divisions->result_array() as $row)
{
    die(print_r($row)); //just to see what I get
}

However, this only gives me the first record in the dataset ([id] of 1).
I am trying to loop through this dataset and then give it to the $data[] array and have the values stored show up in the view file. But everything I'm trying won't let me get at the data I need.

All I want to do is take that result set and put it into the $data array like this:
Code:
$data['division'][$key] = $value;

But everything I'm trying isn't working - it's leaving me stuck at the first level of arrays, which then contain arrays which get at the values I need.

#2
[eluser]Sumon[/eluser]
[quote author="Kinsbane" date="1222299031"]
I have my code grab this dataset with an object, and then I try to loop through the object with this:
Code:
foreach($divisions->result_array() as $row)
{
    die(print_r($row)); //just to see what I get
}
However, this only gives me the first record in the dataset ([id] of 1).
[/quote]
First record in the dataset because die() function stop execution immediately after first print_r($row); statement

[quote author="Kinsbane" date="1222299031"]
All I want to do is take that result set and put it into the $data array like this:
Code:
$data['division'][$key] = $value;
But everything I'm trying isn't working - it's leaving me stuck at the first level of arrays, which then contain arrays which get at the values I need.[/quote]
Try to use:
Code:
foreach($my_array as $key=>$value)
{
    $data['division'][$key] = $value['division_code'];
}
print_r($data['division']); //display what have stored
Is it what you want?

#3
[eluser]manilodisan[/eluser]
Lol...die = die Smile)))

#4
[eluser]Kinsbane[/eluser]
Well, I must say, I am a pretty fart smeller. For a while I didn't know why I was getting the last record in the dataset (id #6), because of the way I was assigning the data to the $data['divisions']. (each loop was replacing the data previously there).

I guess at that point I would then need to have $data['divisions'][1], $data['divisions'][2], etc..

#5
[eluser]Kinsbane[/eluser]
[quote author="Kinsbane" date="1222308954"]Well, I must say, I am a pretty fart smeller. For a while I didn't know why I was getting the last record in the dataset (id #6), because of the way I was assigning the data to the $data['divisions']. (each loop was replacing the data previously there).

I guess at that point I would then need to have $data['divisions'][1], $data['divisions'][2], etc..[/quote]

Yup, that did it:
Code:
$index = 0;
foreach($this->divisions->result_array() as $row)
{
    $data['division'][$index] = $row;
    print_r($row);
    $index++;
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.