/    Sign up×
Community /Pin to ProfileBookmark

How To Output Values related To Same Key ?

Folks,

[code]
If($_GET[type] == “personal”)
{
output all values with the “personal” array key.
}
else
{
output all values with the “commercial” array key.
}
[/code]

How to write this code in PHP from following array keys and values ?
What I want to achieve is written in above code conditions.

[code]
$patterns = array(“commercial” => “domain”,”commercial” =>”fax”,”personal” =>”username”,”personal” =>”email”);
[/code]

Which PHP function to use ?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumFeb 27.2021 — Output your array by var_dump and view the result.
Copy linkTweet thisAlerts:
@developer_webauthorFeb 27.2021 — I tried this but didn't work as print_(r) or var_dump() only echos one value instead of two.

Why ?

<i>
</i>$parameters = ['commercial' =&gt; 'domain', 'commercial' =&gt; 'fax','personal' =&gt; 'username', 'personal' =&gt; 'email'];
$allowedKeys = ['personal'];
$filteredParameters = array_intersect_key($parameters, array_flip($allowedKeys));

echo var_dump($filteredParameters);


array(1) { ["personal"]=> string(5) "email" }
Copy linkTweet thisAlerts:
@SempervivumFeb 27.2021 — >only echos one value instead of two.

var_dump works correctly, the resulting array $patterns indeed has only two entries. The reason is that only one key is possible, if you assign a value to a key that is already present it will overwrite the previous value, not create an additional entry with the same key.
Copy linkTweet thisAlerts:
@developer_webauthorFeb 28.2021 — @Sempervivum#1628643


Mmm. So how can I create group values then ?

You saw above that I created two groups. Personal, Commercial.

Now I want each group to have more than one value.

Don't tell me, I am going to create arrays within arrays, like following as I hate nested stuffs. Feels like a Python tangling everything round and round with its body.

<i>
</i>$array = array(array("username","email"),array("domain","fax");

Even though the above has 2 nested arrays (if that is the right terminology) they are not named. So how to name them ? In tutorials, I have seen arrays like the above but never like the below, which would be much better:

<i>
</i>$array = array($personal = array("username","email"),$commercial = array("domain","fax");

[/code]
Copy linkTweet thisAlerts:
@SempervivumFeb 28.2021 — >Don't tell me, I am going to create arrays within arrays

Indeed this was my first thought and IMO it's the best bet in order to group the data. You can make the structure more clear and readable by sticking to associative arrays:
```
$data = [
'username'=>'John Doe',
'personal'=>[
'phone'=>'05550267',

'email'=>'[email protected]'
],
'commercial'=>[
'phone'=>'057602562',

'email'=>'[email protected]'
'fax'=>'02506752'
]
];
×

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,
)...