Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

looping array issue

$
0
0
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

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>