unemployment Posted May 16, 2011 Share Posted May 16, 2011 I can't access an array value in a multidimensional array. The if should be echoing true. $news['companyid'] is equal to 1, but I must not be using in_array correctly. echo '<pre>'; print_r($own_company); if (in_array($news['companyid'], $own_company)) { echo "true"; } Output Array ( [0] => Array ( [companyid] => 1 [companyname] => Oaysus [companytag] => [companywebsite] => http://oaysus.com [country] => 1 [state] => 0 [city] => [industry] => 4 [stage] => 2 [capitalrequested] => [guestviews] => 0 [iviews] => 3 [eviews] => 3 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/236552-need-help-with-in_array/ Share on other sites More sharing options...
kenrbnsn Posted May 16, 2011 Share Posted May 16, 2011 Try <?php if (in_array($news['companyid'], $own_company[0])) ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/236552-need-help-with-in_array/#findComment-1216050 Share on other sites More sharing options...
unemployment Posted May 16, 2011 Author Share Posted May 16, 2011 Try <?php if (in_array($news['companyid'], $own_company[0])) ?> Ken That works, but I need it to be dynamic for all of the companies I own. UPDATE: Actually... this still fails because it doesn't check the companyid. I don't want it to search the whole array. I want it to search $own_company['companyid'] Quote Link to comment https://forums.phpfreaks.com/topic/236552-need-help-with-in_array/#findComment-1216055 Share on other sites More sharing options...
unemployment Posted May 16, 2011 Author Share Posted May 16, 2011 I have it almost fully working but I need the zero to be dynamic. $own_company = fetch_owned_companies($user_info['uid']); if ($news['companyid'] == $own_company[0]['companyid']) { $viewer_user_lvl = 7; } else { $viewer_user_lvl = user_lvl($news['id']); } Quote Link to comment https://forums.phpfreaks.com/topic/236552-need-help-with-in_array/#findComment-1216078 Share on other sites More sharing options...
PFMaBiSmAd Posted May 16, 2011 Share Posted May 16, 2011 I cannot help but to wonder if this data is coming from a database query and you shouldn't actually be putting the condition(s) into a WHERE clause so that you only retrieve the row(s) you need instead of trying to scan through the results of a query using some slow parsed, tokenized, interpreted php code? What overall goal are you trying to achieve and what does the data look like and what are you trying to match? Quote Link to comment https://forums.phpfreaks.com/topic/236552-need-help-with-in_array/#findComment-1216083 Share on other sites More sharing options...
JonnoTheDev Posted May 16, 2011 Share Posted May 16, 2011 I have it almost fully working but I need the zero to be dynamic. That doesn't make sense. Are you stating that you need a loop? Are you trying to put all company ids into an array i.e <?php $own_company = fetch_owned_companies($user_info['uid']); $company_ids = array(); for($x = 0; $x < count($own_company); $x++) { $company_ids[] = $own_company[$x]['companyid']; } if (in_array($news['companyid'], $company_ids)) { echo "true"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/236552-need-help-with-in_array/#findComment-1216095 Share on other sites More sharing options...
unemployment Posted May 16, 2011 Author Share Posted May 16, 2011 I cannot help but to wonder if this data is coming from a database query and you shouldn't actually be putting the condition(s) into a WHERE clause so that you only retrieve the row(s) you need instead of trying to scan through the results of a query using some slow parsed, tokenized, interpreted php code? What overall goal are you trying to achieve and what does the data look like and what are you trying to match? $own_company fetches a php function that verifies if a user owns a company. It runs a mysql query. The goal is to see... if you own the company set the user_lvl to 7 making the privacy settings not affect your viewing ability. If the user_lvl is below 7 and you don't own a company, display an avatar that corresponds with the users privacy settings. This data is all wrapped in a foreach loop that makes a news feed. So if I own the company and I am viewing the companies avatar, show my the avatar. If someone else creates a company action and I don't own the company, but I can still see the company info and the company has a privacy settings of 6 and I have a user_lvl of 3 then don't display the avatar. This is hard to explain. There is a lot going on here. Quote Link to comment https://forums.phpfreaks.com/topic/236552-need-help-with-in_array/#findComment-1216096 Share on other sites More sharing options...
unemployment Posted May 16, 2011 Author Share Posted May 16, 2011 I have it almost fully working but I need the zero to be dynamic. That doesn't make sense. Are you stating that you need a loop? Are you trying to put all company ids into an array i.e <?php $own_company = fetch_owned_companies($user_info['uid']); $company_ids = array(); for($x = 0; $x < count($own_company); $x++) { $company_ids[] = $own_company[$x]['companyid']; } if (in_array($news['companyid'], $company_ids)) { echo "true"; } ?> The thing that I think everyone is missing is that you can own multiple companies. So when each company loads, scan the array to see if the companyid is owned by you. If it is show the avatar... if not... display it depending on privacy settings. I don't know if this requires a for loop, but it would need to scan all the [companyid]'s in the arrays. Quote Link to comment https://forums.phpfreaks.com/topic/236552-need-help-with-in_array/#findComment-1216099 Share on other sites More sharing options...
JonnoTheDev Posted May 17, 2011 Share Posted May 17, 2011 Then the above code works. Have you even tried it? It seems like you do not understand the code that I have given you to see what it is doing. Are you just looking for a cut and paste answer? <?php /* fetch array of companies owned by user */ $own_company = fetch_owned_companies($user_info['uid']); /* add all the company ids to a separate array */ $company_ids = array(); for($x = 0; $x < count($own_company); $x++) { $company_ids[] = $own_company[$x]['companyid']; } /* if $news['companyid'] is in the owned companies do something */ if (in_array($news['companyid'], $company_ids)) { echo "true"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/236552-need-help-with-in_array/#findComment-1216401 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.