PHP-Smartest Multidimensional array filter - php

I have a multidimensional array as follows
Array
(
[0] => Array
(
[1] => val01
[2] => val02
[3] =>
)
[1] => Array
(
[1] => val11
[2] => Array
(
[sub1] => 1
[sub2] =>
[sub3] => Array
(
[primarysub1] =>
[primarysub2] => pmy2
)
)
[3] => val3
)
[2] => Array
(
[1] => val21
[2] =>
[3] => val23
)
)
And I need to filter the empty values and expected output is as follow.
Array
(
[0] => Array
(
[1] => val01
[2] => val02
)
[1] => Array
(
[1] => val11
[2] => Array
(
[sub1] => 1
[sub3] => Array
(
[primarysub2] => pmy2
)
)
[3] => val3
)
[2] => Array
(
[1] => val21
[3] => val23
)
)
Could somebody help me out with a best way using PHP?

You have to use recursive function for this. hope this might help you
<?php
$rs = unsetValues($arr);
function unsetValues($a)
{
foreach($a as $k=>$v)
{
if(is_array($v))
{
$arr2[$k] = unsetValues($v);
} else {
if($v!="")
$arr2[$k] = $v;
}
}
return $arr2;
}
?>

function filter($input, $callback = null)
{
foreach ($input as &$value)
{
if (is_array($value))
{
$value = filter($value, $callback);
}
}
return array_filter($input, $callback);
}
function remove_empty($val)
{
return !empty($val);
}
$test_arr = array(
0 => array(1 => "val01", 2 => "val02", 3 => ""),
1 => array(1 => "val11", 2 => array("sub1" => 1, "sub2" => "", "sub3" => array("primarysub1" => "", "primarysub2" => "pmy2")), 3 => "val3"),
2 => array(1 => "val21", 2 => "", 3 => "val23")
);
echo '<pre>' . print_r($test_arr, true) . '</pre>';
// filter empty
$result = filter($test_arr, remove_empty);
echo '<pre>' . print_r($result, true) . '</pre>';

Related

compare values from multidimensional array and add key to array

Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
)
)
I want to compare the value of key[0] of each array.
If they are the same then I would like to add a new key[3] to each array with an id.
This is an array of variable products if the product has the same key[0] then its the same product with different variations.
If the key[0] is different from the previous then add id+1, in this example, I would like to end up with:
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
[3] => 1
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
[3] => 2
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
[3] => 3
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
[3] => 3
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
[3] => 3
)
)
can you guys help me with this?
I tried this:
but does not work
foreach ($new as $current_key => $current_array) {
foreach ($new as $search_key => $search_array) {
$ref1 = $current_array[0];
$ref2 = $search_array[0];
if (($search_key != $current_key) and ($ref1 == $ref2)) {
$current_array[3] = $p_id_product;
}
else{
$current_array[3] = $p_id_product++;
}
}
}
Assuming you have already sorted the array by the initial index, so at least they are grouped:
<?php
$data =
[
[
'foo',
'spam',
'bar',
],
[
'foo',
'eggs',
],
[
'bar',
'ham'
],
];
$output = [];
$counter = 0;
$last = null;
foreach($data as $k => $v) {
if($last !== $v[0])
$counter++;
$v[3] = $counter;
$output[$k] = $v;
$last = $v[0];
}
var_export($output);
Output:
array (
0 =>
array (
0 => 'foo',
1 => 'spam',
2 => 'bar',
3 => 1,
),
1 =>
array (
0 => 'foo',
1 => 'eggs',
3 => 1,
),
2 =>
array (
0 => 'bar',
1 => 'ham',
3 => 2,
),
)

How can I create a function to display an array as a tree?

How can I create a function to display an array as a tree. For example I want to obtain a decision tree which I want to walk until I get to the leafs based on the branch's values. I create the tree like bellow:
$tree= new DS_Tree();
$node=array('name' => 'start');
$tree->insert_node($node);
$tree->goto_root();
$mytree = new id3();
$mytree->init($data_array_AttrList,$data_array_values,$data_class,$data_array_instances,$tree);
$mytree->run();
echo '<pre class="brush: php">';
print_r($mytree->tree->draw_tree());
echo '</pre>';
The function draw_tree() is:
public function draw_tree() {
return $this->nodes;
}
The function that creates my tree is:
private function make_tree($attr) {
foreach($this->Values[$attr] as $v) {
$subset = $this->get_subset($attr, $v);
if($the_class = $this->has_same_class($subset)) {
$node =array(
'name' => $attr,
'arc' => $v
);
$this->tree->insert_node($node);
$this->Instance = array_diff_key($this->Instance, $subset);
} else {
$node =array(
'name' => $this->Classa,
'arc' => $v
);
$unresolved = $this->tree->insert_node($node);
}
}
if (isset($unresolved)) {
$this->tree->goto_index($unresolved);
}
}
}
The result is:
Array
(
[0] => Array
(
[name] => Time
[parent] =>
[children] => Array
(
[0] => 1
)
)
[1] => Array
(
[name] => Focus
[arc] => Array
(
[0] => 2 day/week
[1] => 3 day/week
[2] => 4 day/week
[3] => 5 day/week
[4] => 6 day/week
)
[parent] => 0
[children] => Array
(
[0] => 2
)
)
[2] => Array
(
[name] => Dificulty
[arc] => Array
(
[0] => Weght loss
[1] => Mantain weight
[2] => Gain Mass
)
[parent] => 1
[children] => Array
(
[0] => 3
)
)
[3] => Array
(
[name] => Sex
[arc] => Array
(
[0] => Beginner
[1] => Intermediar
[2] => Advance
)
[parent] => 2
[children] => Array
(
[0] => 4
)
)
[4] => Array
(
[name] => Array
(
[Exercise] => Array
(
[0] => Array
(
[0] => Ex1
[1] => Ex2
[2] => Ex3
[3] => Ex4
[4] => Ex5
)
)
)
[arc] => Array
(
[0] => F
[1] => M
)
[parent] => 3
)
)
Just to display an array as a tree:
echo "<pre>";
var_dump($array);
echo "</pre>";
Here is a way to iterate through this data structure and look for a certain value:
function recurseFind($tree, $find, $path = "") {
if (!is_array($tree)) {
// it is a leaf:
if ($tree === $find) {
return $path; // return path where we found it
}
return false;
}
foreach($tree as $key => $value) {
$result = recurseFind($value, $find, $path . (is_numeric($key) ? "[$key]" : "['$key']"));
if ($result !== false) return $result;
}
return false;
}
For the sample input you provided, if you would call it like this:
echo recurseFind($tree, "Mantain weight", '$tree');
Outputs the "location" of that value (first match only) in a format that can be evaluated in PHP:
$tree[2]['arc'][1]

How can i check the unique key and value in this array

Array (
[0] => Array ( [2] => 3 )
[1] => Array ( [12] => 32 )
[2] => Array ( [2] => 3 )
[3] => Array ( [24] => 42 )
);
How can i get the output as unique key value. I need to remove the duplicate values.
I need the o/p like this
Array (
[0] => Array ( [2] => 3 )
[1] => Array ( [12] => 32 )
[2] => Array ( [24] => 42 )
);
array_reduce is likely designed for this purpose:
$a = [ [ 2 => 3 ], [ 3 => 4 ], [ 3 => 15 ], [ 2 => 3 ] ];
array_reduce($a, function($memo, $el) {
if(false === array_search($el, $memo)) array_push($memo, $el);
return $memo;
}, array())
#⇒ array(3) {
# [0] =>
# array(1) {
# [2] =>
# int(3)
# }
# [1] =>
# array(1) {
# [3] =>
# int(4)
# }
# [2] =>
# array(1) {
# [3] =>
# int(15)
# }
#}
Try this:
$array = array(
array(2 => 3),
array(12 => 32),
array(2 => 3),
array(24 => 32)
);
echo 'before array_unique_multi:' . "\n";
print_r($array);
echo 'after array_unique_multi:' . "\n";
print_r(array_unique_multi($array));
function array_unique_multi($array)
{
$hashAry = array();
foreach($array as $k=>$v){
$hash = md5(serialize($v));
if(in_array($hash, $hashAry)) unset($array[$k]);
else $hashAry[]=$hash;
}
$array = array_values($array);
return $array;
}
Output:
before array_unique_multi:
Array
(
[0] => Array
(
[2] => 3
)
[1] => Array
(
[12] => 32
)
[2] => Array
(
[2] => 3
)
[3] => Array
(
[24] => 32
)
)
after array_unique_multi:
Array
(
[0] => Array
(
[2] => 3
)
[1] => Array
(
[12] => 32
)
[2] => Array
(
[24] => 32
)
)
You can use the function array_unique.
http://php.net/manual/fr/function.array-unique.php
foreach ($myMultiArray as $index => $doublonArray) {
$myMultiArray[$index] = array_unique($doublonArray);
}
Use this. This will store all the different key / value pairs in the new array.
$test = array(
array(2 => 3),
array(12 => 32),
array(2 => 3),
array(24 => 42)
);
$tmpArray = array();
$newArray = array();
foreach ($test as $elements) {
foreach ($elements as $key => $val) {
if (!array_key_exists($key, $tmpArray) || $tmpArray[$key] !== $val) {
$tmpArray[$key] = $val;
}
}
}
foreach ($tmpArray as $key => $val) {
$newArray[] = array($key => $val);
}
var_dump($newArray);
Output:
array
0 =>
array
2 => int 3
1 =>
array
12 => int 32
2 =>
array
24 => int 42
$check_arr = Array (
[0] => Array ( [13] => 1 )
[1] => Array ( [13] => 3 )
[2] => Array ( [13] => 1 )
[3] => Array ( [10] => 3 )
) ;
print_r(array_unique($check_arr));
This my result.
Array ( [0] => Array ( [13] => 1 ) );

PHP fetch 3d array

I have below array and i want to fetch [2] => Array with foreach but it's showing me an error.
for example array name is $other
Array
(
[0] => Array
(
[0] => aaaaaaaaaaaa
[1] => bbbbbbbbbbbb
[2] => cccccccccccc
)
[1] => Array
(
[0] => dddddddddddd
[1] => eeeeeeeeeeee
[2] => ffffffffffff
)
[2] => Array
(
[0] => gggggggggggg
[1] => hhhhhhhhhhhh
[2] => iiiiiiiiiiii
)
)
fetch array:
foreach ($other[2] as $value) {
echo $value.'<br/>';
}
How do I print all the values of the second array?
You need to nest further more
foreach ($other as $arr)
{
foreach($arr as $k=>$v)
{
if($k==2)
{
echo $v.'<br/>';
}
}
}
OUTPUT :
cccccccccccc
ffffffffffff
iiiiiiiiiiii
Try Something like this,
<?php
$x = array
(
0 => array
(
0 => 'aaaaaaaaaaaa',
1 => 'bbbbbbbbbbbb',
2 => 'cccccccccccc'
),
1 => array
(
0 => 'dddddddddddd',
1 => 'eeeeeeeeeeee',
2 => 'ffffffffffff'
),
2 => array
(
0 => 'gggggggggggg',
1 => 'hhhhhhhhhhhh',
2 => 'iiiiiiiiiiii'
)
);
$count = count($x);
$w = $count - 1;
var_dump($x[$w]);
?>

how to sum of two multidimensional array?

I am new in php. i need sum of two arrays but not getting right output.my arrays are coming in this way.
this is my first array.
Array
(
[1] => Array
(
[1] => Array
(
[-10] => 21787048.7293
[-5] => 21816115.9548
[-1] => 21839369.7352
[0] => 21845183.1803
[1] => 21850996.6254
[5] => 21874250.4058
[10] => 21903317.6313
)
)
[2] => Array
(
[2] => Array
(
[-10] => 21147607.6407
[-5] => 21496395.4105
[-1] => 21775425.6263
[0] => 21845183.1803
[1] => 21914940.7343
[5] => 22193970.9501
[10] => 22542758.7199
)
)
)
and this is second array.
Array
(
[1] => Array
(
[1] => Array
(
[-10] => 26101989.9443
[-5] => 26131057.1698
[-1] => 26154310.9501
[0] => 26160124.3952
[1] => 26165937.8403
[5] => 26189191.6207
[10] => 26218258.8462
)
)
[2] => Array
(
[2] => Array
(
[-10] => 25462548.8556
[-5] => 25811336.6254
[-1] => 26090366.8413
[0] => 26160124.3952
[1] => 26229881.9492
[5] => 26508912.1651
[10] => 26857699.9349
)
)
)
i need sum of these two arrays by keys.please help me out.
this is my code.my two arrays are $pretax_income, $earning.
foreach($pretax_income as $k=>$value)
{
foreach($value as $v=>$val)
{
foreach($val as $u=>$valArr) {
$comboarray[$elno] = $valArr[$k][$v][$u] + $earning[$k][$v][$u];
}
}
} print_r($comboarray);
function sum()
{
$identicalKeysArrays = func_get_args();
if (is_array($identicalKeysArrays[0])) {
$result = call_user_func_array('sum', array_map(function ($array) {
return $array[0];
}, $identicalKeysArrays));
if ($result) {
return $result;
}
}
if ($identicalKeysArrays[0]) {
$summed = array();
foreach ($identicalKeysArrays as $array) {
foreach ($array as $key => $value) {
$summed[$key] = ($summed[$key] ? $summed[$key] : 0) + $value;
}
}
return $summed;
}
}
$a = array(array(
'-1' => 23,
'2' => 2
));
$b = array(array(
'-1' => 3
));
var_dump(sum($a, $b));
Result:
array(2) { [-1]=> int(26) [2]=> int(2) }
Maybe something like this?
foreach($pretax_income as $key1=>$val1){
foreach($val1 as $key2=>$val2){
foreach($val2 as $key3=>$val3){
$comboarray[$key1][$key2][$key3]=$pretax_income[$key1][$key2][$key3]+$earning[$key1][$key2][$key3];
}
}
}

Categories