Re: How to force an associative array
| From: | JEFFRY KILLEN | Date: | Mon, 23 Jan 2023 00:14:19 +0000 |
| Subject: | Re: How to force an associative array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-328137@lists.php.net to get a copy of this message | ||
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 <john.iliffe@iliffe.ca> 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
> ======