Access newly added key=>value in associative array during loop - php

I am trying to add a key=>value pair to an array while using a foreach loop, when that value is added the foreach loop needs to process the new key=>value pair.
$array = array(
'one' => 1,
'two' => 2,
'three' => 3
);
foreach($array as $key => $value) {
if ($key == 'three') {
$array['four'] = 4;
} else if ($key == 'four') {
$array['five'] = 5;
}
}
If I print the array after the loop, I would expect to see all 5 kv's, but instead I only see this:
Array
(
[one] => 1
[two] => 2
[three] => 3
[four] => 4
)
Is there some way, when I add the fourth pair, to actually process it so the fifth pair gets added within that foreach loop (or another kind of loop?)

According to php documentation,
As foreach relies on the internal array pointer changing it within the loop may lead to unexpected behavior.
You cannot modify the array during foreach. However, an user posted an example of a regular while loop that does what you need: http://www.php.net/manual/en/control-structures.foreach.php#99909
I report it here
<?php
$values = array(1 => 'a', 2 => 'b', 3 => 'c');
while (list($key, $value) = each($values)) {
echo "$key => $value \r\n";
if ($key == 3) {
$values[4] = 'd';
}
if ($key == 4) {
$values[5] = 'e';
}
}
?>
the code above will output:
1 => a
2 => b
3 => c
4 => d
5 => e

That's because PHP will internally use it's own copy of the array pointer. You are iterating trough it's orginal key/values not through the modified array.
As the original array contains the key three, the first if statement will match, but not the second
Another simpler example is the fact, that this is not an infinite loop:
$array = array(1);
foreach($array as $val) {
$array []= $val +1;
}
var_dump($array);
Output:
array(2) {
[0] =>
int(1)
[1] =>
int(2)
}
However, the PHP documentation says not much to that:
As foreach relies on the internal array pointer changing it within the loop may lead to unexpected behavior.

Related

Split flat array into grouped subarrays containing values from consecutive key in the input array

I have an array from array_diff function and it looks like below:
Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
[5] => in
[6] => our
)
As you can see, we have a gap between the keys #3 and #5 (i.e. there is no key #4).
How can I split that array into 2 parts, or maybe more if there are more gaps?
The expected output would be:
Array
(
[0] => Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
)
[1] => Array
(
[0] => in
[1] => our
)
)
You can use old_key,new_key concept to check that there difference is 1 or not? if not then create new index inside you result array otherwise add the values on same index:-
<?php
$arr = Array(0 => 'world',1 => 'is',2 => 'a',3 => 'wonderfull',5 => 'in',6 => 'our');
$new_array = [];
$old_key = -1;
$i = 0;
foreach($arr as $key=>$val){
if(($key-$old_key) ==1){
$new_array[$i][] = $val;
$old_key = $key;
}
if(($key-$old_key) >1){
$i++;
$new_array[$i][] = $val;
$old_key = $key;
}
}
print_r($new_array);
https://3v4l.org/Yl9rp
You can make use of the array internal pointer to traverse the array.
<?php
$arr = Array(0=>"world",1=>"is",2=>"a",3=>"wonderfull",5=>"in",6=>"our");
print_r($arr);
$result = Array();
$lastkey;
while($word = current($arr))
{
$key = key($arr);
if(isset($lastkey) && $key == $lastkey + 1)
{
$result[count($result) - 1][] = $word;
}
else
{
$result[] = Array($word);
}
$lastkey = $key;
next($arr);
}
print_r($result);
?>
This task is a perfect candidate for a reference variable. You unconditionally push values into a designated "bucket" -- in this case a subarray. You only conditionally change where that bucket is in the output array.
There are two important checks to make when determining if a new incremented key should be generated:
if it is not the first iteration and
the current key minus (the previous key + 1) does not equal 0.
Code: (Demo)
$nextKey = null;
$result = [];
foreach ($array as $key => $val) {
if ($nextKey === null || $key !== $nextKey) {
unset($ref);
$result[] = &$ref;
}
$ref[] = $val;
$nextKey = $key + 1;
}
var_export($result);
This solution generates an indexed array starting from zero with my sample input and uses only one if block. In contrast, AliveToDie's solution generates a numerically keyed array starting from 1 and uses two condition blocks containing redundant lines of code.

Edit array placement

I am having a simple issue with ordering an array elements.
Let's assume we have an array like this
[0]=>zero
[1]=>one
[2]=>two
[3]=>three
What i want is a way to move some elements to first position for example move one and two to first positions so i will have:
[1]=>one
[2]=>two
[0]=>zero
[3]=>three
and this should be done without knowing the current position of the element in the array which means it should be done by specifying the name of wanted element to move.
I thought about array_splice() but it won't work since i should specify the key of the element in array.
You could build two arrays (one with the searched values and one without) and merge them. Keep in mind that if you want to keep numeric keys, you should use the + operator instead of array_merge :
<?php
$array = array('zero', 'one', 'two', 'three');
$searched_values = array('one', 'two');
$array_first = $array_second = array();
foreach ($array as $key=>$value) {
if (in_array($value, $searched_values))
$array_first[$key] = $value;
else
$array_second[$key] = $value;
}
$array_end = $array_first + $array_second;
echo '<pre>'; print_r($array_end); echo '</pre>';
Returns :
Array
(
[1] => one
[2] => two
[0] => zero
[3] => three
)
EDIT : Keeping the $searched_values order
In this case, you just need to loop over the serached values in order to build an array of found values and simultaneously delete the initial array's entry :
<?php
$array = array('zero', 'one', 'two', 'three');
$searched_values = array('one', 'three', 'two');
$array_found = array();
foreach ($searched_values as $key=>$value) {
$found_key = array_search($value, $array);
if ($found_key !== false) {
$array_found[$found_key] = $value;
unset($array[$found_key]);
}
}
$array_end = $array_found + $array;
echo '<pre>'; print_r($array_end); echo '</pre>';
?>
Returns :
Array
(
[1] => one
[3] => three
[2] => two
[0] => zero
)
The only way I can think to do this is to remove it then add it:
$key = array_search('one', $array); // $key = 1;
$v = $my_array[$key];
unset($my_array[$key]);
array_unshift($my_array, $v);
Output:
[0] => one
[1] => zero
[2] => two
[3] => three
UPDATE:
If you use array_unshift it will not override your current element.

php create multidimensional array from flat one

I have an array like this:
<?php
$array = array( 0 => 'foo', 1 => 'bar', ..., x => 'foobar' );
?>
What is the fastest way to create a multidimensional array out of this, where every value is another level?
So I get:
array (size=1)
'foo' =>
array (size=1)
'bar' =>
...
array (size=1)
'x' =>
array (size=1)
0 => string 'foobar' (length=6)
<?php
$i = count($array)-1;
$lasta = array($array[$i]);
$i--;
while ($i>=0)
{
$a = array();
$a[$array[$i]] = $lasta;
$lasta = $a;
$i--;
}
?>
$a is the output.
What exactly are you trying to do? So many arrays of size 1 seems a bit silly.
you probably want to use foreach loop(s) with a key=>value pair
foreach ($array as $k=>$v) {
print "key: $k value: $v";
}
You could do something like this to achieve the array you asked for:
$newArray = array();
for ($i=count($array)-1; $i>=0; $i--) {
$newArray = array($newArray[$i]=>$newArray);
}
I'm confused about what you want to do with non-numeric keys (ie, x in your example). But in any case using array references will help
$array = array( 0 => 'foo', 1 => 'bar', x => 'foobar' );
$out = array();
$curr = &$out;
foreach ($array as $key => $value) {
$curr[$value] = array();
$curr = &$curr[$value];
}
print( "In: \n" );
print_r($array);
print( "Out : \n" );
print_r($out);
Prints out
In:
Array
(
[0] => foo
[1] => bar
[x] => foobar
)
Out :
Array
(
[foo] => Array
(
[bar] => Array
(
[foobar] => Array
(
)
)
)
)
You can use a recursive function so that you're not iterating through the array each step. Here's such a function I wrote.
function expand_arr($arr)
{
if (empty($arr))
return array();
return array(
array_shift($arr) => expand_arr($arr)
);
}
Your question is a little unclear since in your initial statement you're using the next value in the array as the next step down's key and then at the end of your example you're using the original key as the only key in the next step's key.

Trying to loop through array and reassign value

I have two arrays, both with the same indexes. What I want to do is loop through one of the arrays (portConfigArray), and change the value of an existing item by using data from a second array. (portStatusArray)
Here's the logic:
$i=0;
foreach ($portConfigArray as $configentry)
{
$configentry['LinkState'] = $portStatusArray[$i]['LinkState'];
$i = $i + 1;
echo $configentry['LinkState'];
}
$portdetailsArray = $portConfigArray;
var_dump($portdetailsArray);
the echo statement shows the correct values being assigned for each item in the portConfigArray. (its just a string value like "Up" or "Down")
But in the var_dump I can see that the value hasn't been updated correctly. It shows
["LinkState"]=> string(0) ""
as the output for each record.
Can you tell me what I'm doing wrong?
You need to make $configentry a reference, otherwise it's just a copy
foreach ($portConfigArray as &$configentry)
foreach ($portConfigArray as $configentry)
Should be
foreach ($portConfigArray as &$configentry)
Essentially this means the loop deals with the actual value rather than a copy of it.
While you can make $configentry a reference, as stated in other answers, it CAN cause major issues if you reuse such "referenced" variables later on in the script for other purposes. A safer method is to use the key=>val version of foreach, and directly modify the array:
foreach($portConfigArray as $key => $configentry) {
$portConfigArray[$key] = 'newvalue';
}
The reference version can cause issues, e.g.
php > $a = array('a', 'b', 'c');
php > foreach($a as &$val) { $val++; };
php > print_r($a);
Array
(
[0] => b
[1] => c
[2] => d
)
php > $b = array('p', 'q', 'r');
php > foreach($b as $val) { $val++; }; <--note, $val, not &$val
php > print_r($b);
Array
(
[0] => p <---hey, these 3 didn't change!
[1] => q
[2] => r
)
php > print_r($a);
Array
(
[0] => b
[1] => c
[2] => s <---uh-oh!
)
php >

How to remove values from an array whilst renumbering numeric keys

I have an array which may contain numeric or associative keys, or both:
$x = array('a', 'b', 'c', 'foo' => 'bar', 'd', 'e');
print_r($x);
/*(
[0] => a
[1] => b
[2] => c
[foo] => bar
[3] => d
[4] => e
)*/
I want to be able to remove an item from the array, renumbering the non-associative keys to keep them sequential:
$x = remove($x, "c");
print_r($x);
/* desired output:
(
[0] => a
[1] => b
[foo] => bar
[2] => d
[3] => e
)*/
Finding the right element to remove is no issue, it's the keys that are the problem. unset doesn't renumber the keys, and array_splice works on an offset, rather than a key (ie: take $x from the first example, array_splice($x, 3, 1) would remove the "bar" element rather than the "d" element).
This should re-index the array while preserving string keys:
$x = array_merge($x);
You can fixet with next ELEGANT solution:
For example:
<?php
$array = array (
1 => 'A',
2 => 'B',
3 => 'C'
);
unset($array[2]);
/* $array is now:
Array (
1 => 'A',
3 => 'C'
);
As you can see, the index '2' is missing from the array.
*/
// SOLUTION:
$array = array_values($array);
/* $array is now:
Array (
0 => 'A',
1 => 'C'
);
As you can see, the index begins from zero.
*/
?>
I've come up with this - though I'm not sure if it's the best:
// given: $arr is the array
// $item is the item to remove
$key = array_search($item, $arr); // the key we need to remove
$arrKeys = array_keys($arr);
$keyPos = array_search($key, $arrKeys); // the offset of the item in the array
unset($arr[$key]);
array_splice($arrKeys, $keyPos, 1);
for ($i = $keyPos; $i < count($arrKeys); ++$i) {
if (is_int($arrKeys[$i])) --$arrKeys[$i]; // shift numeric keys back one
}
$arr = array_combine($arrKeys, $arr); // recombine the keys and values.
There's a few things I've left out, just for the sake of brevity. For example, you'd check if the array is associative, and also if the key you're removing is a string or not before using the above code.
Try array_diff() it may not order the new array correctly though
if not the following should work
You will need to iterate over it in the remove function.
function remove($x,$r){
$c = 0;
$a = array();
foreach ($x as $k=>$v){
if ($v != $r) {
if (is_int($k)) {
$a[$c] = $v;
$c++;
}
else {
$a[$k] = $v;
}
}
}
return $a;
}
DC
I don't think there is an elegant solution to this problem, you probably need to loop to the array and reorder the keys by yourself.

Categories