Adding an element to a multidimensional array
Posted
by stef
on Stack Overflow
See other posts from Stack Overflow
or by stef
Published on 2010-03-18T15:23:51Z
Indexed on
2010/03/18
15:51 UTC
Read the original article
Hit count: 136
php
How can I loop through the array below and an element per array, with key "url_slug" and value "foo"? I tried with array_push but that gets rid of the key names (it seems?) Doing a foreach($array as $k => $v) doesn't do it either, I think.
The new array should be exactly the same only having 4 elements per array instead of 3, with the key / values above.
Array
(
[0] => Array
(
[name_en] => Test 5
[url_name_nl] => test-5
[cat_name] => mobile
)
[1] => Array
(
[name_en] => Test 10
[url_name_nl] => test-10
[cat_name] => mobile
)
[2] => Array
(
[name_en] => Test 25
[url_name_nl] => test-25
[cat_name] => mobile
)
)
EDIT: full working solution. A little more complex than originally described
foreach ($prods as $key => &$value)
{
if($key == "cat_name") $slug = $value['cat_name'];
$url_slug = $this->lang->line($slug);
$value['url_slug'] = $url_slug;
}
© Stack Overflow or respective owner