2

I have a multidimensional array in PHP that appears as:

Array
(
    [0] => Array
    (
        [background] => https://example.com/image.jpg
        [description] => Example text
        [url] => https://example.com
    )

    [1] => Array
    (
        [background] => https://example.com/image.jpg
        [description] => Example text
        [url] => https://example.com
    )
)

I would like to loop through this array and append the same parameter to both url keys. I tried doing this through a function with a double foreach loop and was able to append the parameter successfully, but I'm having trouble returning an array with the updated values.

Here is what I've tried:

Call

$array = append_field($array, 'url', '?parameter=test');

Function

function append_field($array, $field, $parameter)
{
    foreach ($array as $inner_array) :
        foreach ($inner_array as $key => $append) :
            if ($key == $field) :
                $append .= $parameter;
            endif;
        endforeach;
    endforeach;

    return $array;
}

3 Answers 3

3

You need to pass the array values as references in both foreach loops to be able to write to them. Otherwise you are iterating over copies of your values.

Ref : http://php.net/manual/en/language.references.php

function append_field($array, $field, $parameter)
{
    foreach ($array as &$inner_array) :
        foreach ($inner_array as $key => &$append) :
            if ($key == $field) :
                $append .= $parameter;
            endif;
        endforeach;
    endforeach;

    return $array;
}

But you could also do it without references, this time by writing to the full array path including both keys :

function append_field($array, $field, $parameter)
{
    foreach ($array as $i => $inner_array) :
        foreach ($inner_array as $key => $append) :
            if ($key == $field) :
                $array[$i][$key] .= $parameter;
            endif;
        endforeach;
    endforeach;

    return $array;
}
Sign up to request clarification or add additional context in comments.

Comments

3

Just change this line

$append .= $parameter;

to this

$inner_array[$key] = $append.$parameter

and foreach ($array as $inner_array): to foreach ($array as &$inner_array) :

Comments

2

Some more way, to achieve same result, for example using array_map()

[akshay@localhost tmp]$ cat test.php
<?php
$arr = array(
    array(
        'background'=>'https://example.com/image.jpg',
        'description'=>'Example text',
        'url'=>'https://example.com'
    ),
    array(
        'background'=>'https://example.com/image.jpg',
        'description'=>'Example text',
        'url'=>'https://example.com'
    ),

);

$append = array('url'=>'?parameter=test');
print_r( 
    array_map(function($item) use ($append) {foreach($append as $k => $v){ if(isset($item[$k]))$item[$k].=$v;}return $item;}, $arr )
);


?>

Output:

[akshay@localhost tmp]$ php test.php
Array
(
    [0] => Array
        (
            [background] => https://example.com/image.jpg
            [description] => Example text
            [url] => https://example.com?parameter=test
        )

    [1] => Array
        (
            [background] => https://example.com/image.jpg
            [description] => Example text
            [url] => https://example.com?parameter=test
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.