2 Replies - 3903 Views - Last Post: 04 January 2013 - 05:04 AM Rate Topic: -----

#1 skirkpatrick  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-February 08

looping array issue

Posted 03 January 2013 - 09:44 AM

I have an array of objects and I am trying to change the values based on the the objects position in the array. This is a simplified version of my order_table array.

Array
(
    [0] => stdClass Object
        (
            [table-position] => 1
            [row-type] => T
            [item-id] => 
        )

    [1] => stdClass Object
        (
            [table-position] => 2
            [row-type] => T
            [item-id] => 
        )

)


I am trying to use a while loop to take the values of the order array so the "order_table" array is updated with the values from the "order" array.

						$r = 0;
						while( $r < 2)
						{
						$order_table[$r]->{'item-id'} 		= $order->products[$r]['name'];
						$r++;
						}



order Object
(
    [products] => Array
        (
            [0] => Array
                (
                    [qty] => 2400
                    [name] => PFG-100063
                )

            [1] => Array
                (
                    [qty] => 500
                    [name] => PFG-100419
                )

        )


So that my original array would then become:
Array
(
    [0] => stdClass Object
        (
            [table-position] => 1
            [row-type] => T
            [item-id] => PFG-100063
        )

    [1] => stdClass Object
        (
            [table-position] => 2
            [row-type] => T
            [item-id] => PFG-100419
        )

)



But when I run the code I get the "item-id value repeated:
Array
(
    [0] => stdClass Object
        (
            [table-position] => 1
            [row-type] => T
            [item-id] => PFG-100419
        )

    [1] => stdClass Object
        (
            [table-position] => 2
            [row-type] => T
            [item-id] => PFG-100419
        )

)


If I try to simply change a specific value outside of the loop it repeats the value
						$order_table[1]->{'item-id'} 		= "test";



I get:
Array
(
    [0] => stdClass Object
        (
            [table-position] => 1
            [row-type] => T
            [item-id] => test
        )

    [1] => stdClass Object
        (
            [table-position] => 2
            [row-type] => T
            [item-id] => test
       )

)


Not sure what I am doing wrong. Any insight would be greatly appreciated.

Thanks

This post has been edited by Dormilich: 03 January 2013 - 09:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: looping array issue

#2 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: looping array issue

Posted 04 January 2013 - 02:12 AM

use a for-loop, it is clearer for such things:

for($r = 0; $r<=2; $r++)
{
    $order_table[$r]->{'item-id'} = $order->products[$r]['name'];
}


Was This Post Helpful? 0
  • +
  • -

#3 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: looping array issue

Posted 04 January 2013 - 05:04 AM

Can you post more code, like where you create the object and arrays?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1