This is what I have:
[courses] => array
(
array([id]=>1,[name]=>"course_1",[age]=>16,[location]=>"Berlin"),
array([id]=>2,[name]=>"course_1",[age]=>18,[location]=>"Berlin"),
array([id]=>3,[name]=>"course_1",[age]=>20,[location]=>"Berlin"),
array([id]=>4,[name]=>"course_1",[age]=>16,[location]=>"London"),
array([id]=>5,[name]=>"course_1",[age]=>18,[location]=>"Rome"),
array([id]=>6,[name]=>"course_2",[age]=>16,[location]=>"Berlin")
);
What I need is to transform this into a multidimensional array like this:
- each name can have multiple ages
- each age can have multiple locations
- id is not needed
So the new transformed array should have courses that have multiple ages that have multiple locations.
I first tried to create an array for each key:
// all courses, sorted, once
$x = 0;
$course_names = array();
foreach ($courses as $item) {
$course_names[$x] = $item['name'];
$x++;
}
$course_names = array_unique($course_names);
sort($course_names);
// all ages, sorted, once
$x = 0;
$ages = array();
foreach ($courses as $item) {
$ages[$x] = $item['age'];
$x++;
}
$ages = array_unique($ages);
sort($ages);
// all locations, sorted, once
$x = 0;
$locations = array();
foreach ($courses as $item) {
$locations[$x] = $item['location'];
$x++;
}
$location = array_unique($location);
sort($location);
Then I tried to create another array as requested (don't swear at me, is one of many tries):
$counter = 0;
foreach ($location as $loc_key=>$loc_val) {
foreach ($ages as $age_key=>$age_val) {
foreach ($course_names as $cou_key=>$cou_val) {
foreach ($courses as $course) {
if ($course['name']==$cou_val) {
if ($course['age']==$age_val) {
if ($course['location']==$loc_val) {
$final_json[$counter]['ages']['locations'][] = $loc_val;
}
$final_json[$counter]['ages'][] = $age_val;
}
$final_json[$counter]['id'] = $course['id'];
$final_json[$counter]['name'] = $cou_val;
$counter++;
}
}
}
}
}
The resulted array should be something like this:
[courses] => array
(
[0] => array
(
[name] => "course_1",
[ages] => array
(
[0] => array
(
[age] => 16,
[locations] => array ([0]=>"Berlin",[1]=>"London")
),
[1] => array
(
[age] => 18,
[locations] => array ([0]=>"Berlin",[1]=>"Rome")
),
[2] => array
(
[age] => 20,
[locations] => array ([0]=>"Berlin")
)
)
)
[1] => array
(
[name] => "course_2",
[ages] => array
(
[0] => array
(
[age] => 16,
[locations] => array ([0]=>"Berlin")
)
)
)
);
I'm fighting with this for some time, I feel blocked, any idea is more than welcomed.
$courses = array(
array("id"=>1,"name"=>"course_1","age"=>16,"location"=>"Berlin"),
array("id"=>2,"name"=>"course_1","age"=>18,"location"=>"Berlin"),
array("id"=>3,"name"=>"course_1","age"=>20,"location"=>"Berlin"),
array("id"=>4,"name"=>"course_1","age"=>16,"location"=>"London"),
array("id"=>5,"name"=>"course_1","age"=>18,"location"=>"Rome"),
array("id"=>6,"name"=>"course_2","age"=>16,"location"=>"Berlin"),
);
foreach ($courses as $item) {
$course_age[] = $item['age'];
$course_names[] = $item['name'];
$course_location[] = $item['location'];
}
$course_age = array_unique($course_age);
$course_names = array_unique($course_names);
$course_location = array_unique($course_location);
$finalArray = array();
$finalArrayKey=0;
foreach ($course_names as $key => $courseValue) {
foreach ($courses as $key => $actualValue) {
if($actualValue['name']==$courseValue){
$finalArray[$finalArrayKey]['name'] = $courseValue;
$finalArray[$finalArrayKey]['ages'] = array();
$finalArray[$finalArrayKey]['ages']['location'] = array();
}
}
foreach ($courses as $key => $actualValue) {
if($actualValue['name']==$courseValue){
if(!in_array($actualValue['age'], $finalArray[$finalArrayKey]['ages']))
$finalArray[$finalArrayKey]['ages'][] = $actualValue['age'];
if(!in_array($actualValue['location'], $finalArray[$finalArrayKey]['ages']['location']))
$finalArray[$finalArrayKey]['ages']['location'][] = $actualValue['location'];
}
}
++$finalArrayKey;
}
echo "<pre>";
print_r($finalArray);
Sample Output:
Array
(
[0] => Array
(
[name] => course_1
[ages] => Array
(
[location] => Array
(
[0] => Berlin
[1] => London
[2] => Rome
)
[0] => 16
[1] => 18
[2] => 20
)
)
[1] => Array
(
[name] => course_2
[ages] => Array
(
[location] => Array
(
[0] => Berlin
)
[0] => 16
)
)
)
For simplicity sake decided to modify the output of the finalArray.
The new finalArray would look like this:
Array
(
[course_1] => Array
(
[ages] => Array
(
[16] => Array
(
[locations] => Array
(
[0] => Berlin
[1] => London
)
),
[18] => Array
(
[locations] => Array
(
[0] => Berlin
[1] => Rome
)
),
[20] => Array
(
[locations] => Array
(
[0] => Berlin
)
)
)
),
[course_2] => Array
(
[ages] => Array
(
[16] => Array
(
[locations] => Array
(
[0] => Berlin
)
)
)
)
)
This is the code:
foreach ($courses as $item) {
$course_names[] = $item['name'];
}
$course_names = array_unique($course_names);
sort($course_names);
$finalArray = array();
foreach ($course_names as $key => $courseValue) {
foreach ($courses as $key => $actualValue) {
if($actualValue['name']==$courseValue) {
$finalArray[$courseValue] = array();
$finalArray[$courseValue]['id'] = $actualValue['id'];
$finalArray[$courseValue]['ages'] = array();
}
}
foreach ($courses as $key => $actualValue) {
if($actualValue['name']==$courseValue) {
if(!array_key_exists($actualValue['age'],$finalArray[$courseValue]['ages'])) {
$finalArray[$courseValue]['ages'][$actualValue['age']] = array ();
}
}
}
}
foreach ($finalArray as $course_name=>$arr) {
foreach ($arr['ages'] as $age=>$arr2) {
foreach($courses as $item) {
if($item['name'] == $course_name && $item['age'] == $age && !in_array($item['location'], $finalArray[$course_name]['ages'][$age]['locations'])) {
$finalArray[$course_name]['ages'][$age]['locations'][] = $item['location'];
}
}
}
}
Not sure exactly what format you're looking for, but have a look at this:
$courses = array
(
array('id'=>1, 'name' =>"course_1",'age'=>16,'location'=>"Berlin"),
array('id'=>2,'name'=>"course_1",'age'=>18,'location'=>"Berlin"),
array('id'=>3,'name'=>"course_1",'age'=>20,'location'=>"Berlin"),
array('id'=>4,'name'=>"course_1",'age'=>16,'location'=>"London"),
array('id'=>5,'name'=>"course_1",'age'=>18,'location'=>"Rome"),
array('id'=>6,'name'=>"course_2",'age'=>16,'location'=>"Berlin")
);
$newCourses = [];
foreach ($courses as $item){
$newCourses[$item['name']]['ages'][$item['age']]['locations'][] = $item['location'];
}
$newCourses will be:
array (
'course_1' =>
array (
'ages' =>
array (
16 =>
array (
'locations' =>
array (
0 => 'Berlin',
1 => 'London',
),
),
18 =>
array (
'locations' =>
array (
0 => 'Berlin',
1 => 'Rome',
),
),
20 =>
array (
'locations' =>
array (
0 => 'Berlin',
),
),
),
),
'course_2' =>
array (
'ages' =>
array (
16 =>
array (
'locations' =>
array (
0 => 'Berlin',
),
),
),
),
)
Even if this isn't the format you're after you could probably play with it to get it how you want.
I have two arrays with different structures. Array 1 and Array 2 that I will name from MyList and MyFiles. I would get to return only MyList values that do not have in MyFiles. But the two arrays have different structures and I'm having trouble trying to compare
MyList
Array
(
[info] => Array
(
[0] => Array
(
[player] => Messi
[week] => Array
(
[id] => 252
[videos] => Array
(
[0] => Array
(
[id] => 2929850
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929848
[link] => best.mp4
)
[2] => Array
(
[id] => 2929847
[link] => dribbling.mp4
)
)
)
)
[1] => Array
(
[player] => CR7
[week] => Array
(
[id] => 251
[videos] => Array
(
[0] => Array
(
[id] => 2929796
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929795
[link] => best.mp4
)
)
)
)
[2] => Array
(
[player] => Neymar
[week] => Array
(
[id] => 253
[videos] => Array
(
[0] => Array
(
[id] => 2929794
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929793
[link] => best.mp4
)
)
)
)
)
)
MyFiles Array
Array
(
[252] => Array
(
[0] => Array
(
[id] => 2929850
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929848
[link] => best.mp4
)
)
[251] => Array
(
[0] => Array
(
[id] => 2929796
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929795
[link] => best.mp4
)
)
)
the comparison must be made by id of the week and id of the video
I tried this but it did not work out:
$new = array();
foreach ($list['info'] as $source) {
foreach ($source["week"]['videos'] as $keys => $videos) {
foreach ($file as $key => $upload) {
if ($source["week"]["id"] == $key ) {
for($i=0; $i<count($source["week"]["videos"]); $i++){
if($videos["id"] == $upload[$i]["id"]){
unset($videos);
}else{
$new[] = $videos;
}
}
} else {
$new[] = $videos;
}
}
}
}
The expected return would be something like this.
Array
(
[info] => Array
(
[0] => Array
(
[player] => Messi
[week] => Array
(
[id] => 252
[videos] => Array
(
[2] => Array
(
[id] => 2929847
[link] => dribbling.mp4
)
)
)
)
[2] => Array
(
[player] => Neymar
[week] => Array
(
[id] => 253
[videos] => Array
(
[0] => Array
(
[id] => 2929794
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929793
[link] => best.mp4
)
)
)
)
)
)
I have hidden the array in a usable format for my sake in the future should this answer be incorrect and need changing.
$desired = array();
$desired['info'][0]['player'] = 'Messi';
$desired['info'][0]['week']['id'] = 252;
$desired['info'][0]['week']['videos'][2]['id'] = 2929847;
$desired['info'][0]['week']['videos'][2]['link'] = 'dribbling.mp4';
$desired['info'][2]['player'] = 'Neymar';
$desired['info'][2]['week']['id'] = 253;
$desired['info'][2]['week']['videos'][0]['id'] = 2929794;
$desired['info'][2]['week']['videos'][0]['link'] = 'goals.mp4';
$desired['info'][2]['week']['videos'][1]['id'] = 2929793;
$desired['info'][2]['week']['videos'][1]['link'] = 'best.mp4';
$list = array();
$list["info"][0]["player"] = "Messi";
$list["info"][0]["week"]["id"] = "252";
$list["info"][0]["week"]["videos"][0]["id"] = 2929850;
$list["info"][0]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][0]["week"]["videos"][1]["id"] = 2929848;
$list["info"][0]["week"]["videos"][1]["link"] = "best.mp4";
$list["info"][0]["week"]["videos"][2]["id"] = 2929847;
$list["info"][0]["week"]["videos"][2]["link"] = "dribbling.mp4";
$list["info"][1]["player"] = "CR7";
$list["info"][1]["week"]["id"] = "251";
$list["info"][1]["week"]["videos"][0]["id"] = 2929796;
$list["info"][1]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][1]["week"]["videos"][1]["id"] = 2929795;
$list["info"][1]["week"]["videos"][1]["link"] = "best.mp4";
$list["info"][2]["player"] = "Neymar";
$list["info"][2]["week"]["id"] = "253";
$list["info"][2]["week"]["videos"][0]["id"] = 2929794;
$list["info"][2]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][2]["week"]["videos"][1]["id"] = 2929793;
$list["info"][2]["week"]["videos"][1]["link"] = "best.mp4";
$file = array();
$file[252][0]['id'] = 2929850;
$file[252][0]['link'] = 'goals.mp4';
$file[252][1]['id'] = 2929848;
$file[252][1]['link'] = 'best.mp4';
$file[251][0]['id'] = 2929796;
$file[251][0]['link'] = 'goals.mp4';
$file[251][1]['id'] = 2929795;
$file[251][1]['link'] = 'best.mp4';
Edit
function array_diff_assoc_recursive($array1, $array2) {
$difference=array();
foreach ($array1 as $key => $value) {
if (is_array($value)) {
if( !isset($array2[$key]) || !is_array($array2[$key])) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if (!empty($new_diff))
$difference[$key] = $new_diff;
}
} else if (!array_key_exists($key,$array2) || $array2[$key] !== $value) {
$difference[$key] = $value;
}
}
return $difference;
}
$new = array('info' => array());
foreach ($list['info'] as $key => $item) {
$a = $item['week']['videos'];
//$b = $file[$item['week']['id']] ?? []; // This is PHP7+
$b = isset($file[$item['week']['id']]) ? $file[$item['week']['id']] : [];
$c = array_diff_assoc_recursive($a, $b);
if (!empty($c)) {
$new['info'][$key] = $item;
$new['info'][$key]['week']['videos'] = $c;
}
}
You will need a function that will check the difference between the videos arrays.
What I do is simply iterate over the list array and then check the difference between that item and the file array. The difference is then stored in $c.
If there is a difference then if statement is fired which will store that player in the $new array and then replace the videos array with the difference array.
This is similar to what you were doing when you were unsetting variables.
I have three arrays i need to create an array which can be multidimensional.
1st array
Array
(
[0] => Test_One
[1] => Test_two
)
2nd array
Array
(
[0] => www.link.com
[1] => www.link2.com
)
3rd array
Array
(
[0] => Song1
[1] => song2
)
What i want
Array
(
[www.link.com] => Array
(
[0] => Test_one
[1] => Song1
)
[www.link2.com] => Array
(
[0] => Test_two
[1] => Song2
)
)
Assuming that you have same number of elements in all three arrays:
<?php
$arr1 = Array
(
0 => "Test_One",
1 => "Test_two"
);
$arr2 = Array
(
0 => "www.link.com",
1 => "www.link2.com"
);
$arr3 = Array
(
0 => "Song1",
1 => "Song2"
);
$final = []; //for versions below PHP 5.4 use $final = array();
foreach($arr2 as $key=>$value) {
$final[$value] = [$arr1[$key],$arr3[$key]];
}
print_r($final);
will output:
Array
(
[www.link.com] => Array
(
[0] => Test_One
[1] => Song1
)
[www.link2.com] => Array
(
[0] => Test_two
[1] => Song2
)
)
Update: Simplified foreach loop. From Comments #uchiha
Assuming that you havn't same number of elements in all three arrays:
<?php
$arr1 = Array
(
0 => "Test_One",
1 => "Test_two"
);
$arr2 = Array
(
0 => "www.link.com",
1 => "www.link2.com"
);
$arr3 = Array
(
0 => "Song1",
);
$final = []; //for versions below PHP 5.4 use $final = array();
foreach($arr2 as $key=>$value) {
if(array_key_exists($key,$arr1)) {
$final[$value][] = $arr1[$key];
}
if(array_key_exists($key,$arr3)) {
$final[$value][] = $arr3[$key];
}
}
print_r($final);
Output:
Array
(
[www.link.com] => Array
(
[0] => Test_One
[1] => Song1
)
[www.link2.com] => Array
(
[0] => Test_two
)
)
Hope this help :)
<?php
$array1 = Array
(
'Test_One',
'Test_two'
);
$array2 = Array
(
'www.link.com',
'www.link2.com'
);
$array3 = Array
(
'Song1',
'song2'
);
$array4 = array();
$i = 0;
foreach ($array2 as $a2){
$array4[$a2][] = $array1[$i];
$array4[$a2][] = $array3[$i];
$i++;
}
echo "<pre>";
print_r($array4);
?>
Trying to alphabetically merge arrays with foreach loop.
<?php
$fruits = array(
'Apple' => array('ids'=>array(1,2)),
'Banana' => array('ids'=>array(3,4)),
'Ananas' => array('ids'=>array(5,6))
);
$result = array();
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
$result[$first_letter] = $subarr;
}
print_r($result);
gives me smth like
Array
(
[A] => Array
(
[ids] => Array
(
[0] => 5
[1] => 6
)
)
[B] => Array
(
[ids] => Array
(
[0] => 3
[1] => 4
)
)
)
instead of smth like
[A] => Array
(
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 6
)
)
how can I fix it?
You overwrite your result every iteration in this line:
$result[$first_letter] = $subarr;
Just create a new array if the subArray in the result array doesn't exists and merge the ids subArray into your result array.
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
if(!isset($result[$first_letter]))
$result[$first_letter] = [];
$result[$first_letter] = array_merge($result[$first_letter], $subarr["ids"]);
}
Please try to use foreach loop.
$fruits = array(
'Apple' => array('ids'=>array(1,2)),
'Banana' => array('ids'=>array(3,4)),
'Ananas' => array('ids'=>array(5,6))
);
$result = array();
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
foreach($subarr as $key=>$value){
foreach ($value as $gkey => $gvalue) {
$result[$first_letter]['ids'][] = $gvalue;
}
}
}
echo "<pre>";
print_r($result);
Display above code output like below.
Array
(
[A] => Array
(
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 6
)
)
[B] => Array
(
[ids] => Array
(
[0] => 3
[1] => 4
)
)
)
I need to get the last entries of all duplicate records in an array. How can I do this in php
example data
Input Array
Array
(
[0] => Array ( [0] => A )
[1] => Array ( [0] => A )
[2] => Array ( [0] => B )
[3] => Array ( [0] => C )
[4] => Array ( [0] => C )
[5] => Array ( [0] => D )
[6] => Array ( [0] => F )
[7] => Array ( [0] => F )
)
Result output List 1 should only contain last entry of all duplicate records
Array
(
[1] => Array ( [0] => A )
[4] => Array ( [0] => C )
[7] => Array ( [0] => F )
)
Result output List 2 Should contain all other enteries.
Array
(
[0] => Array ( [0] => A )
[2] => Array ( [0] => B )
[3] => Array ( [0] => C )
[5] => Array ( [0] => D )
[6] => Array ( [0] => F )
)
Not sure if you wanted it this way, but you can try this:
<?php
$array = array
(
0 => array ( 0 => 'A' ),
1 => array ( 0 => 'A' ),
2 => array ( 0 => 'B' ),
3 => array ( 0 => 'C' ),
4 => array ( 0 => 'C' ),
5 => array ( 0 => 'D' ),
6 => array ( 0 => 'F' ),
7 => array ( 0 => 'F' )
);
foreach ($array as $k => $v){
echo $k . " --> ";
if (is_array($v)){
foreach($v as $k1=>$v1){
echo $v1."<br />";
$new_array[$k]=$v1;
}
}else{
echo $v."<br />";
}
}
echo "<br />";
// Restructured array
print_r($new_array);
echo "<br />";
// Duplicates
print_r(get_duplicates($new_array));
echo "<br />";
// All entries
print_r(array_unique($new_array));
// Function to get duplicates
function get_duplicates( $array ) {
return array_unique( array_diff_assoc( $array, array_unique( $array ) ) );
}
?>
output would be:
//original
Array ( [0] => A [1] => A [2] => B [3] => C [4] => C [5] => D [6] => F [7] => F )
//dupes
Array ( [1] => A [4] => C [7] => F )
//all entries
Array ( [0] => A [2] => B [3] => C [5] => D [6] => F )
typed this up quickly. haven't had a chance to test it yet, but should do the trick.
note that the inner loop is non-optimal -- it could be improved a lot, especially if the data is known to be sorted (as in the example data), but you didn't specify that, so I haven't assumed it.
<?php
$output_dups = array();
$output_remainder = array();
foreach($input_array as $key=>$data) {
$dup_found = false;
foreach($input_array as $key2=>$data2) {
if($key < $key2 && $data===$data2) { $dup_found = true; }
}
if($dup_found) { $output_dups[] = $data; } else { $output_remainder[] = $data; }
}
?>
//My implementation is something like this.
set_time_limit (1500) ;
ini_set("memory_limit","128M");
$fileName = "_one";
$objScan = new scanCSV();
$objScan->setCSVFileName($fileName);
$objScan->loadCsvFile();
$objScan->separateDuplicateFromUniq();
$objScan->process();
class scanCSV
{
private $_csvFile = NULL;
private $_arrayListAll = NULL;
private $_allDuplicateRec = NULL;
private $_uniqueRec = NULL;
function setCSVFileName($fileName){
$this->_csvFile = $fileName;
}
//-----------------------------------------------------------------------
function loadCsvFile()
{
$arrayListAll = array();
if (($handle = fopen($this->_csvFile . ".csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$arrayListAll[] = $data;
}
}
$this->_arrayListAll = $arrayListAll;
}
//-----------------------------------------------------------------------
public function separateDuplicateFromUniq(){
$allDuplicateRec = array();
$uniqueRec = array();
foreach($this->_arrayListAll as $data){
if ($this->getcount($this->_arrayListAll, $data) > 1)
$allDuplicateRec[] = $data;
else
$uniqueRec[] = $data;
}
$this->_allDuplicateRec = $allDuplicateRec;
$this->_uniqueRec = $uniqueRec;
}
//-----------------------------------------------------------------------
public function process (){
$uniq = $this->removeDuplicate ($this->_allDuplicateRec);
$this->writeCSVFile ($this->_csvFile . "A.csv", $uniq);
$restofEntries = $this->removeLastEntries ($this->_arrayListAll, $uniq);
$this->writeCSVFile ($this->_csvFile . "B.csv", $restofEntries);
}
//-----------------------------------------------------------------------
function removeDuplicate ($allDuplicateRec)
{
foreach ($allDuplicateRec as $k => $v)
if ( $this->getcount($allDuplicateRec, $v) > 1 )
unset($allDuplicateRec[$k]);
return $allDuplicateRec;
}
//-----------------------------------------------------------------------
function removeLastEntries ($arrayListAll, $uniq){
foreach ($uniq as $entry)
if(in_array($entry, $arrayListAll))
unset($arrayListAll[array_search($entry, $arrayListAll)]);
return $arrayListAll;
}
//-----------------------------------------------------------------------
function getcount($arrayList1, $data){
$address = $data[2];
$count =0;
foreach ($arrayList1 as $dt)
if ($address == $dt[2])
$count++;
return $count;
}
//-----------------------------------------------------------------------
function writeCSVFile ($fileName, $data){
$fp = fopen($fileName, 'w');
foreach ($data as $k=>$fields)
fputcsv($fp, $fields);
fclose($fp);
}
//-----------------------------------------------------------------------
} // end of scan Optimized