Jump to content

Need Help with in_array


unemployment

Recommended Posts

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

        )

 

)

Link to comment
Share on other sites

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']

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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";
}
?>


Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.