PHP: put array entries in new order - php

I try to take the following array:
Array
(
[0] => Array
(
[1] => 12
[2] => 21
[3] => 33
)
[1] => Array
(
[1] => 5
[2] => 5
[3] => 4
)
[2] => Array
(
[1] => 1
[2] => 10
[3] => 11
)
[3] => Array
(
[3] => 2
)
)
and create a new array with it, taking the first entries of the source array. So the first array from this one would look like this:
Array
(
[0] => 12
[1] => 5
[2] => 1
[3] => 2
)
And the second one obviously like this:
Array
(
[0] => 21
[1] => 5
[2] => 10
)
I tried around with 2 for loops, it somewhat works with
for ($i = 1; $i < count($month_array[$i]) + 1; $i++)
{
unset($temp_array);
for ($i2 = 0; $i2 < count($month_array); $i2++)
{
if (isset($month_array[$i2][$i]))
{
$temp_array[] = $month_array[$i2][$i];
}
}
}
But it leaves out some of the elements and if the source array is not complete, meaning only the 3rd array has a value for key 3, it also fails.
Any help ? Many thanks !

Try it like this:
$org_array = array(array(1 => 12,2 => 21,3 => 33),array(1 => 5,2 => 5,3 => 4),array(1 => 1,2 => 10,3 => 11),array(3 => 2),); // your original array
$new_array = array(); // the output array
foreach($org_array as $sub_org_value) { // for each sub array of your original array do this
foreach($sub_org_value as $key => $value) { // for each entry in a sub array do this
$new_array[$key][] = $value; // add the entry into the new_array in the right sub array
}
}
print_r($new_array[1]); // yields 12,5,1
print_r($new_array[2]); // yields 21,5,10
print_r($new_array[3]); // yields 33,4,11,2
Afterwards you have an array $new_array that contains three arrays. The first sub array has all 1 values, the second sub array has all 2 values and the third sub array has all 3 values.

Related

Combine multiple arrays

I have arrays like this
Array 1:
Array
(
[0] => Array
(
[0] => 0
[1] => -0.025
)
[1] => Array
(
[0] => 0
[1] => -0.025
)
[2] => Array
(
[0] => 0
[1] => -0.025
)
)
Array 2:
Array
(
[0] => Array
(
[0] => 0
[1] => -0.025
)
[1] => Array
(
[0] => 0
[1] => -0.025
)
)
Array 3:
Array
(
[0] => Array
(
[0] => 0
[1] => -0.025
)
[1] => Array
(
[0] => 0
[1] => -0.025
)
[2] => Array
(
[0] => 0
[1] => -0.025
)
[3] => Array
(
[0] => 0
[1] => -0.025
)
)
and more of them.
I want to combine them to this
Array
(
[0] => Array
(
[0] => 0
[1] => -0.025
[2] => 0
[3] => -0.025
[4] => 0
[5] => -0.025
)
[1] => Array
(
[0] => 0
[1] => -0.025
[2] => 0
[3] => -0.025
[4] => 0
[5] => -0.025
)
[2] => Array
(
[0] => 0
[1] => -0.025
[2] => 0
[3] => 0
[4] => 0
[5] => -0.025
)
[3] => Array
(
[0] => 0
[1] => 0
[2] => 0 // These (0-3) are 0 because the other two arrays haven't 3 in the first level
[3] => 0
[4] => 0
[5] => -0.025
)
)
The arrays have a different number of entries in the first level. In the second level there are always 2 entries.
In the second level of the combined array the first two keys (0 and 1) should always have the entries from the first array. The second two keys (2 and 3) should always have the entries from the second array and so on. In my example there are 30 arrays that I want to combine.
If an array does have more first level entries than others all entries should have 0 as value.
I hope you understand this :)
I built it ;-/
Working demonstration at eval.in
Requirements:
Basically it is transposing rows into columns.
the output columns all have the length of the longest array.
The source arrays can have different lengths. Any empty entries are assumed to have a value of array(0, 0).
The output columns are converted into a single dimensional array. i.e. source value arrays are appended to the column array.
Explanation:
I decided to use the internal iterator that all arrays have. i.e. the arrays already can record there own state such as current position (row) that they are on.
After that it is just 'housekeeping':
keep a list of which arrays still have entries to be processed. When none of them have then we are finished.
record which output column ($mergedEntryNo) that we are creating.
scan along each (row) reading entries for the current row.
after each row: advance to the next row and record which arrays are active - if any.
The class (SimpleMerge) that does the work
class SimpleMerge {
protected $sources = array(); // a list of arrays!
protected $sourceCount = 0; // useful
protected $isActive = array(); // which of them have entries to process.
public $merged = array(); // output in here and public
protected $anyActiveSources = false;
// need a list of arrays
public function __construct(array $allSources)
{
$this->sources = $allSources;
$this->sourceCount = count($allSources);
$this->isActive = array_fill(0, $this->sourceCount, true);
}
// generate the output by scanning the arrays line by line
public function generateOutput()
{
$this->generateInit();
$mergedEntryNo = 0;
while ($this->anyActiveSources) {
// set the next output entries
for ($sourceNo = 0; $sourceNo < $this->sourceCount; $sourceNo++) {
$this->addEntry($mergedEntryNo, $this->getEntry($sourceNo));
}
$mergedEntryNo++;
$this->nextPassAdvance();
$this->setIsActiveSource();
}
return $this->merged;
}
// ensure everything is initialized correctly
public function generateInit()
{
$this->merged = array();
foreach ($this->sources as &$source) {
reset($source); // force internal iterators to the start
$this->merged[] = array(); // empty arrays in the output
}
unset($source);
$this->setIsActiveSource();
}
// add to output
public function addEntry($mergedNo, array $values)
{
foreach ($values as $value) {
$this->merged[$mergedNo][] = $value;
}
}
// get the current source entry - will be array of zeroes if end of array
public function getEntry($sourceNo)
{
if ($this->isActive[$sourceNo]) {
return current($this->sources[$sourceNo]);
}
else {
return array(0, 0);
}
}
// check and set which ones are still active and also indicate if any are active
public function setIsActiveSource()
{
$activeCount = 0;
for ($sourceNo = 0; $sourceNo < $this->sourceCount; $sourceNo++) {
$isActive = current($this->sources[$sourceNo]) !== false;
$this->isActive[$sourceNo] = $isActive;
$activeCount = $activeCount + ($isActive ? 1 : 0);
}
$this->anyActiveSources = $activeCount > 0;
}
// advance iterators on the sources that are still active
public function nextPassAdvance()
{
for ($sourceNo = 0; $sourceNo < $this->sourceCount; $sourceNo++) {
if ($this->isActive[$sourceNo]) { // was last time
next($this->sources[$sourceNo]);
}
}
}
}
Run it
// create and run the generator...
$mergeAll = new SimpleMerge($allSourcesList);
$merged = $mergeAll->generateOutput();
Output:
Note, there are no zero values in the input. The values indicate source table and entry number.
Outpt: Array
(
[0] => Array
(
[0] => 11
[1] => -11.025
[2] => 21
[3] => -21.025
[4] => 31
[5] => -31.025
[6] => 41
[7] => -41.025
)
[1] => Array
(
[0] => 12
[1] => -12.025
[2] => 22
[3] => -22.025
[4] => 32
[5] => -32.025
[6] => 42
[7] => -42.025
)
[2] => Array
(
[0] => 13
[1] => -13.025
[2] => 0
[3] => 0
[4] => 33
[5] => -33.025
[6] => 43
[7] => -43.025
)
[3] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 34
[5] => -34.025
[6] => 44
[7] => -44.025
)
)

how to check multidimensional array for duplicated value

i have this array and i don't want to remove duplicated values..
i want to check if there are duplicates in the first value or not
( [0] => 1500,[0] => 1111, [0] => 1500)
if there are then return true else return false how to do this ?
Array
(
[0] => Array
(
[0] => 1500
[1] => first
[2] =>
[3] =>
[4] => 50
[5] =>
[6] =>
)
[1] => Array
(
[0] => 1111
[1] => second
[2] =>
[3] =>
[4] => 10
[5] =>
[6] =>
)
[2] => Array
(
[0] => 1500
[1] => third
[2] =>
[3] =>
[4] => 100
[5] =>
[6] =>
)
)
If you have PHP 5.5+ available, the function array_column() makes it easy to extract the first "column" of the sub-arrays, and feed the resultant array to array_count_values(), which would produce an array of values like [1500] => 2, [1111] => 1, from which you can easily deduce which have > 1.
That would look like:
// PHP 5.5+ only...
// Gets counts of each first sub-array value
$counts = array_count_values(array_column($input_multidimensional_array, 0));
// Test that the array key has > 1
// To check a specific one for duplicates:
if (isset($counts['1500']) && $counts['1500'] > 1) {
// Yes, it has duplicates.
}
But... Since you do not have PHP 5.5+, you'll have to use some form of loop.
$temp = array();
foreach ($input_multidimensional_array as $sub_array) {
// A temporary array holds all the first elements
$temp[] = $sub_array[0];
}
// Count them up
$counts = array_count_values($temp);
// Then use the same process to check for multiples/duplicates:
if (isset($counts['1500']) && $counts['1500'] > 1) {
// Yes, it has duplicates.
}
In either of those cases, you could also use array_filter() to only return the array from $counts which had multiples.
// Filter to only those with > 1 into $only_duplicates
$only_duplicates = array_filter($counts, function($v) {
return $v > 1;
});
// To further reduce this only to the _values_ themselves like 1500, 1111
// use array_keys:
$only_duplicates = array_keys($only_duplicates);
// is now array('1500')

How to process second dimension of an Array based on similar first dimension value?

I am trying to write some php code to process the second dimension's value of an array based on similar values of the first dimension values.
Following is the sample output.
[0] => Array (
[0] => 1
[1] => 0.091238491238491
)
[1] => Array (
[0] => 2
[1] => 0.2221793635487
)
[2] => Array (
[0] => 2
[1] => 0.10662717512033
)
[3] => Array (
[0] => 4
[1] => 0.44354338998346
)
[4] => Array (
[0] => 6
[1] => 0.2248243559719
)
[5] => Array (
[0] => 6
[1] => 0.31764705882353
)
[6] => Array (
[0] => 6
[1] => 0.15764625384879
)
[7] => Array (
[0] => 6
[1] => 0.19160083160083
)
[8] => Array (
[0] => 12
[1] => 0.31054875069499
)
[9] => Array (
[0] => 12
[1] => 0.10915034227918
)
[10] => Array (
[0] => 15
[1] => 0.32915461266474
)
//...........goes to 46000 elements
Now what I want to do is, if the index 0 values of each array is similar then I want to add the index 1's value.
So for example, if 0 index values for 4 arrays are same , I want to add index 1 values of all 4 arrays.
If there is a unique value on 0th index, dont add it with anything, simply store index 1's value and move on.
Thanks very much.
Ghanshyam
$added = array();
foreach ($array as $item) {
if (isset($added[$item[0]])) {
$added[$item[0]] += $item[1];
} else {
$added[$item[0]] = $item[1];
}
}
$p=0;
$temp = $final_prod_ex[0][1];
for($x=0; $x<count($final_prod)-1; $x++){
if($final_prod_ex[$x][0]==$final_prod_ex[$x+1][0]){
$temp = $temp + $final_prod_ex[$x+1][1];
}
else{
$ans[$p] = $temp." ".$final_prod_ex[$x][0];
$temp = $final_prod_ex[$x+1][1];
$p++;
}
}
Finally figured it out after a lot of thinking(I'm new to programming)...Array's name is $final_prod_ex. Comment on this if I can make it better. And sorry #deceze. I could not understand your solution. I know you were trying to give the value of one array as an index to another. But what the scenario is, that value isnt like 0,1,2,3,4.... Its like 1,3,5,6,7,10. We are missing numbers in between. Maybe I didnt understand your solution. Correct me if I am wrong.
Thanks for all the help.

Changing values of all similar sub arrays in PHP

I've got the following 2D array, stored inside the array variable $my_array
Array
(
[0] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[2] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
)
I wanted to decrement all the [1] sub array values by 3. Tried the following code, with no success.
$my_array[$i]['1']=($my_array[$i]['1'])-3;
print_r($my_array);
Ideas?
foreach ($my_array as &$val) {
$val[1] -= 3
}
Something like this is what you're after.
foreach($my_array as $k=>$v){
if (isset($my_array[$k][1]) && is_numeric($my_array[$k][1])){
$my_array[$k][1] -= 3;
}
}

Array sorting question

So I have an array such as this one:
Array
(
[-1] => Array
(
[3] => 3
[1] => 1
[6] => 6
[7] => 7
[5] => 5
)
)
It also contains some other keys that should not be modified.
I'd like to the numbers which are in a second array to come first (in the order of that second array), and then will be the numbers that don't exist in the second array, if any.
So for that matter, the second array would be:
Array
(
[0] => 6
[1] => 5
[2] => 3
)
And the final array should be as follows (please remember, there are some more keys inside of that array that should stay as they are):
Array
(
[-1] => Array
(
[6] => 6
[5] => 5
[3] => 3
[1] => 1
[7] => 7
)
)
Any ideas how that can be done?
Thanks!
It's not and shouldn't be termed as sorting but may be this code snippet may help you do what you want to:
$a1 = Array ( [-1] => Array ( [3] => 3 [1] => 1 [6] => 6 [7] => 7 [5] => 5 ) );
$a2 = Array ( [0] => 6 [1] => 5 [2] => 3 );
$sorted = getSortedArray($a1[-1] , $array2);
function getSortedArray($array1 , $array2){
$temp = Array();
$count = 0;
$totalKeys = sizeof($array2);
for($i=0;$i<sizeof($array2);$i++){
$temp[i] = $array1[$array2[i]];
unset($array1[$array2[i]]);
}
while($count!=sizeof($array1))
$temp[$totalKeys++] = $array1[$count++];
return $temp;
}
I believe the function you're looking for is called array_multisort().
array_multisort() can be used to sort
several arrays at once, or a
multi-dimensional array by one or more
dimensions.

Categories