Page 1 of 1
updating associative array in array
Posted: Thu Mar 25, 2004 2:11 am
by microthick
I have an array structure that looks like this:
Array
(
[0] => Array
(
[name] => Content-Type
[content] => some content type
)
[1] => Array
(
[name] => Content-Transfer-Encoding
[content] => some content transfer encoding
)
[2] => Array
(
[name] => Content-Encoding
[content] => some content encoding
)
)
So, I can easily push new associations onto the array:
array_push('name' => 'Content-SuperDuper-Encoding', 'content' => 'some content super duper encoding');
But, what if I want to update an existing key?
I can't seem to come across the right code. This is what I've tried, and is NOT working:
Code: Select all
foreach ($headers as $header) {
if ($headerї'name'] == $name) {
$headerї'content'] = $content;
}
}
Any ideas?
Posted: Thu Mar 25, 2004 1:59 pm
by Farrier
I suspect it's not that code that's broken, but rather that your push is broken.
Try displaying your $headers datastructure using var_dump() or print_r().
I *suspect* that your push is pushing those values on as individual values, rather than as a separate associative array.
From the manual, my emphasis:
int array_push ( array array, mixed var [, mixed ...])
array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as: <?php
$array[] = $var;
?>
repeated for each var.
I suspect that what you REALLY want, instead of array_push, is just:
Code: Select all
# Create a new message.
$new_msg=array('name' => 'jim', 'content'=>'some text');
# Append new associative array to existing headers.
$headers[]=$new_msg;
Posted: Thu Mar 25, 2004 5:04 pm
by microthick
I'm sorry, I posted the wrong code regarding how I was array_push'ing.
It actually looks like this:
Code: Select all
array_push($someArray, array('name' => 'Content-SuperDuper-Encoding', 'content' => 'some content super duper encoding'));
So I have an array called $someArray and I'm pushing a new associative array into that array.
I can push things into arrays fine, but I'm having trouble updating existing associations that are contained within that array.
Posted: Thu Mar 25, 2004 6:01 pm
by Farrier
Ah, I see. No, you can't use foreach to modify an array like that, since it creates a temporary copy of each element of the array that it loops through.
Code: Select all
<?php
# This creates $headers as an array of arrays.
$headers= array(
array ('name' => 'name1', 'content' => 'encoding1'),
array ('name' => 'name2', 'content' => 'encoding2'),
array ('name' => 'name3', 'content' => 'encoding3'),
);
# This pushes some values.
array_push($headers, array('name'=>'name4', 'content'=>'encoding4'));
array_push($headers, array('name'=>'name5', 'content'=>'encoding5'));
# This prints it out
print_r($headers);
# This doesn't alter any internal values.
# Only temporary values of '$header' are changed.
foreach ($headers as $header) {
$header['name']='fish';
}
# This prints it out
print_r($headers);
# This does alter internal values.
foreach ($headers as $key => $val) {
$headers[$key]['name']='fish';
}
# This prints it out
print_r($headers);
?>
Posted: Fri Mar 26, 2004 3:49 am
by microthick
Thanks!
