Jump to content

using array_replace on a 2 dimensional array


Go to solution Solved by Barand,

Recommended Posts

Hi,

I have these 2 arrays which are merged:

$table = array ( 
            0 => array ( 
                'fname' => 'Peter', 
                'lname' => 'Smith', 
                'age' => '37'
                ), 
            1 => array ( 
                'fname' => 'Paul', 
                'lname' => 'Hartley', 
                'age' => '48'
                ), 
            2 => array ( 
                'fname' => 'Mary', 
                'lname' => 'Baker', 
                'age' => '42'
                ), 
            3 => array ( 
                'fname' => 'Jane', 
                'lname' => 'Doe', 
                'age' => '51'
              
                )
            );


$newdata = array ( 
            4 => array ( 
                'fname' => 'Jon', 
                'lname' => 'Atkins', 
                'age' => '27'
                ), 
            5 => array ( 
                'fname' => 'Phil', 
                'lname' => 'Jones', 
                'age' => '14'
                ), 
            6 => array ( 
                'fname' => 'Frank', 
                'lname' => 'Lampard', 
                'age' => '48'
                ), 
            7 => array ( 
                'fname' => 'Toney', 
                'lname' => 'Brentford', 
                'age' => '25'
              
                )
            );

$table = array_merge($table, $newdata);

I am trying to use array_replace to edit values in the array. I know I can do it like this:

$table[2]['age'] = 67;

But I wanted to use array_replace. So I did this:

$replacements = array(3 => "Steve", 7 => "Adam");

$newArray = array_replace($table, $replacements);



print_r($newArray);

But all I did was enter Steve at position 3 and Adam at position 7. Here is the output from print_r:

Array ( [0] => Array ( [fname] => Chris [lname] => Smith [age] => 37 ) [1] => Array ( [fname] => Paul [lname] => Hartley [age] => 48 ) [2] => Array ( [fname] => Mary [lname] => Baker [age] => 67 ) [3] => Steve [4] => Array ( [fname] => Jon [lname] => Atkins [age] => 27 ) [5] => Array ( [fname] => Phil [lname] => Jones [age] => 14 ) [6] => Array ( [fname] => Tank [lname] => Lampard [age] => 48 ) [7] => Adam )

I also tried:

$array2 = array("fname" => "Steve");

$array3 = array("age" => "72");

$replacedArray = array_replace($table, $array2,$array3);

print_r($replacedArray);

But all that did was add [fname] Steve and [age] 72 at the end of the array. Here is the print_r output

Array ( [0] => Array ( [fname] => Chris [lname] => Smith [age] => 37 ) [1] => Array ( [fname] => Paul [lname] => Hartley [age] => 48 ) [2] => Array ( [fname] => Mary [lname] => Baker [age] => 67 ) [3] => Array ( [fname] => Jane [lname] => Doe [age] => 51 ) [4] => Array ( [fname] => Jon [lname] => Atkins [age] => 27 ) [5] => Array ( [fname] => Phil [lname] => Jones [age] => 14 ) [6] => Array ( [fname] => Tank [lname] => Lampard [age] => 48 ) [7] => Array ( [fname] => Toney [lname] => Parker [age] => 25 ) [fname] => Steve [age] => 72 )

I even wrote a little foreach loop which wasn't very good ( I know it's not very logical)  but I thought I'd give it a try as I'd been trying all day:

$table[0]['fname'] = "Chris";


$table[2]['age'] = 67;


foreach ($table as $key => $value){
    $age[] = $table[2]['age'];
    $fname[] = $table[0]['fname'];

    $age[] = 58;
    $fname[] = "Brandon";


    
}
$replacedArray = array_replace($table, $age,$fname);
print_r($replacedArray);

This is the output from print_r($replacedArray);

Array ( [0] => Chris [1] => Brandon [2] => Chris [3] => Brandon [4] => Chris [5] => Brandon [6] => Chris [7] => Brandon )

Can  anyone help please?

Link to comment
Share on other sites

3 minutes ago, webdeveloper123 said:

This is the output from print_r($replacedArray);

Array ( [0] => Chris [1] => Brandon [2] => Chris [3] => Brandon [4] => Chris [5] => Brandon [6] => Chris [7] => Brandon )

Can  anyone help please?

Can anyone help with what? You've shown us a lot of things weren't what you wanted (even they they do what the manual says they do).

How about telling what result you do want.

Link to comment
Share on other sites

I have to ask this, since you are apparently heavily involved in using arrays and don't understand how to work with them.  Is all the data you are manipulating stored in a database?  And if that is true, why do you even need these arrays when you have a permanent store of your data that a simple query will produce whatever output you want?

Link to comment
Share on other sites

Oh yeah. Basically I want to edit values in the array. So for example If I have the array in my first post

I would like to Edit index 1 =>age to 33

5 => fname = Sandra

3 => lname = Cunningham

@ginerjm No I don't have any of this data in a db. I wanted to brush up on my array skills. It's just that if they basic 1 dimensional arrays then I'd be fine but 2d arrays are a bit trickier.  

Link to comment
Share on other sites

In the long run I would think that brushing up on you db design skills and your mysql writing skills would be a much better path.  Playing with data (which is what you are saying) in arrays gets you nowhere since you can't save the results.  Updating a db gives you lasting results.

Link to comment
Share on other sites

  • Solution
58 minutes ago, webdeveloper123 said:

was wondering if it can be done using array_replace? 

With similar...

$new = [   1 => ['age' => 33],
           5 => ['fname' => 'Sandra'],
           3 => ['lname' => 'Cunningham'] 
       ];
$table = array_replace_recursive($table, $new);
Original $table                                            $new                                                    resulting $table

image.thumb.png.baf6f7060152e080dc8a740821ab0d4e.png

Link to comment
Share on other sites

ahhh that's how you do it. I was using array_replace_recursive for a few hours yesterday but did not get my expected output. So you created a new array with the data you want to change then pass that to the function. I was trying this:

$array2 = array( 4 => ["fname"] => "Steve");

$array3 = array( 7 => ["age"] => "72");

But it had errors.

Thanks for the help Barand!

Link to comment
Share on other sites

19 minutes ago, webdeveloper123 said:

I was trying this:

$array2 = array( 4 => ["fname"] => "Steve");

$array3 = array( 7 => ["age"] => "72");

But it had errors.

 

You were close but your arrays were incorrectly defined. This works too

$table = array_merge($table, $newdata);

$array1 = array( 1 => ['age' => 33] );
$array5 = array( 5 => ['fname' => 'Sandra'] );
$array3 = array( 3 => ['lname' => 'Cunningham'] );


$table2 = array_replace_recursive($table, $array1, $array5, $array3);

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.