Ok, I have an array like so:
$myArray[32]['value'] = 'value1';
$myArray[32]['type'] = 'type1';
$myArray[33]['value'] = 'value2';
$myArray[33]['type'] = 'type2';
$myArray[35]['value'] = 'value3';
$myArray[42]['value'] = 'value4';
$myArray[42]['type'] = 'type4';
Ok, looking for a quick way to change all numbers in the first key 32, 33, 35, and 42 into 0, 1, 2, and 3 instead. But I need to preserve the 2nd key and all of the values. The array is already ordered correctly, since I ordered it using a ksort, but now I need to reset the array from 0 - count($myArray) - 1 and keep the 2nd key intact and its value as well.
Can someone please help me?
$myArray = array_values($myArray);
There might be simpler solutions but here is one working solution:
$myArray = array();
$myArray[32]['value'] = 'value1';
$myArray[32]['type'] = 'type1';
$myArray[33]['value'] = 'value2';
$myArray[33]['type'] = 'type2';
$myArray[35]['value'] = 'value3';
$myArray[42]['value'] = 'value4';
$myArray[42]['type'] = 'type4';
$map = array_flip(array_keys($myArray)); // map old keys to new keys.
$newarray = array();
foreach($myArray as $key => $value) {
$newarray[$map[$key]] = $value; // use new key and old value.
}
You don't need it. Why not to just leave this array alone? Unnecessary moves would lead your code to a mess.
Related
I have multiple arrays that contain multiple elememts, for example:
$array1 = (1,2)
$array2 = (3,4)
I need to generate all possible combinations for elements of arrays. For given example, the output should be:
1_3, 1_4, 2_3, 2_4
However, the issue is that both number of arrays and amount of elements in each one can be different. So we can also take something like that:
$array1 = (1,2,3,4)
$array2 = (5,6,7)
$array3 = (8,9,10,11,12)
and output should look like that:
1_5_8, 1_5_9, 1_5_10 etc. until 4_7_12.
How do I implement something like that? I know that I should use foreach loop, but I have no idea what to do if amount of "foreaches" can be different every time I execute algorithm.
Any help is really appreciated :)
<?php
$array1 = [1,2,3,4];
$array2 = [5,6,7];
$array3 = [8,9,10,11,12];
$collection = [$array1,$array2,$array3];
$result = $collection[0];
array_shift($collection);
foreach($collection as $each_array){
$temp = [];
foreach($result as $each_sub_result){
foreach($each_array as $each_item){
$temp[] = $each_sub_result."_".$each_item;
}
}
$result = $temp;
}
print_r($result);
Algorithm:
We collect all arrays in our $collection variable.
Now, we loop over all elements of our $collection variable where each individual item is an array.
I have done an array_shift() since we are assigning first element of $collection to $result(so we don't want to iterate over them again).
We maintain $temp to store temporary results. Note that we use $result also to store temporary results, so that we can iterate over it and get new temporary results. By temporary, I mean building the final array. For example: It starts with 1, then 1_5 and then finally 1_5_8.
After the looping completes, we will have our final answer in $result.
try something like this:
$array1 = array(1,2,3);
$array2 = array(4,5,6);
foreach ($array1 as $a1){
foreach ($array2 as $a2){
echo $a1 . '_' . $a2 . ', ';
}
}
You can expand it like you wish.
I'm good in JS so that's why I did the algo in JS, you can try to do this in PHP,,no much difficult. You just have to pass in all the array is the function and the rest should be done by itself. Let me know if any confusions.
var arr1 = [1, 2, 3];
var arr2 = [4, 5];
var arr3 = [6, 7, 8];
var numberOfArrays = -1;
function processArrays(concatWith = [], allArrays) {
const duplicateArray = [...allArrays];
for (var i = 0; i < allArrays.length; i++) {
const currentArray = duplicateArray.splice(0, 1);
currentArray[0].forEach(function (value) {
concatWith.push(value);
processArrays(concatWith, duplicateArray);
if (concatWith.length === numberOfArrays) {
console.log(concatWith.join('_'));
}
concatWith.pop();
});
}
}
function makeCombinations(arrayOfAllArrays = []) {
numberOfArrays = arrayOfAllArrays.length;
processArrays([], arrayOfAllArrays);
}
makeCombinations([arr1, arr2, arr3]);
Hope my title explains it ok! Here's more detail:
I'm creating an array which stores keys & their values. Eg.
test1 = hello
test2 = world
test3 = foo
What is the cleanest way of working out what to call the next key? Let's say I will know the first part is 'test', but I don't know what the highest value number is. Obviously in this case I want it to be called 'test4'.
In the example below I want the next key to be 'test46', as it is the next highest value:
test6 = blah
test45 = boo
test23 = far
This sounds like you should be using an array with numerical indexes instead.
You could however use some code like this...
$arr = array('test6', 'test45', 'test23');
$max = 0;
foreach($arr as $value) {
$number = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
$max = max($max, $number);
}
$newKey = 'test' . ++$max; // string(6) "test46"
CodePad.
Implementation of #alex answer without using a loop:
$arr = array('test6', 'test45', 'test23');
$max = max(filter_var_array($arr, FILTER_SANITIZE_NUMBER_INT));
$newKey = 'test' . ++$max; // string(6) "test46"
CodePad
This data structure would be better stored as an array.
$test = array();
$test[] = 'hello';
$test[] = 'world';
$test[] = 'foo';
You then don't need to know the highest number to add a new item, just use the empty brackets syntax (shown above) to add an item to the end of the array.
You then have access to a wealth of array functions that PHP gives you to work with your data: http://php.net/manual/en/ref.array.php
When you want to get item 43 from the array, use:
echo $test[42];
Arrays are counted from 0 rather than 1, so item 43 will have an index of 42.
What are you using that for? If numbering the array is a must-have, just use a simple numerical indexed array instead, and simply prepend the key with "test" if you need it to show up as "test1":
<?php
$array = array(
6 => 'blah',
45 => 'boo',
23 => 'bar'
);
$array[] = 'new';
echo $array[46] . "\n"; // this is 'new'
foreach( $array as $key => $value ) {
echo "test$key = $value<br />\n"; // test6 = blah
}
So here's the input:
$in['a--b--c--d'] = 'value';
And the desired output:
$out['a']['b']['c']['d'] = 'value';
Any ideas? I've tried the following code without any luck...
$in['a--b--c--d'] = 'value';
// $str = "a']['b']['c']['d";
$str = implode("']['", explode('--', key($in)));
${"out['$str']"} = 'value';
This seems like a prime candidate for recursion.
The basic approach goes something like:
create an array of keys
create an array for each key
when there are no more keys, return the value (instead of an array)
The recursion below does precisely this, during each call a new array is created, the first key in the list is assigned as the key for a new value. During the next step, if there are keys left, the procedure repeats, but when no keys are left, we simply return the value.
$keys = explode('--', key($in));
function arr_to_keys($keys, $val){
if(count($keys) == 0){
return $val;
}
return array($keys[0] => arr_to_keys(array_slice($keys,1), $val));
}
$out = arr_to_keys($keys, $in[key($in)]);
For your example the code above would evaluate as something equivalent to this (but will work for the general case of any number of -- separated items):
$out = array($keys[0] => array($keys[1] => array($keys[2] => array($keys[3] => 'value'))));
Or in more definitive terms it constructs the following:
$out = array('a' => array('b' => array('c' => array('d' => 'value'))));
Which allows you to access each sub-array through the indexes you wanted.
$temp = &$out = array();
$keys = explode('--', 'a--b--c--d');
foreach ($keys as $key) {
$temp[$key] = array();
$temp = &$temp[$key];
}
$temp = 'value';
echo $out['a']['b']['c']['d']; // this will print 'value'
In the code above I create an array for each key and use $temp to reference the last created array. When I run out of keys, I replace the last array with the actual value.
Note that $temp is a REFERENCE to the last created, most nested array.
I've got two arrays, one with IDs and one with Names:
$ids = array(4, 13, 6, 8, 10);
$names = array('alice', 'bob', 'charles', 'david', 'elizabeth');
I need to update the db so that the rows with the ids have the names in the array. Here's the tricky bit: I also have two ints:
$special_name = 2; // the index in $names, in this case we mean 'charles'
$special_id = 13; // the id value
I don't care about which name goes to which id, except that the name with the $special_name should go on the $special_id.
What's the most elegant way to get there? All of the methods that I'm thinking of seem pretty messy. The best I've thought of is to extract out the special items from each array, and do those first, and then do the rest, perhaps building a new array like this:
$mapped = new array();
$mapped[$special_id] = $names[$special_name];
foreach ($ids as $id) {
if ($id != $special_id) {
$mapped[$id] = current($names);
}
// advance $names pointer
$next_name = next($names);
if ($next_name == $special_name) next($names);
}
I haven't tested that yet (I'm about to) but it's meant to produce something like:
$mapped = array(13=>'charles', 4=>'alice',6=>'bob', 8=>'david', 10=>'elizabeth');
and then running through that to do the actual update. Got a better idea?
UPDATE: added the possible solution above. Meanwhile a couple answers have come in.
If it wasn't for the special Ids, you could have just array_combine'd the two arrays. Here is how I think to have solved the issue:
Setup
$ids = array(4, 13, 6, 8, 10);
$names = array('alice', 'bob', 'charles', 'david', 'elizabeth');
$specialNameIndex = 2;
$specialId = 13;
Solution
$result = array($specialId => $names[$specialNameIndex]);
unset($ids[array_search($specialId, $ids)],
$names[$specialNameIndex]);
$result += array_combine($ids, $names);
Result
print_r($result);
Array
(
[13] => charles
[4] => alice
[6] => bob
[8] => david
[10] => elizabeth
)
you can use array_combine and then set/append your special values:
$mapped = array_combine($ids, $names);
$mapped[$special_id] = $names[$special_name];
Are the $ids and $names arrays synced? (Does 4 correspond to 'alice'?)
for ($i=0; $i < count($ids); $i++) {
$indexed[$ids[$i]] = $names[$i]; // $indexed[4] = 'alice';
$indexed2[] = array ( $ids[$i] => $names[$i] ); // $indexed[0] = ( 4 => 'alice')
}
Pick your fave
Since you use the default indexes you can use foreach() on keys($ids) to get the indexes so that you can iterate through both arrays at once. Just compare the value of the current index of $ids and use the alternate index of $names when appropriate.
$arr[] = $new_item;
Is it possible to get the newly pushed item programmatically?
Note that it's not necessary count($arr)-1:
$arr[1]=2;
$arr[] = $new_item;
In the above case,it's 2
end() do the job , to return the value ,
if its help to you ,
you can use key() after to petch the key.
after i wrote the answer , i see function in this link :
http://www.php.net/manual/en/function.end.php
function endKey($array){
end($array);
return key($array);
}
max(array_keys($array)) should do the trick
The safest way of doing it is:
$newKey = array_push($array, $newItem) - 1;
You can try:
max(array_keys($array,$new_item))
array_keys($array,$new_item) will return all the keys associated with value $new_item, as an array.
Of all these keys we are interested in the one that got added last and will have the max value.
You could use a variable to keep track of the number of items in an array:
$i = 0;
$foo = array();
$foo[++$i] = "hello";
$foo[++$i] = "world";
echo "Elements in array: $i" . PHP_EOL;
echo var_dump($foo);
if it's newly created, you should probably keep a reference to the element. :)
You could use array_reverse, like this:
$arr[] = $new_item;
...
$temp = array_reverse($arr);
$new_item = $temp[0];
Or you could do this:
$arr[] = $new_item;
...
$new_item = array_pop($arr);
$arr[] = $new_item;
If you are using the array as a stack, which it seems like you are, you should avoid mixing in associative keys. This includes setting $arr[$n] where $n > count($arr). Stick to using array_* functions for manipulation, and if you must use indexes only do so if 0 < $n < count($arr). That way, indexes should stay ordered and sequential, and then you can rely on $arr[count($arr)-1] to be correct (if it's not, you have a logic error).