Combining two arrays PHP - php

I'm busy with sorting the structure of a menu in my application. Once the menu is reorderd by the user, the values (say; Menu item 1, Menu item 2, etc) are still in the same place.
Now I have two arrays, one that holds the way they are sorted (Array 1) and one that holds the values of the menu items. (Array 2)
Example of both arrays;
(Array 1, that holds the keys)
Array
(
[0] => 1
[1] => 2
[2] => 0
)
The above array's values are the keys for the new array.
(Array 2, holds the values)
Array
(
[0] => value_0
[1] => value_1
[2] => value_2
)
So I thought it would be best to create a new array which consist out of;
The values of Array 1
The values of Array 2
However, i'm running into a problem. I want the values in array 2 to stick to their keys. So lets say I change the position of value_0 to the last, the new array would look like this;
Array
(
[1] => value_1
[2] => value_2
[0] => value_0
)
Is there a way to achieve this or am I doing it completely wrong?
Edit
Ok, so multidemensional array it is. However i'm having problems creating one.
Array 1 and Array 2 both come from the database. Array 1 with the sorting order and Array 2 contains the values. Now, the values in array 2 are stored like this; value1,value2,value3. So to be able to work with them I explode on , (comma).
The results on the fetchs are both different;
For the first array it returns as many as how many values there are.
(So if there are 3 values, it will return 3 different positions.)
For the second array it will return 18 records, since this is tied to
other menu items (sub menu's etc).
So for the first array I do;
while ($row = mysql_fetch_assoc($result_query_test)) {
$positions[] = $row['position'];
}
For the second array I do;
while ($row = mysql_fetch_assoc($result_values)) {
$array_values = explode(',', $row['values']);
}
From then on i'm having problems creating the multidimensinonal array;
while ($row = mysql_fetch_assoc($result_values)) {
$array_values = explode(',', $row['values']);
foreach ($positions as $new_key) {
foreach ($array_values as $value) {
$new_array[] = array('key' => $new_key, 'value' => $value);
}
}
}
Edit two:
This is what I use now;
(Since $all_values is a multidimensional array because I have to explode on the values beforehand.)
foreach ($all_values as $values) {
foreach ($values as $key => $value) {
$new_array[] = array('key' => $positions[$key], 'value' => $value);
}
}
This is what the $new_array returns;
Array
(
[0] => Array
(
[key] => 0
[value] => value_0
)
[1] => Array
(
[key] => 2
[value] => value_2
)
[2] => Array
(
[key] => 1
[value] => value_1
)
[3] => Array
(
[key] => 0
[value] => value_0
)
[4] => Array
(
[key] => 2
[value] => value_2
)
[5] => Array
(
[key] => 1
[value] => value_1
)
Now I need to get the values and implode them with comma's. However, since not every 3 values (value_0, value_1, value_3) are together I can't do that now.
In this example there are 3 keys, (0,1,2) which should be a different array along with their values, like you did in your example:
Array (
[0] = Array (
[key] = 1,
[value] = value_1
),
[1] = Array (
[key] = 2,
[value] = value_2
),
[2] = Array (
[key] = 0,
[value] = value_0
)
)

Why not make a multidimensional array?
$arrayThree =
Array (
[0] = Array (
[key] = 1,
[value] = value_1
),
[1] = Array (
[key] = 2,
[value] = value_2
),
[2] = Array (
[key] = 0,
[value] = value_0
)
)
No matter what order they're in, the key and value are always the set.
foreach ($arrayThree as $tempArray)
{
echo $tempArray['key'];
echo $tempArray['value'];
}
Create Array
$arrayOne = array();
$arrayTwo = array();
$arrayThree = array();
$query = 'SELECT key FROM table1 ';
$result = mysql_query($query) or die(mysql_error());
while($data = mysql_fetch_assoc($result))
{
$arrayOne[] = $data['key'];
}
$query = 'SELECT value FROM table2 ';
$result = mysql_query($query) or die(mysql_error());
while($data = mysql_fetch_assoc($result))
{
$arrayTwo[] = $data['value'];
}
foreach($arrayOne as $key => $value)
{
$arrayThree[] = array('key' => $value, 'value' => $arrayTwo[$key]);
}
You can always use the mysqli or PDO versions, if you're using them.
Example Data
//THESE MIMIC YOUR SELECT RESULTS
$test_keys = array(1,2,3);
$test_values = array('value_1', 'value_2', 'value_3');
//DEFAULTS
$arrayOne = array();
$arrayTwo = array();
$arrayThree = array();
//WHILE FIRST SELECT
for($i=0;$i<count($test_keys);$i++)
{
$arrayOne[] = $test_keys[$i];
}
//WHILE SECOND SELECT
for($i=0;$i<count($test_values);$i++)
{
$arrayTwo[] = $test_values[$i];
}
//MAKE THE FINAL ARRAY
foreach($arrayOne as $key => $value)
{
$arrayThree[] = array('key' => $value, 'value' => $arrayTwo[$key]);
}
//CHECK THE OUTPUT FOR THE NEW ARRAY
echo '<pre>'.print_r($arrayThree,true).'</pre>';
Example Output
Array
(
[0] => Array
(
[key] => 1
[value] => value_1
)
[1] => Array
(
[key] => 2
[value] => value_2
)
[2] => Array
(
[key] => 3
[value] => value_3
)
)
Imploded List
$implodeValues = array_map(function($item) { return $item['value']; }, $arrayThree);
$implodeVariable = implode(',', $implodeValues);
echo $implodeVariable;
Implode Output
value_1,value_2,value_3

I think you can obtain what you want doing this:
$new = array();
for($i = 0, $len = count($array1), $i < $len, ++$i) {
$new[$array1[$i]] = $array2[$i];
}
now $new contains the values in the order you want them

Related

Combining Arrays while merging the values with the same key

I have two arrays with same amount of values. I need to combine them ( array1 value to key, array2 value as value) without losing the values of the second array due to duplicate key. when I use combine_array() as expected it just gets the last value of the second array with the same key.
Array
(
[0] => 1
[1] => 2
[2] => 2
[3] => 3
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Desired result
Array
(
[1] => 1
[2] => Array(
[0]=>2
[1]=>3
)
[3] => 2
)
This code will meet your request
$array1 = array("0"=>1,"1"=>2,"2"=>2,"3"=>3);
$array2 = array("0"=>1,"1"=>2,"2"=>3,"3"=>4);
$array = array();
foreach($array1 as $key => $value){
if($value != $array2[$key]){
$array[$key][] = $value;
$array[$key][] = $array2[$key];
}else{
$array[$key] = $value;
}
}
print_r($array);
The desired result is
Array
(
[0] => 1
[1] => 2
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 3
[1] => 4
)
)
I'm sure there are way better solutions than this, but it does the job for now. I would appreciate if someone can send a better written solution.
$combined = array();
$tempAr = array();
$firstMatch = array();
$count = 0;
foreach ($array1 as $index => $key) {
if (array_key_exists($key, $combined)) {
$tempAr[] = $array2[$index];
$count++;
} else {
$totalCount = $count;
}
if (!array_key_exists($key, $firstMatch)) {
$firstMatch[$key] = $array2[$index];
}
$output = array_slice($tempAr, $totalCount);
$combined[$key] = $output;
}
$combined = array_merge_recursive($firstMatch, $combined);

php split array on change of value in column data

I am trying to split array data into multiple arrays based on change in data value at known position (column).
$input = array(
array(1,2,3),
array(4,5,3),
array(7,8,4),
array(9,10,4),
array(11,12,4)
);
Here column 3 changes values from 3 to 4
and expected result is to have 2 arrays
$out1 = array(array(1,2,3),array(4,5,3));
$out2 = array(array(7,8,4), array(9,10,4), array(11,12,4));
since number of rows are variable, cannot use array_chunk
since column 3 values are variable, cannot use array_filter
number of output arrays are also variable.
trying splice but failing...
You can use array_reduce to make new array, where index will be equal to last numbers in items
$new = array_reduce($input, function($c, $x) { $c[$x[2]][] = $x; return $c; }, [] );
$out1 = $new[3];
$out2 = $new[4];
demo
But if array is not sorted and you want to split at points of changing that number, the code can be
$i = -1;
$last = null;
$new = [];
foreach($input as $x) {
if ($x[2] != $last) {
$i++;
$last = $x[2];
}
$new[$i][] = $x;
}
demo
You can use split index with index of array,
$out1 = $out2 = [];
$splitIndex = 2;
foreach($input as $k => $v){
if($k < $splitIndex){
$out1[] = $v;
}else{
$out2[] = $v;
}
}
print_r($out1);
print_r($out2);
Working demo
Output:-
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 3
)
)
Array
(
[0] => Array
(
[0] => 7
[1] => 8
[2] => 4
)
[1] => Array
(
[0] => 9
[1] => 10
[2] => 4
)
[2] => Array
(
[0] => 11
[1] => 12
[2] => 4
)
)

Pushing a sub array into the same array

I am trying to put content of one array into the same array. Here I have an array $mclass with values such as
Array
(
[0] => stdClass Object
(
[room_id] => 1,3,5
[day] => 1
[class_teacher] => TEA-2014-2
[final_exam_date] => 2015-09-21
)
)
You can see I have room_id index with 1,3,5 value. Now, I want to explode the room_id and get duplicate of same array index data with change of room_id and push into the array. and finally delete the current array index such as [0]. Here I want the final result as.
Array
(
[0] => stdClass Object
(
[room_id] => 1
[day] => 1
[class_teacher] => TEA-2014-2
[final_exam_date] => 2015-09-21
)
[1] => stdClass Object
(
[room_id] => 3
[day] => 1
[class_teacher] => TEA-2014-2
[final_exam_date] => 2015-09-21
)
[2] => stdClass Object
(
[room_id] => 5
[day] => 1
[class_teacher] => TEA-2014-2
[final_exam_date] => 2015-09-21
)
)
Here is my code for the same:
if(count($mclass)>0)
{
foreach($mclass as $mclasskey=>$mclass_row)
{
/* Room ID Calculation */
if(isset($mclass[$mclasskey]))
{
$temp_room_id = explode(',',$mclass_row->room_id);
if(count($temp_room_id)>1)
{
foreach($temp_room_id as $trkey=>$tr)
{
if(!in_array($temp_room_id[$trkey], $morning_class_semester))
{
array_push($morning_class_semester,$temp_room_id[$trkey]);
}
}
if(count($morning_class_semester)>0)
{
foreach($morning_class_semester as $mcskey=>$mcs)
{
$index_count = count($new_test);
$test[$index_count] = $mclass[$mclasskey];
$test[$index_count]->room_id = $morning_class_semester[$mcskey];
array_push($new_test,$test[$index_count]);
}
unset($mclass[$mclasskey]);
}
}
}
}
}
The code below does what you're looking for using only arrays. So you'll have to change the array access operators to -> since you're accessing an object. I'd do so, but it would break the example, so I'll leave that up to you.
Code Explained:
Loop through array selecting each subarray (object in your case), explode on the $item('room_id') ... ($item->room_id in your case) ... and create sub arrays, via loop, from that using the data from the original using each key. Remove the original item (which has the combined room_ids) and combine the placeholder and original array.
<?php
//Establish some data to work with
$array = array(
array(
"room_id" => "1,3,5",
"day" => 1,
"class_teacher" => "TEA-2014-2",
"final_exam_date" => "2015-09-21",
));
foreach ($array as $key => $item) {
$placeholder = array();
$ids = explode(',',$item['room_id']);
if (count($ids) > 1) {
foreach ($ids as $id) {
$push = array(
'room_id' => $id,
'day' => $item['day'],
'class_teacher' => $item['class_teacher'],
'final_exam_date' => $item['final_exam_date']
);
array_push($placeholder, $push);
}
$array = array_merge($array, $placeholder);
unset($array[$key]);
}
}
var_dump($array);
?>

Get duplicate and unique data between two multi-dimensional arrays

There's a lot of questions out there that deals between arrays and multi-dimensional arrays but only one so how about two then? Let's say this is the data:
Array
(
[0] => Array
(
[BRANCHCODE] => 2
[BRANCH] => BRANCH 1
[AGREEID] => 1
)
[1] => Array
(
[BRANCHCODE] => 2
[BRANCH] => BRANCH 2
[AGREEID] => 2
)
[2] => Array
(
[BRANCHCODE] => 2
[BRANCH] => BRANCH 3
[AGREEID] => 3
)
)
Array
(
[0] => Array
(
[BRANCHCODE] => 2
[BRANCH] => BRANCH 4
[AGREEID] => 1
)
[1] => Array
(
[BRANCHCODE] => 2
[BRANCH] => BRANCH 5
[AGREEID] => 4
)
[2] => Array
(
[BRANCHCODE] => 2
[BRANCH] => BRANCH 6
[AGREEID] => 5
)
)
I managed to get the duplicated data but I can't get the unique data. Here's the php code on how I got the duplicates and also the count of the duplicated data.
In here I loop through both of the two arrays the first one is the array of the uploaded data and the next loop is the database results. I compared the two arrays if the AGREEID in the uploaded data has duplicate in the database. If the AGREEID in the uploaded data is unique I will insert it in the database.
foreach ($result as $key=>$upload_data) {
$agreeid_upload = $result[$key]['AGREEID'];
$data = $result[$key];
$another_data = $result[$key];
foreach ($reports as $dbase_data) {
$agreeid = $dbase_data->AGREEID;
if($agreeid_upload == $agreeid){ /// record has duplicate in the database
$count_duplicates = $count_duplicates + 1;
$duplicates[$key] = $data;
}else{
///here i want to store into another array the unique data..
}
}
}
The method used would work, but is going to be inefficient with a large amount of data because it will do a lot of extra looping.
$dbAgreeeData = array();
$duplicates = array();
$unique = array();
foreach ($reports as $dbase_data) {
$dbAgreeeData[$db_dbase_data->AGREEID] = $dbase_data;
}
foreach ($result as $key=>$upload_data) {
$agreeId = $upload_data['AGREEID'];
if (isset($dbAgreeeData[$agreeId)){
$duplicates[$agreeId] = $upload_data;
}
else {
$unique[$agreeId] = $upload_data;
}
}
$numDuplicate = count($duplicates);
$numUnique = count($unique);
Or
$dbAgreeeData = array();
$uploadData= array();
foreach ($reports as $dbase_data) {
$dbAgreeeData[$db_dbase_data->AGREEID] = $dbase_data;
}
foreach ($result as $key=>$upload_data) {
$uploadData[$upload_data['AGREEID']] = $upload_data;
}
$duplicates = array_intersect_key($dbAgreeData, $uploadData;
$unique = array($dbAgreeData, $uploadData);
$numDuplicate = count($duplicates);
$numUnique = count($unique);

Intersecting multidimensional array of varying size

I have a multidimensional array that looks like this:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 3
)
[1] => Array
(
[id] => 1
)
[2] => Array
(
[id] => 2
)
[3] => Array
(
[id] => 5
)
[4] => Array
(
[id] => 4
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
)
[1] => Array
(
[id] => 3
)
[2] => Array
(
[id] => 4
)
[3] => Array
(
[id] => 5
)
)
[2] => Array
(
[0] => Array
(
[id] => 3
)
)
)
I need to find a way to return the intersecting value(s). In this case that would be
[id] => 3
The length of the array could be different, so I can't just use array_intersect().
This would be simple if your arrays contained only integers, but as they contain an another array, it gets a bit more complicated. But this should do it:
function custom_intersect($arrays) {
$comp = array_shift($arrays);
$values = array();
// The other arrays are compared to the first array:
// Get all the values from the first array for comparison
foreach($comp as $k => $v) {
// Set amount of matches for value to 1.
$values[$v['id']] = 1;
}
// Loop through the other arrays
foreach($arrays as $array) {
// Loop through every value in array
foreach($array as $k => $v) {
// If the current ID exists in the compare array
if(isset($values[$v['id']])) {
// Increase the amount of matches
$values[$v['id']]++;
}
}
}
$result = array();
// The amount of matches for certain value must be
// equal to the number of arrays passed, that's how
// we know the value is present in all arrays.
$n = count($arrays) + 1;
foreach($values as $k => $v) {
if($v == $n) {
// The value was found in all arrays,
// thus it's in the intersection
$result[] = $v;
}
}
return $result;
}
Usage:
$arrays = array(
array(array('id' => 3), array('id' => 1), array('id' => 2), array('id' => 5), array('id' => 4)),
array(array('id' => 1), array('id' => 3), array('id' => 4), array('id' => 5)),
array(array('id' => 3))
);
print_r(custom_intersect($arrays));
Result:
Array
(
[0] => 3
)
This function isn't perfect: if you have duplicate ID's in one array, it will not work. That would require a bit more code to first make the array values unique, but this will probably work in your case.
You can use array_uintersect() to get the intersection of arrays using a custom comparison function. You have to call it with call_user_func_array() though, as it expects each array as a separate argument:
//build list of parameters for array_uintersect()
$params = array_merge($input, array('compare_func'));
$result = call_user_func_array('array_uintersect', $params);
function compare_func($a, $b) {
return $a['id'] - $b['id'];
}
You can't simply call array_intersect() with call_user_func_array(), because it seems to compare arrays by comparing their string representation (which will always be 'Array').
$a = array(4,3);
$b = array(4,3,2,1);
$c = array(1,2,3,4,5);
$d = array(5,4,3);
$array = array($a,$b,$c,$d);
for ($i=0; $i<count($array); $i++){
if ($i==0){
$array2 = $array[$i];
} else {
$array2 = array_intersect($array2, $array[$i]);
}
}
print_r($array2);`
Result:
3,4
As mentioned in one of the comments of the php.net website (array_intersect function).
$a = array(1,2,3,4,5,2,6,1); /* repeated elements --> $a is not a set */
$b = array(0,2,4,6,8,5,7,9,2,1); /* repeated elements --> $b is not a set */
$ua = array_merge(array_unique($a)); /* now, $a is a set */
$ub = array_merge(array_unique($b)); /* now, $b is a set */
$intersect = array_merge(array_intersect($ua,$ub));
This will return this array:
Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
[4] => 6
)

Categories