I have a PHP page with a project ID. I run a query using a class and return a data set of attributes of some of the items associated with this project ID. The data is returned in the following object format:
Array
(
[0] => stdClass Object
(
[id] => 1
[projectID] => 469
[itemID] => item_1
[weight] => 20
[length] => medium
)
[1] => stdClass Object
(
[id] => 2
[projectID] => 469
[itemID] => item_2
[weight] => 50
[length] => medium
)
[2] => stdClass Object
(
[id] => 3
[projectID] => 469
[itemID] => item_4
[weight] => 75
[length] => long
)
)
So, now I need to add these attributes to the page element. Each page element has unique ID that matches itemID from returned data, but I'm having hard time figuring out how to match the two.
<div id="item_2">'.( $myarray[1]['itemID'] == 'item_2' ?
$myarray[1]['weight'] : '0' ).' lb</div>
<div id="item_2">'.( $myarray[1]['itemID'] == 'item_2' ?
$myarray[1]['length'] : '0' ).' ft</div>
<div id="item_3">'.( $myarray[2]['itemID'] == 'item_3' ?
$myarray[2]['weight'] : '0' ).' lb</div>
<div id="item_3">'.( $myarray[2]['itemID'] == 'item_3' ?
$myarray[2]['length'] : '0' ).' ft</div>
itemIdkeys are unique then make a loop (or use array_walk) that will go through returned array of objects and will produce array indexed byitemId. Then under index$arr['item_3']you will have data from original array, so you can for example echo<div id="item_3">'.$arr['item_3']->wiegth.'</div>. Dunno if this is what you mean. I think you should reedit question to be more clear.$myarray['item_4']->weightif you iterate through whole array ONCE and create another array from the original one.itemIdones.