0

I am trying to check if these following 2 var values exist in the "$query_string_params_and_values_white_list" array or not.

//Values to check:
$table = 'users';
$column = 'kites';

//Array:
$query_string_params_and_values_white_list = array('table'=>array('users','members'),array('column'=>array('keyword','phrase')));

On my test, I get echoed ...

'no exists' 'no exists'

Since the first var value does exist in the array then my result echo should've been:

'exists' 'no exists'

But since I got undesired result it means my code is faulty.

Here is my code ...

$query_string_params_and_values_white_list = array('table'=>array('users','members'),array('column'=>array('keyword','phrase')));

$table = 'users';
$column = 'kites';

if(in_array("$table",$query_string_params_and_values_white_list))
{
    echo 'exists';
}
else
{
    echo 'no exists';
}

if(in_array("$column",$query_string_params_and_values_white_list))
{
    echo 'exists';
}
else
{
    echo 'no exists';
}

Where did I go wrong ? Am checking in array instead of sub-array. Correct ? If so, then how to check sub array ?

Can you show me how I should've coded it ?

Thanks!

  • @Lety, none of them got anything to do with sub-arrays and their keys and values. – Nut Cracking Dude Sep 05 '21 at 20:21
  • 1
    Does this answer your question? [in\_array() and multidimensional array](https://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array) – ArSeN Sep 05 '21 at 20:39
  • @ArSeN, I don't know if that link "in_array() and multidimensional array" answers my question or not because the code samples in there are too complicated for me to understand as I still a beginner at procedural style. However Umnitsyn Vladimir answered my question in this thread below and I accepted his answer. Now you check his answer and decide for yourself if that link you suggested answered my question or not and then get back to me. I prefer Umnitsyn Vladimir's answer though as it is simple code suitable for a beginner. Thanks for trying to help me out, though! – Nut Cracking Dude Sep 08 '21 at 12:36

1 Answers1

0

You can use few different approaches to list subarrays, but first - array definition:

$query_string_params_and_values_white_list = array(
  'table'=>array('users','members'),
  array(
    'column'=>array('keyword','phrase')
  )
);

If its correct and You really want that kind of array, then use something like:

$table = 'users';
$column = 'kites';


foreach ($query_string_params_and_values_white_list as $subarray){
    if(in_array($table,$subarray))
    {
        echo "exists\n";
    }
    else
    {
        echo "no exists\n";
    }
}

foreach ($query_string_params_and_values_white_list as $subarray){ //listing 1st level
    foreach ($subarray as $subsubarray){ //listing 2nd level
        if(in_array($column,$subsubarray))
        {
            echo "exists\n";
        }
        else
        {
            echo "no exists\n";
        }
    }
}

foreach checks every element of array, and by using it twice you can get to elements on 3rd dimension ($query... arrary -> unnamed array -> 'column' array). PHP gives some warnings about that, so I assume You meant:

$query_string_params_and_values_white_list = array(
    'table'=>array('users','members'),
    'column'=>array('keyword','phrase')
);

then searching becomes a bit easier:

$table = 'users';
$column = 'kites';

foreach ($query_string_params_and_values_white_list as $subarray){
    if(in_array($table,$subarray))
    {
        echo "exists\n";
    }
    else
    {
        echo "no exists\n";
    }
}

foreach ($query_string_params_and_values_white_list as $subarray){
    if(in_array($column,$subarray))
    {
        echo "exists\n";
    }
    else
    {
        echo "no exists\n";
    }
}

P.S. don't afraid to decompose array structures - PHP allows that and it gives more confidence in what You trying to do.

Vladimir
  • 368
  • 1
  • 3
  • 9
  • ,Thanks!You talk about unnamed array.; I guess I got my subarray structure wrong. I am weak in this subarray field. I think you corrected it as your structure and code seems to be giving me the result I want. Talking about your 2nd example. – Nut Cracking Dude Sep 08 '21 at 12:27