Prepend to multidimensional associative array

271 views Asked by At

I'm PHP beginner and have a question:

I have a multidimensional associative array:

array(
        "X" => array( "x1" => "1", "x2" => "2", "x3" => "3" ),
        "Y" => array( "y1" => "1", "y2" => "2", "y3" => "3" ),
        "Z" => array( "z1" => "1", "z2" => "2", "z3" => "3" )
)

and need to prepend "" => "" to every element X, Y, Z, so then it will be:

array(
            "X" => array( "" => "", "x1" => "1", "x2" => "2", "x3" => "3" ),
            "Y" => array( "" => "", "y1" => "1", "y2" => "2", "y3" => "3" ),
            "Z" => array( "" => "", "z1" => "1", "z2" => "2", "z3" => "3" )
)

X, Y, Z names are often changed, so I need to get a key name first and then add new value.

I think of using foreach somehow. I'm trying it, but can achieve it.

Thanks

4

There are 4 answers

0
andyron On BEST ANSWER
 <?php
    $temp = array(
       "X" => array( "x1" => "1", "x2" => "2", "x3" => "3" ),
       "Y" => array( "y1" => "1", "y2" => "2", "y3" => "3" ),
       "Z" => array( "z1" => "1", "z2" => "2", "z3" => "3" )
    );
    $you_array = array(); 
    foreach($temp  as $k=>$v){

       $v = array_merge([""=>""],$v); 
       $you_array[$k] = $v;
    }
    print_r($you_array);
 ?>
0
Akshay Hegde On

I think this is what you are looking for

[akshay@localhost tmp]$ cat test.php
<?php
$array=array(
        "X" => array( "x1" => "1", "x2" => "2", "x3" => "3" ),
        "Y" => array( "y1" => "1", "y2" => "2", "y3" => "3" ),
        "Z" => array( "z1" => "1", "z2" => "2", "z3" => "3" )
);

// Input
print_r($array);

// & reference
foreach($array as &$sub_array)
{
    $sub_array = array(""=>"")+$sub_array;
}

// Output
print_r($array);
?>

Output

[akshay@localhost tmp]$ php test.php
Array
(
    [X] => Array
        (
            [x1] => 1
            [x2] => 2
            [x3] => 3
        )

    [Y] => Array
        (
            [y1] => 1
            [y2] => 2
            [y3] => 3
        )

    [Z] => Array
        (
            [z1] => 1
            [z2] => 2
            [z3] => 3
        )

)
Array
(
    [X] => Array
        (
            [] => 
            [x1] => 1
            [x2] => 2
            [x3] => 3
        )

    [Y] => Array
        (
            [] => 
            [y1] => 1
            [y2] => 2
            [y3] => 3
        )

    [Z] => Array
        (
            [] => 
            [z1] => 1
            [z2] => 2
            [z3] => 3
        )

)
0
Rajinder On

check the following code:

<?php
$temp = array(
        "X" => array( "x1" => "1", "x2" => "2", "x3" => "3" ),
        "Y" => array( "y1" => "1", "y2" => "2", "y3" => "3" ),
        "Z" => array( "z1" => "1", "z2" => "2", "z3" => "3" )
);
$you_array = array(); 
foreach($temp  as $k=>$v){

    array_unshift($v, " "); 
    $you_array[$k] = $v;
}
print_r($you_array);
?>

Out put :

Array ( 

[X] => Array ( [0] => [x1] => 1 [x2] => 2 [x3] => 3 ) 

[Y] => Array ( [0] => [y1] => 1 [y2] => 2 [y3] => 3 ) 

[Z] => Array ( [0] => [z1] => 1 [z2] => 2 [z3] => 3 ) 

)
0
André On

You can use array_unshift to add new elements to an array.

As you already mentioned, create a foreach loop and add your new elements to each sub-array:

$new = array("new_key" => "");
foreach ($yourArray as $key => $value)
   array_unshift($yourArray[$key], $new);

Be careful: Your new element needs a key value. An empty key (like you mentioned) is not possible. If you just add a new element without a key array("") it will be added with a numerical key (0 => ""). Existing numerical keys will be modified to start counting from zero.