0

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>
6
  • If itemId keys are unique then make a loop (or use array_walk) that will go through returned array of objects and will produce array indexed by itemId. 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. Commented Jul 5, 2014 at 17:41
  • I can have a few of the items on a page and I think it's going to affect my loading time is I was to loop though that array to match a set of attributes for each item. I wish I could simply do $myarray['item_4']['weight'] but I can't unless I add add top node ID, i.e. $myarray[2]['item_4']['weight']. Is there a way to break that huge object? Commented Jul 5, 2014 at 17:52
  • You can simply do $myarray['item_4']->weight if you iterate through whole array ONCE and create another array from the original one. Commented Jul 5, 2014 at 18:00
  • I'm not sure I understand how would I create another array. There will have to be an array for each item? Commented Jul 5, 2014 at 18:12
  • yes, create another array or just replace default numerical keys with itemId ones. Commented Jul 5, 2014 at 19:31

1 Answer 1

1

Assuming that item_id keys are unique this code creates array indexed by item_id from the original array.

$newArr = array();
//create reindexed array based on data from original array ($orgArr)
array_walk($orgArr, function($v) use (& $newArr) { $newArr[$v->item_id] = $v; } );

Then you can access individual properties in the following way.

echo $newArr['item_1']->weight;
echo $newArr['item_2']->length;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.