Xtremer360 Posted September 17, 2012 Share Posted September 17, 2012 I have the following code that works however I want to add to it. I have another array of values that I want to see if the news_article_category_id exists inside the array and if it is then make the checked value of the checkbox set to checked. How would that be accomplished? <?php foreach ($news_article_categories as $news_article_category) { $data = array('name' => 'news_article_categories', 'id' => $news_article_category->news_article_category_id); echo form_label(form_checkbox($data).$news_article_category->news_article_category_name); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/268454-checking-array-values/ Share on other sites More sharing options...
ManiacDan Posted September 17, 2012 Share Posted September 17, 2012 With IF and in_array Quote Link to comment https://forums.phpfreaks.com/topic/268454-checking-array-values/#findComment-1378596 Share on other sites More sharing options...
Xtremer360 Posted September 17, 2012 Author Share Posted September 17, 2012 <?php foreach ($news_article_post_tags as $news_article_post_tag) { if (in_array($news_article_post_tag->news_article_post_tag_id, $news_articles_index_post_tags_data)) { $checked_value = 'checked'; } else { $checked_value = ''; } $data = array('name' => 'news_article_post_tags', 'id' => $news_article_post_tag->news_article_post_tag_id, 'checked' => $checked_value); echo form_label(form_checkbox($data).$news_article_post_tag->news_article_post_tag_name); } ?> I ran a print_r on both arrays and this is the result. Array ( [0] => stdClass Object ( [news_article_category_id] => 1 [news_article_category_name] => Headlines [news_article_posting_permission_name] => Editors and Admins and Webmasters Only ) [1] => stdClass Object ( [news_article_category_id] => 3 [news_article_category_name] => Columns [news_article_posting_permission_name] => Editors and Admins and Webmasters Only ) [2] => stdClass Object ( [news_article_category_id] => 4 [news_article_category_name] => Main News [news_article_posting_permission_name] => Editors and Admins and Webmasters Only ) [3] => stdClass Object ( [news_article_category_id] => 2 [news_article_category_name] => Rumors [news_article_posting_permission_name] => All Users ) ) Array ( [0] => stdClass Object ( [news_article_id] => 1 [news_article_category_id] => 1 ) ) I'm trying to figure out why none of the checkboxes are being checked. Quote Link to comment https://forums.phpfreaks.com/topic/268454-checking-array-values/#findComment-1378701 Share on other sites More sharing options...
ManiacDan Posted September 17, 2012 Share Posted September 17, 2012 Well it's an array of objects. You need to roll your own solution to in_array which will actually check the object member variables. Quote Link to comment https://forums.phpfreaks.com/topic/268454-checking-array-values/#findComment-1378703 Share on other sites More sharing options...
Xtremer360 Posted September 17, 2012 Author Share Posted September 17, 2012 What do you mean? Quote Link to comment https://forums.phpfreaks.com/topic/268454-checking-array-values/#findComment-1378706 Share on other sites More sharing options...
ManiacDan Posted September 17, 2012 Share Posted September 17, 2012 in_array doesn't work with arrays of objects. You have arrays of objects. You need to know if one incomplete object is inside a list of other more-complete objects. You need to write a function to check that. You may hit a snag when you realize that one array's objects have category_id and the other array's objects have category_name. Quote Link to comment https://forums.phpfreaks.com/topic/268454-checking-array-values/#findComment-1378716 Share on other sites More sharing options...
Xtremer360 Posted September 17, 2012 Author Share Posted September 17, 2012 Still having an issue with this. If you notice in the bottom var dump you notice how both of the objects have a news article id of 1 which is the article your viewing which is right. Now the tags associated with it is tag id 1 and tag id of 2. So if you look above those are all the post tags in the database. I need it to take those two post tag ids of 1 and 2 and scroll through the top object and have the post tag with an id of 1 and 2 be checked so according to this the checkbox of Kid Wonder and Oriel should both be checked. I tried the in_array but couldn't find a working solution. news_article_post_tags array(3) { [0]=> object(stdClass)#33 (2) { ["news_article_post_tag_id"]=> string(1) "1" ["news_article_post_tag_name"]=> string(10) "Kid Wonder" } [1]=> object(stdClass)#34 (2) { ["news_article_post_tag_id"]=> string(1) "2" ["news_article_post_tag_name"]=> string(5) "Oriel" } [2]=> object(stdClass)#35 (2) { ["news_article_post_tag_id"]=> string(1) "3" ["news_article_post_tag_name"]=> string(11) "Ryu Satoshi" } } news article index post tags data array(2) { [0]=> object(stdClass)#37 (2) { ["news_article_id"]=> string(1) "1" ["news_articles_post_tag_id"]=> string(1) "1" } [1]=> object(stdClass)#38 (2) { ["news_article_id"]=> string(1) "1" ["news_articles_post_tag_id"]=> string(1) "2" } } s Quote Link to comment https://forums.phpfreaks.com/topic/268454-checking-array-values/#findComment-1378743 Share on other sites More sharing options...
ManiacDan Posted September 17, 2012 Share Posted September 17, 2012 For the third time now: in_array won't work here. These are arrays of objects. Your var-dump output keeps changing, so I'm going to leave you to figure this out on your own. You have a problem: You have two arrays of objects. You wish for the objects in array1 to do something special when their attribute matches the attribute of any object in array2. Break down that problem. Write a function which accepts 2 inputs (a news_article_post_tag object, and the news article index post tags data array) and returns TRUE if the news_article_post_tag object matches any of the items in the index post tags array. Then, use it when you print these items to check them when appropriate. Quote Link to comment https://forums.phpfreaks.com/topic/268454-checking-array-values/#findComment-1378754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.