webdeveloper123 Posted May 8, 2022 Share Posted May 8, 2022 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? Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/ Share on other sites More sharing options...
Barand Posted May 8, 2022 Share Posted May 8, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/#findComment-1596029 Share on other sites More sharing options...
ginerjm Posted May 8, 2022 Share Posted May 8, 2022 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? Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/#findComment-1596030 Share on other sites More sharing options...
webdeveloper123 Posted May 9, 2022 Author Share Posted May 9, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/#findComment-1596052 Share on other sites More sharing options...
Barand Posted May 9, 2022 Share Posted May 9, 2022 $table[1]['age'] = 33; $table[5]['fname'] = 'Sandra'; $table[3]['lname'] = 'Cunningham'; Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/#findComment-1596056 Share on other sites More sharing options...
ginerjm Posted May 9, 2022 Share Posted May 9, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/#findComment-1596057 Share on other sites More sharing options...
webdeveloper123 Posted May 9, 2022 Author Share Posted May 9, 2022 Hey thanks Barand, I know how to do it that way and have done it that way but was wondering if it can be done using array_replace? @ginerjmYes I was going to move onto mysqli_multi_query and parameterized queries after doing this array stuff Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/#findComment-1596059 Share on other sites More sharing options...
Barand Posted May 9, 2022 Share Posted May 9, 2022 IMHO PHP arrays are a powerfulr feature of the language and one should learn to use them and the associated array functions. Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/#findComment-1596060 Share on other sites More sharing options...
Solution Barand Posted May 9, 2022 Solution Share Posted May 9, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/#findComment-1596064 Share on other sites More sharing options...
webdeveloper123 Posted May 9, 2022 Author Share Posted May 9, 2022 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! Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/#findComment-1596070 Share on other sites More sharing options...
webdeveloper123 Posted May 9, 2022 Author Share Posted May 9, 2022 So your $new array is pretty nifty, you can re-use that, adjust it, for many different array function on 2d arrays Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/#findComment-1596071 Share on other sites More sharing options...
Barand Posted May 9, 2022 Share Posted May 9, 2022 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); Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/#findComment-1596073 Share on other sites More sharing options...
webdeveloper123 Posted May 9, 2022 Author Share Posted May 9, 2022 Yep. Thanks Barand! Quote Link to comment https://forums.phpfreaks.com/topic/314769-using-array_replace-on-a-2-dimensional-array/#findComment-1596074 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.