kmartel;10988466 wrote:
I do realize that I am searching for strings, but after doing more research it looks like my "strings" (aka what I am defining as flags) are not tied to the original key of User1 or User2, etc. They are getting their own Key subsequent to the next key, correct?
Yes, because they are indeed not tied to any preset key at all. Have a look at the array
$users = array(
'First Last1' => 'some.biz1.host.com', 'AO',
'First Last2' => 'some.biz2.host.com', 'ALD',
'First Last3' => 'some.biz3.host.com', 'OD'
);
# is the same as
$users = array(
'First Last1' => 'some.biz1.host.com',
'First Last2' => 'some.biz2.host.com',
'First Last3' => 'some.biz3.host.com',
'AO',
'ALD',
'OD'
);
# is the same as
$users = array(
'First Last1' => 'some.biz1.host.com',
'First Last2' => 'some.biz2.host.com',
'First Last3' => 'some.biz3.host.com',
0 => 'AO',
1 => 'ALD',
2 => 'OD'
);
kmartel;10988466 wrote:
So, it would prove difficult to match my "flags" to the key I want them to belong to .. at least for me because I am still learning.
First off, I wonder why the array looks like this in the first place. I suspect it should be constructed some other way to begin with...
But, IF there is indeed a connection between the array values that is NOT present in its structure, such that 'AO' really does belong with 'First Last1', 'ALD' really belongs with 'First Last2' etc, then you could
$users = array(
'First Last1' => 'some.biz1.host.com', 'AO',
'First Last2' => 'some.biz2.host.com', 'ALD',
'First Last3' => 'some.biz3.host.com', 'OD'
);
# iterate over all keys (both integer and string keys)
# This happens in the order they are inserted
foreach ($users as $u)
{
echo $u . '<br/>';
}
# which means that you can do this...
# used to keep track of your integer indexed elements
$i = 0;
foreach ($users as $k => $u)
{
# only execute this for string keys, not integer keys
if (is_string($k))
{
# Referenced users['First Last1'] etc, and assigns an array of
# array('some.biz1.host.com', 'AO') etc
$users[$k] = array($u, $users[$i]);
# remove 'AO' etc from $users - But the foreach loop is not affected by this
# even though the array $users is updated to reflect this after the loop
unset($users[$i]);
# update $i to reference the next integer key
++$i;
}
}
printf('<pre>%s</pre>', print_r($users,1));
kmartel;10988466 wrote:
I suppose what I need is a snippit/function that can simply check if that string (flags) match the key before it and return 1 or 0.
No, becuase there really is no connection between the two present in the data structure. What you should do is build a data structure that matches your needs, which means that things that are supposed to belong with each other really are grouped together.
kmartel;10988466 wrote:
for the most part, quite advanced with no "dumbing down" effect.
I wouldn't worry too much about that. You should get the hang of this with experience and practice.
Another thing you could do with the code snippet I provided you with is to add string keys to the nested arrays, like this
$users[$k] = array('url' => $u, 'flags' => $users[$i]);
It doesn't change the data in the array, just the keys. But it will make code that accesses this array in other parts of your script more easily readable, since it's more obvious what $users[0]['url'] contains than $users[0][0] contains.