/    Sign up×
Community /Pin to ProfileBookmark

How To Check For Sub-Array keys & Values ?

Hi,

I need to check for keys and values inside a sub-array. How to do that ?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorSep 05.2021 — Folks,

Now, 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!
Copy linkTweet thisAlerts:
@NogDogSep 05.2021 — You'd have to check for:
[code=php]in_array($column, $query_string_params_and_values_white_list['column'])[/code]

If you want to check all sub-arrays, then you'd have to do some sort of loop on the array, or dive in to the confusion that is [array_walk_recursive()](https://www.php.net/manual/en/function.array-walk-recursive.php).
Copy linkTweet thisAlerts:
@developer_webauthorSep 08.2021 — @NogDog#1636522

Can you show me code sample for the array_walk_recursive() as this is new to me. Might aswell learn this as a bonus from you.

As for looping in the array, I got help. Look:


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

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

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


It seems to be working. Do you think the code is ok and valid ?

Originally, my 2 sub arrays (table, column) were like this and I was told one was nameless:
<i>
</i>$query_string_params_and_values_white_list = array('table'=&gt;array('users','members'),array('column'=&gt;array('keyword','phrase')));


I was then suggested to name the sub array, like this:
<i>
</i>$query_string_params_and_values_white_list = array(
'table'=&gt;array('users','members'),
array(
'column'=&gt;array('keyword','phrase')
)
);


I think the suggestion was right. But I want your confirmation. I know you will say, it depends on what I wanted done.

This was wrong:
<i>
</i>array('column'=&gt;


This is right:
<i>
</i>column'=&gt; array(


So I was told.

In short, I added the extra:
<i>
</i>array(


Bold parts are the extras I never should have added.

$query_string_params_and_values_white_list = array(

'table'=>array('users','members'),

**array(**'column'=>array('keyword','phrase')**)**

);



Anyway, let me tell you what I originally wanted done.

My dynamic url will only be valid if it contains the "table" and "column" to represent Mysql Table and Column.

Eg:

https://localhost/Templates/Pagination_Template.php?table=users&column=keywords

I have a whie-list for both the tables and columns:
<i>
</i>//Valid list of Mysql Tables.
$table_white_list = array('sale','sold','links');
//Valid list of Mysql Table Columns.
$column_white_list = array('email','domain','url','anchor','description','keyword');


Now, if someone typed this url:

https://localhost/Templates/Pagination_Template.php?table=users&column=kites

You can see it's invalid because there is no "kites" column in the columns white-list.

Now, I wanted to write code to check if the table value and the column value in the url ($_GET) are valid or not. And so, created code with sub-array like this:
<i>
</i>$query_string_params_and_values_white_list = array('table'=&gt;array('users','members'),array('column'=&gt;array('keyword','phrase')));


And I wrote the checking code like you see in my original post and so not bothering here. Check my original post.

Note that this is the first time I writing multi-dimensional arrays and so my sub-array structure was probably wrong. You tell me, was it wrong or right ? Don't forget to answer this because I don't want to pester you again for the answer.

Anyway, I was told I wrote a nameless sub-array and my multi-dimensional array code should actually be like this:
<i>
</i>$query_string_params_and_values_white_list = array(
'table'=&gt;array('users','members'),
'column'=&gt;array('keyword','phrase')
);


Q1.

Now what is your professional opinion ?

Q2,

And my checking code should be like this based on the above code, I was told:
<i>
</i>$table = 'users';
$column = 'kites';

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

But I get echoed:

exists no exists

no exists no exists

I shouldn't get echoed like that. I should get echoed like this:

exists

no exists

Meaning 'users' exist in tables sub-array,

Kites do not exist in columns sub-array.

So how to fix the "checking code" ? Can you show me sample code ?

Thanks!
Copy linkTweet thisAlerts:
@ginerjmSep 08.2021 — The only thing that I can see is a whole lot of very complicated structures that you need help with when a simple database would be a whole lot easier to manage and understand.

Decide what your sets of data are and build a table(s) that organizes them; then write a query to extract what you need from that. I"m sure you have written that before, so why not now?
Copy linkTweet thisAlerts:
@developer_webauthorSep 08.2021 — @ginerjm#1636693

I am trying to learn how to extract keys and values from sub arrays.

Don't you know my questions' answers ? Did I ask too complicated question for a beginner like me to an adv programmer like you ?
Copy linkTweet thisAlerts:
@ginerjmSep 08.2021 — Since it's been over a year since I've been here I'm surprised that you are still a 'beginner'.
Copy linkTweet thisAlerts:
@developer_webauthorSep 08.2021 — @ginerjm#1636693

I am trying to learn how to extract keys and values from sub arrays.

Don't you know my questions' answers ? Did I ask too complicated question for a beginner like me to an adv programmer like you ?

Here is the original thread that will tell you ALL. Tell you that I dealing with whitelists (arrays, subarrays):

https://www.webdeveloper.com/d/396413-how-to-check-for-sub-array-keys-values
Copy linkTweet thisAlerts:
@developer_webauthorSep 08.2021 — @developer_web#1636708

I've been fiddling with php since 2017. You've kindly plus ill manneredly replied to me before in other forums.

Google for:

"uniqueideaman AND ginerjm"

"phpsane AND ginerjm"

I can't remember all my past usernames in other forums where you have replied sometimes nicely but most of the time very very rudely.

Why are you so haughty ?

And yes, I'm still a beginner since I am struggling still at procedural style and oop and objects do my head-in.

EDIT 1:

Oops! I just found this on google:

https://www.webdeveloper.com/d/368269-how-you-would-insert

@NogDog

Can you delete my old username's account NogDog ?

https://www.webdeveloper.com/u/site-developer

Joined Oct 18, 2017

97 discussions/threads

318 posts

Bluddy Hell! Seems like I made 97 discussions/threads and 318 posts with that old Username of mine.

Let me check how many I made with this one ...

https://www.webdeveloper.com/u/developer_web

Joined Sep 10, 2019

Bluddy Hell, even more! Seems like I made 342 discussions/threads and 1594 posts !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

WTF!

Sorry. I didn't open two accs at the same time. I stopped attending this forum with the old username. Attended other forums for a year or two before returning back to this forum. After returning, I forgot the Username and thought it's probably been deleted since my acc has been inactive for long and so I opened this new acc under new Username.

EDIT 2:

Oops! I think this was my original account as I used that username across 10-20 forums:

https://www.webdeveloper.com/u/uniqueideaman

Joined Oct 12, 2016

Discussions/Threads: 56

Posts: 170

Yeah, delete that account too!

NOTE:

site-developer and developer_web usernames were based around this forum's domain name.

Originally, I used UniqueideaMan on 10 or so forums and then got banned for cross posting. And so, then I went around using different Usernames for each forum. The Usernames were based around the forums' domain names so I can easily remember my Usernames.
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...