0

Hello I am stuck modifying a value in a PHP array.

echo '<pre>';
print_r($data);
echo '</pre>';

I have to change this value :

enter image description here

I dont have to use to the keys : 900 and 2200 cause the values can change... How can I process to replace the first value "1578" by another in my $data array ?

Thanks a lot :)

2
  • do you have to replace the first value "1578" in just [900] or also in the first value "1578" in [1250], [1500], [1750] Commented Jun 17, 2021 at 11:53
  • Thanks for your answer. I need to change the first value of 900 without using 900 as a key Commented Jun 17, 2021 at 12:07

4 Answers 4

1

You have to use nested loop.

foreach($data as $key => $value) {
  foreach( $value as $k => $v ) {
    if( $k === 0 ) {
        $data[$key][$k] = 'some value here';
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

this is exactly what I was looking for, thank you
0

You would change everytime the first value of your array or just the first "1578" of your array ?

If is the first case you can do : $data[900][array_key_first($data[900])] = $newValue;

Else you can search the key of the first 1578 and change it :

$key = array_search('1578', $data[900]);
$data[900][$key] = $newValue;

And if you want to change all the first 1578 in all your sub-array, loop on it

1 Comment

Thanks for your answer, as I said I need to change the first value of 900 but I cant use 900 as a key. by the way 1578 is not always the same value as well it can change too. I dont know if Im clear or not^^
0

make a loop on your array like:

foreach ($data as $key => $arr)
{
    $firstKey = array_search(1578, $arr);
    # replace
    if ($firstKey !== false)
        $data[$key][$firstKey] = "my new value";
}

2 Comments

thanks, it might work but in my case I forgot to specify that 1578 is not a fixed value. it can change from one array to another
do you have a map like if the key is 900 then the value is 1578, etc... then use this map
0

You can use double foreach loop and than break immediately after you find the first value like this:

foreach($data as $key1 => $value1){
    foreach($data[$key1] as $key2 => $value2){
        $data[$key1][$key2] = $your_new_value;
        break 2;
    }
}

The 2 after the break statement means that you break out of both loops at the same time. Alternatively you can use array_keys which returns the keys of an array like this.

$key1 = array_keys($data)[0];
$key2 = array_keys($data[$key1])[0];
$data[$key1][$key2] = $your_new_value;

array_keys returns an array so the [0] at the end of the line means the first element of that array, because the first element of that array is usually the first key.

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.