I have nested multidimensional arrays that may be 2 or 3 levels deep. Inside it I may or may not have list arrray. I need to loop over the array:
Array
(
[0] => Array
(
[id] => 1
[name] => cat_name_1
[list] => Array
(
[1] => swgdgbdg
[2] => xcbcxb
)
)
[1] => Array
(
[id] => 3
[name] => cat_name_3
[list] => Array
(
[0] => Array
(
[id] => 1
[name] => cat_name_1
[list] => Array
(
[1] => 543h54h
[2] => 54hrhhfr2
)
)
[1] => Array
(
[id] => 2
[name] => cat_name_2
[list] => Array
(
[1] => eherfherh
[2] => 4564642
)
)
[2] => Array
(
[1] => erggb45yt
[2] => jyuk768k
)
[3] => Array
(
[1] => sdgsdgsdg
[2] => 4tg34g34g
)
)
)
and store the following in another array:
array (
0 => array (
[1] => swgdgbdg
[2] => xcbcxb
) ,
1 => array(
[1] => 543h54h
[2] => 54hrhhfr2
) ,
2 => array(
[1] => eherfherh
[2] => 4564642
),
3 => array(
[1] => erggb45yt
[2] => jyuk768k
),
4 => array(
[1] => sdgsdgsdg
[2] => 4tg34g34g
)
);
You can use array_walk_recursive() to get the key 1,2 and are not array element, like this, check the live demo here.
$result = [];
$temp = [];
array_walk_recursive($array, function($v, $k) use(&$result, &$temp){
if($k == 1)
$temp[$k] = $v;
if($k == 2)
{
$temp[$k] = $v;
$result[] = $temp;
}
});
print_r($result);
Edit for unfixed length and unordered index, live demo.
Extend the array_walk_recursive() with a third parameter for the callfunction to indicate if it's at the start of an subarray.
$result = [];
$temp = [];
$length = 0;
uarray_walk_recursive($array, function($v, $k, $f) use(&$result, &$temp, &$length){
if(is_numeric($k)) {
if($f && $length != 0) {
$result[] = $temp;
$length = 0;
$temp = [];
}
$temp[$k] = $v;
$length++;
}
});
$result[] = $temp;
print_r($result);
function uarray_walk_recursive(&$input, $funcname)
{
if (!is_callable($funcname)) {
if (is_array($funcname)) {
$funcname = $funcname[0] . '::' . $funcname[1];
}
user_error('uarray_walk_recursive() Not a valid callback ' . $user_func,
E_USER_WARNING);
return;
}
if (!is_array($input)) {
user_error('uarray_walk_recursive() The argument should be an array',
E_USER_WARNING);
return;
}
$args = func_get_args();
$flag = true;
foreach ($input as $key => $item) {
if (is_array($item)) {
$args[2] = false;
$flag = true;
uarray_walk_recursive($item, $funcname, $args);
$input[$key] = $item;
} else {
$args[2] = $flag;
$flag = false;
$args[0] = &$item;
$args[1] = &$key;
call_user_func_array($funcname, $args);
$input[$key] = $item;
}
}
}
PseudoCode
function getSomething(var arr)
{
flag = 0;
output = []
for( i = 0 to arr.length)
{
check arr[i] is array,
if yes,then flag = 1 and output.add(getSomething(arr[i]))
if not, continue
}
if flag =0,then return arr
else return output;
}
Related
I have PHP array. I am trying to convert into a parent-child array:
I have used a recursive function but not getting my output.
Array
(
[0] => Array
(
[id] => 1
[subscription_name] => Yearly
[parent_id] => 0
)
[1] => Array
(
[id] => 22
[subscription_name] => Yearly new
[parent_id] => 1
)
[2 => Array
(
[id] => 23
[subscription_name] => Yearly new offer
[parent_id] => 22
)
[3] => Array
(
[id] => 24
[subscription_name] => Weekly
[parent_id] => 0
)
[4] => Array
(
[id] => 25
[subscription_name] => Weekly new offer
[parent_id] => 24
)
)
I expect this result
Array
(
[0] => Array
(
[id] => 1
[subscription_name] => Yearly new offer
[childrens] => Array
(
[0] => Array
(
[id] => 22
)
[1] => Array
(
[id] => 23
)
)
)
[1] => Array
(
[id] => 24
[subscription_name] => Weekly new offer
[childrens] => Array
(
[0] => Array
(
[id] => 25
)
)
)
)
I have tried but not getting my desire Output
My PHP function is
function tree(array $elements, $parent_id = 0) {
echo "<pre>";
$branch = array();
$a=array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parent_id && $element['subscription_type_id'] !=1 ) {
$children = $this->tree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
else {
$element['children'] = array();
}
$branch[] = $element;
}
}
return $branch;
}
Here is the output I am getting from the above function:
Array
(
[0] => Array
(
[id] => 1
[subscription_name] => Yearly
[children] => Array
(
[0] => Array
(
[id] => 22
[subscription_name] => Yearly new
[children] => Array
(
[0] => Array
(
[id] => 23
[subscription_name] => Yearly new offer
[children] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 24
[subscription_name] => Weekly
[children] => Array
(
[0] => Array
(
[id] => 25
[subscription_name] => Weekly new offer
[children] => Array
(
)
)
)
)
Please help me to solve this problem.
Thanks.
Really, i mean REALLY naive version but by the way, your data would be really more easy to exploit, like don't have any parent_id to 0 if it's not a parent Id (NULL for example) I also do not understand why do you have different subscription_name. I think you should work more on change your input instead of find a complexe algorithm to parse them
function tree(array $elements) {
$openList = [];
$result = [];
$id = 0;
foreach ($elements as $key => $element) {
if($key != 0) // I suppose the trial one is not used
{
if($element['parent_id'] == 0) // a root
{
$closeList = [];
$openList[$element['id']] = $element['id'];
$result[$id] = $element;
unset($result[$id]['parent_id']);
$result[$id]['children'] = [];
$newOpenlist = [];
while(count($openList) > 0)
{
foreach ($elements as $key => $element) {
if($element['parent_id'] != 0 && in_array($element['parent_id'], $openList) && !in_array($element['parent_id'], $closeList))
{
$newOpenlist[$element['id']] = $element['id'];
$result[$id]['children'][] = $element;
}
}
foreach($openList as $item)
{
$closeList[] = $item;
}
$openList = $newOpenlist;
$newOpenlist = [];
}
}
$id++;
}
}
return $result;
}
From your question, I do not clearly understand the logic behind the subscription_name. Regarding the childrens below code might work.
<?php
$array = [
[
'id' => 1,
'subscription_name' => 'Yearly',
'parent_id' => 0
],
[
'id' => 22,
'subscription_name' => 'Yearly new',
'parent_id' => 1
],
[
'id' => 23,
'subscription_name' => 'Yearly new offer',
'parent_id' => 22
],
[
'id' => 24,
'subscription_name' => 'Weekly',
'parent_id' => 0
],
[
'id' => 25,
'subscription_name' => 'Weekly new offer',
'parent_id' => 24
],
];
function find_childrens_parent_not_zero($array) {
foreach($array as $key => $value) {
if($value['parent_id'] != 0) {
if(!is_array($array[$key]['childrens'])) {
$array[$key]['childrens'] = [];
}
foreach($array as $k => $v) {
if($v['parent_id'] == $value['id']) {
array_push($array[$key]['childrens'], array('id' => $v['id']));
unset($array[$k]['parent_id']);
}
}
}
}
return $array;
}
function find_childrens_parent_zero($array) {
foreach($array as $key => $value) {
if($value['parent_id'] == 0) {
if(!is_array($array[$key]['childrens'])) {
$array[$key]['childrens'] = [];
}
foreach($array as $k => $v) {
if($v['parent_id'] == $value['id']) {
array_push($array[$key]['childrens'], array('id' => $v['id']));
unset($array[$k]['parent_id']);
}
}
}
}
return $array;
}
function merge_children($array) {
foreach($array as $key => $value) {
if($value['parent_id'] == 0) {
//pluck childrens of it's children
foreach($value['childrens'] as $k => $v) {
foreach($array as $ke => $val) {
if($v['id'] == $val['id']) {
$array[$key]['childrens'] = array_merge($array[$key]['childrens'], $array[$ke]['childrens']);
}
}
}
}
}
return $array;
}
/**
* Remove parent not zero elements
*/
function cleanup_array($array) {
$result = [];
foreach($array as $key => $value) {
if(array_key_exists('parent_id', $value )) {
unset($value['parent_id']);
array_push($result, $value);
}
}
return $result;
}
echo '<pre>';
$result_parent_not_zero = find_childrens_parent_not_zero($array);
$result_parent_zero = find_childrens_parent_zero($result_parent_not_zero);
$result_merged_children = merge_children($result_parent_zero);
$result_cleaned_up = cleanup_array($result_merged_children);
print_r($result_cleaned_up);
Will give you the result as
<pre>Array
(
[0] => Array
(
[id] => 1
[subscription_name] => Yearly
[childrens] => Array
(
[0] => Array
(
[id] => 22
)
[1] => Array
(
[id] => 23
)
)
)
[1] => Array
(
[id] => 24
[subscription_name] => Weekly
[childrens] => Array
(
[0] => Array
(
[id] => 25
)
)
)
)
<?php
$load[0]['id'] = 0;
$load[0]['subscription_name'] = 'Trial';
$load[0]['parent_id'] = 0;
$load[1]['id'] = 1;
$load[1]['subscription_name'] = 'Yearly';
$load[1]['parent_id'] = 0;
$load[2]['id'] = 2;
$load[2]['subscription_name'] = 'Trial';
$load[2]['parent_id'] = 1;
$load[3]['id'] = 3;
$load[3]['subscription_name'] = 'Yearly';
$load[3]['parent_id'] = 1;
$load[4]['id'] = 4;
$load[4]['subscription_name'] = 'Trial';
$load[4]['parent_id'] = 2;
function tree($load){
$output = [];
foreach($load as $l){
$temp = [];
foreach($load as $ll){
if($ll['parent_id']==$l['id']){
if($l['id']==$ll['id'])
continue;
$temp[] = $ll;
}
}
$l['childrens'] = $temp;
$output[] = $l;
}
return $output;
}
echo "<pre>";
print_r(tree($load));
I need to flatten a PHP array but having some issues getting the desired results.
Array
(
[0] => Array
(
[case_code_id] => 1
[parent_id] => 0
[case_code] => Main A
[sub_codes] => Array
(
[0] => Array
(
[case_code_id] => 3
[parent_id] => 1
[case_code] => Sub A
[sub_codes] => Array
(
[0] => Array
(
[case_code_id] => 5
[parent_id] => 3
[case_code] => Sub Sub A
[sub_codes] => Array
(
)
)
)
)
[1] => Array
(
[case_code_id] => 4
[parent_id] => 1
[case_code] => Sub B
[sub_codes] => Array
(
)
)
)
)
[1] => Array
(
[case_code_id] => 2
[parent_id] => 0
[case_code] => Main B
[sub_codes] => Array
(
)
)
)
But I would like to convert this to the following:
Array
(
[0] => Array
(
[case_code_id] => 1
[parent_id] => 0
[case_code] => Main A
)
[1] => Array
(
[case_code_id] => 3
[parent_id] => 1
[case_code] => Sub A
)
[2] => Array
(
[case_code_id] => 5
[parent_id] => 3
[case_code] => Sub Sub A
)
[3] => Array
(
[case_code_id] => 4
[parent_id] => 1
[case_code] => Sub B
)
[4] => Array
(
[case_code_id] => 2
[parent_id] => 0
[case_code] => Main B
[sub_codes] => Array
)
I have tried several loops but nothing returns the full array.
Here is what I have for my loop:
public function array_flatten($array,$list=array()){
for ($i=0;$i<count($array);$i++) {
$results[] = array(
'case_code_id'=>$array[$i]['case_code_id'],
'case_code'=>$array[$i]['case_code'],
'parent_id'=>$array[$i]['parent_id']
);
if (count($array[$i]['sub_codes']) > 0) {
$this->array_flatten($array[$i]['sub_codes'],$results);
} else {
$results[] = $array[$i];
}
}
return $results;
}
And I'm calling it like this: ($multi contains the multidimensional array)
$flat = $this->array_flatten($multi);
The variable $multi is created from this function:
public function build_case_code_tree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = $this->build_case_code_tree($elements, $element['case_code_id']);
$element['sub_codes'] = $children;
$branch[] = $element;
}
}
return $branch;
}
Any thoughts?
function array_flatten($a, $flat = []) {
$entry = [];
foreach ($a as $key => $el) {
if (is_array($el)) {
$flat = array_flatten($el, $flat);
} else {
$entry[$key] = $el;
}
}
if (!empty($entry)) {
$flat[] = $entry;
}
return $flat;
}
print_r(array_flatten($multi));
You're not using $list anywhere in the code, and nothing is passed by reference.
You're close, but your function should use $list in the place of $results, and it should receive $list by reference and modifying it in place instead of returning it.
Something like this (untested though):
function array_flatten($array,&$list=array()){
for ($i=0;$i<count($array);$i++) {
$list[] = array(
'case_code_id'=>$array[$i]['case_code_id'],
'case_code'=>$array[$i]['case_code'],
'parent_id'=>$array[$i]['parent_id']
);
if (count($array[$i]['sub_codes']) > 0) {
$this->array_flatten($array[$i]['sub_codes'],$list);
} else {
$list[] = $array[$i];
}
}
}
And calling it like this:
$flat = Array();
$this->array_flatten($multi, $flat);
// Result is inside $flat now
I have an input array like this:
Array
(
[one] => one
[two] => two
[group1] => Array
(
[three] => three
[four] => four
[group2] => Array
(
[five] => five
)
)
[group3] => Array
(
[six] => six
)
)
I want to extract following 6 strings from above array:
Array
(
[0] => one
[1] => two
[2] => group1,three
[3] => group1,four
[4] => group1,group2,five
[5] => group3,six
)
Any Idea? Is there any useful PHP function for this?
I have tried something like this:
function getStrings( $data, &$result, $parent = '' ) {
foreach( $data as $key => $value ) {
if( is_array( $value ) ) {
getStrings( $value, $result, $key );
} else {
if( $parent == '' ) {
$result[] = $value;
} else {
$result[] = $parent . ',' . $value;
}
}
}
}
$tree = array();
getStrings( $input, $tree );
print_r( $tree );
Result:
Array
(
[0] => one
[1] => two
[2] => group1,three
[3] => group1,four
[4] => group2,five
[5] => group3,six
)
According to your data structure which is:
$data = [
'one' => 'one',
'two' => 'two',
'group1' => [
'three' => 'three',
'four' => 'four',
'group2' => ['five']
],
'group3' => ['six' => 'six']
];
You can use Recursive function:
function make_implode($data){
$rows = [];
foreach($data as $key => $val) {
if(is_array($val)) {
//$rows[] = $key;
$nestedData = make_implode($val);
if(is_array($nestedData)){
foreach($nestedData as $keyNested => $valNested) {
$rows[] = $key.','.$valNested;
}
} else {
$rows[] = $key.','.$nestedData;
}
} else {
$rows[] = $val;
}
}
return $rows;
}
$data = make_implode($data);
echo '<pre>'.print_r($data,1).'</pre>';
//prints
Array
(
[0] => one
[1] => two
[2] => group1,three
[3] => group1,four
[4] => group1,group2,five
[5] => group3,six
)
use a recursive function, similar to:
$a = array("one"=>"one","group1"=>array("three"=>"three"));
var_dump($a);
function recFor($a) {
foreach($a as $k => $v) {
if(is_array($v)) {
$tmp= recFor($v);
$res[] = $tmp[0];
} else {
$res[] = $v;
}
}
return $res;
}
$b = recFor($a);
var_dump($b);
This is just a quick and dirty example, but you should get the idea.
The first array
Array
(
[0] => Array
(
[1] => 2
)
[1] => Array
(
[1] => 2
)
[2] => Array
(
[2] => 1
)
[3] => Array
(
[3] => 1
)
)
I want output like
Array
(
[0] => Array
(
[1] => 4
)
[1] => Array
(
[2] => 1
)
[2] => Array
(
[3] => 1
)
)
How can i do this?
Seems like a good case for array_reduce():
$res = array_chunk(array_reduce($arr, function(&$current, $item) {
foreach ($item as $key => $value) {
if (!isset($current[$key])) {
$current[$key] = 0;
}
$current[$key] += $value;
}
return $current;
}, []), 1, true);
For the final result I'm using array_chunk(); it takes an array and creates single element sub arrays of each element.
$result = array();
foreach ($input as $subarray) {
foreach ($subarray as $key => $value) {
if (isset($result[$key])) {
$result[$key][$key] += $value;
} else {
$result[$key] = array($key => $value);
}
}
}
$result = array_values($result); // Convert from associative array to indexed array
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