0

I have an array as shown below.

array (size=2)
    '1S1' =>
      array (size=8)
        'order_id' => int 0
        'item_id' => int 1
        'special_desc' => string 'Special XXX' (length=11)
        'qty' => int 2
        'price' => int 50
        'amount' => int 0
        'created_at' => int 1376580193
        'updated_at' => int 1376580193
    '1S2' =>
      array (size=8)
        'order_id' => int 0
        'item_id' => int 2
        'special_desc' => string 'Special YYY' (length=11)
        'qty' => int 3
        'price' => int 150
        'amount' => int 0
        'created_at' => int 1376580193
        'updated_at' => int 1376580193

If I wanted to replace "order_id" of both elements of this array to a new value before saving to the database, what array function or technique can I use?

Thanks.

4
  • Is the array dynamic in which, you wouldn't know the keys to the first level? Commented Aug 15, 2013 at 16:15
  • 1
    What's wrong with looping through your array and modifing the variables directly? Commented Aug 15, 2013 at 16:17
  • 1
    just loop through your array eg. with foreach and modify your value Commented Aug 15, 2013 at 16:21
  • Sorry, I thought image is easy to read, I didn't know people copies the code somewhere to help fixing my problem. Thanks Cris for image to text conversion. Commented Aug 15, 2013 at 18:12

2 Answers 2

2

Take a look at array_walk:

$array = array_walk( $array, function( $subArray ) {
    $subArray[ 'order_id' ] = 'someNewValue';
} );

A simple foreach could work too.

// Noting the '&' by reference call
foreach( $array as &$subArray ) {
    $subArray[ 'order_id' ] = 'someNewValue';
}
// Or
foreach( $array as $key => $value ) {
   $array[ $key  ][ 'order_id' ] = 'someNewValue';
}
Sign up to request clarification or add additional context in comments.

6 Comments

The correct function to use in this situation is array_walk, not array_map (your function doesn’t return anything).
I use for each but the values did not change. $cartItems = array_values($cartItems); foreach($cartItems as $cartItem) { $cartItem['order_id'] = 50; } var_dump($cartItems);
@Kongthap you need to specify that you are editing it by reference. Thats what the & is for in front of my $subArray is. Change your foreach to foreach( $cartItems as &$cartItem )
@Kongthap I added another way to do a foreach for you, that doesnt use by reference
Using $array['1S1']['order_id'] = $new_value is working, but I have to loop through the whole array at runtime.
|
0
$array_name['1S1']['order_id'] = some value;
$array_name['1S2']['order_id'] = some value;

3 Comments

This is why I specificly asked the OP if the first level array keys were dynamic in which case this won't work :p You would have to loop
Sorry, looping through the whole array is better for my case, but I still cannot get value changed using foreach.
Your answer is working for changing to the new value but I don't understand why foreach ($entireArray as $eachArray) { $eachArray['order_id'] = $new_value; } is not working...

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.