1

So, I have the following php:

<?php 
$user_id = get_current_user_id();   
$user_number= get_user_meta($user_id, 'number', false); 
$numbers= print_r($user_number);    
?>

I get the following:

Array
(
[0] =&gt; Array
    (
        [1] =&gt; 769099
        [2] =&gt; 768785
        [3] =&gt; 769135
        [4] =&gt; 769118
        [5] =&gt; 769136
        [6] =&gt; 769122
        [7] =&gt; 769130
    )

)

Now, I am trying to use in_array to add a condition as following:

<?php if (in_array ($number_id, $numbers)){?>

where $number_id is one of the number in the array.

I have two questions:

Do I have to use print_r to get the values instead of simply saying Array in order to use in_array?

How do I actually user in_array? in this case?

(For example, using the get_user_meta, I simply get Array. I don't want to use print_r. How do I do this? Thanks!)

4
  • in_array(value, $array) Commented Dec 8, 2015 at 7:15
  • 1
    ` $numbers` is multidimension. use in_array ($number_id, $numbers[0]) Commented Dec 8, 2015 at 7:17
  • what's the difference between 0 and 1? Commented Dec 8, 2015 at 7:20
  • the [0] means the index of the array you want to check Commented Dec 8, 2015 at 7:23

3 Answers 3

3

print_r() is normally print your array. Your code Should be:

<?php 
$user_id = get_current_user_id();   
$user_number= get_user_meta($user_id, 'number', false);   
?>

If you want to check number_id in your current user number array than your code should be :

if(in_array('769118',$user_number[0])){ // 769118 is $number_id
    echo "Match Found";
}else{
    echo "No Match Found";  
}

You can refer this link : in_array

Sign up to request clarification or add additional context in comments.

Comments

1

print_r() is only a function to display the values within an array.

In your case the function get_user_meta() retrieve the numbers associated with the user_id and nothing more.

And the function in_array() is there to check to the existence of a specific value within an array.

1 Comment

So, I got rid of print_r. And <?php if (in_array ($number_id, $user_number)){?> Is this correct? (doesn't seem to work)...
0

try this

<?php if (in_array ($number_id, $user_number[0])){?>

2 Comments

what's the difference between 0 and 1?
There is a difference.. 0 != 1.. this answer doesn't solve the problem.. @Sugumar you've added 2 answer but both are wrong.. you should pay more attention before adding answers

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.