Re: How to force an associative array
[email protected] (John Iliffe)
| Newsgroups | php.general |
|---|---|
| Message-ID | <[email protected]> |
Thanks Jeffry:
Looks like I forgot to provide a big enough chunk of the programme code :-(
This is entirely within a group of nested loops that select one header at a
time, then run through the required header list (each of which is an array, but
with enumerated members rather than associative. I need the associative array
for the last result because I need to be able to select by header name. (these
are selections from the standard e-mail headers, and what is received includes
many that are a bit unpredictable).
Anyhow, here is the full code for the selection, when this clip starts the
received headers are in an array $_headers[] that will be presented one at a
time to the selection process, in one random example I found 47 headers and I
suppose more are possible. $_i will be used for future requirements that aren't
written yet. I added the remark "******here***" on the line where the problem
seems to arise to make it easier to find the problem that I am having; it isn't
actually in the programme. All the fwrite() are for debugging and will
eventually be removed.
---
for ($_j=0;$_j<$_num_headers;$_j++)
{
$_i=0;
$_the_header = $_headers[$_j]; // <---this is the received headers, one at a
time
for ($_k=0;$_k<count($_head_reqd);$_k++)
{
fwrite($_xf,("\ntesting [" . $_k . "] = >" . $_head_reqd[$_k] . "< against
header >" . $_the_header . "\n"));
if (($_pos = stripos($_the_header,$_head_reqd[$_k])) !== false)
{
if ($_pos < 3)
{
$_name = substr($_head_req[$_k],0,-1); // knock off ':'
******here*** $_head_array[$_name] = $_the_header; // primary header;
may be more, eg "To:"
fwrite ($_xf, "\n--got a match on " . $_head_reqd[$_k] ." header request---\n");
}
}
}
}
---
Regards,
John
======
On Sun, 2023-01-22 at 16:14 -0800, JEFFRY KILLEN wrote:
> It looks like you have not declared and intialized $_k before using it
>
> > if (($_pos = stripos($_the_header,$_head_reqd[$_k])) !== false)
>
> I believe you should declair it and initialize it in a higher scope like so:
>
> $_k = 0;
> > if (($_pos = stripos($_the_header,$_head_reqd[$_k])) !== false)
>
> You are also not incrementing $_k that I can see
>
> > On Jan 22, 2023, at 2:20 PM, John Iliffe <[email protected]> wrote:
> >
> > I have an application where the members (by name) of an associative array are
> > not known until run time. I have been unable to force the member names to the
> > correct name, what I get is a single addition to the array with the name 0,
> > indicating that this is being created with the elements numbered. All added
> > members of the array disappear except the last one added.
> >
> > $_head_array = array("start"=>"start");
> >
> > if (($_pos = stripos($_the_header,$_head_reqd[$_k])) !== false)
> > {
> > if ($_pos < 3)
> > {
> > $_name = substr($_head_req[$_k],0,-1); // knock off ':'
> > $_head_array[$_name] = $_the_header;
> > }
> > }
> >
> > ... etc
> >
> > What this is doing is iterating through $_head_reqd (a list of header names) to
> > find out if $_the_header is one of these. If so, it is added to the associative
> > array $_head_array. I set up $_head_array with a first element "start"=>"start"
> > to force (as I thought) an associative array.
> >
> > What results (as displayed by print_r):
> >
> > -----------selected headers--------
> > Array
> > (
> > [start] => start
> > [0] => Subject: 876543
> > )
> > ----------end of selected headers-----
> >
> > Several hits preceding "Subject" are not recorded even though they were selected
> > initially, and the last line SHOULD read "[Subject] => Subject: 876543"
> >
> > How can I force the proper element names?
> >
> > Thanks in advance.
> >
> > John
> > ======
>