I have to compare two 7-dimensional Arrays and for every match i have to count +1 to a new array.
This is the array with user-input, which i want to compare with the "right values"
Array([task] => Array(
[OF] => Array(
[nr] => Array(
[1] => Array(
[pos] => Array(
[0] => Array(
[value] => :
)
)
)
[2] => Array(
[pos] => Array(
[0] => Array(
[value] => +
)
)
)
[3] => Array(
[pos] => Array(
[1] => Array(
[value] => -
)
[2] => Array(
[value] => +
)
)
)
[4] => Array(
[pos] => Array(
[0] => Array(
[value] => -
)
)
)
[5] => Array(
[pos] => Array(
[0] => Array(
[value] => *
)
)
)
[6] => Array(
[pos] => Array(
[0] => Array(
[value] => :
)
)
)
[7] => Array(
[pos] => Array(
[0] => Array(
[value] => *
)
)
)
)
)
),
[TA] => Array(
[nr] => Array(
[1] => Array(
[pos] => Array(
[0] => Array(
[value] => :
)
)
)
[2] => Array(
[pos] => Array(
[0] => Array(
[value] => +
)
)
)
[3] => Array(
[pos] => Array(
[1] => Array(
[value] => -
)
[2] => Array(
[value] => +
)
)
)
[4] => Array(
[pos] => Array(
[0] => Array(
[value] => -
)
)
)
[5] => Array(
[pos] => Array(
[0] => Array(
[value] => *
)
)
)
[6] => Array(
[pos] => Array(
[0] => Array(
[value] => :
)
)
)
[7] => Array(
[pos] => Array(
[0] => Array(
[value] => *
)
)
)
)
)
)
The right values are in the same structure with different values.
Now i have to loop through the arrays and check if the user-input is == the "right value".
Sorry if it's hard to understand. It's slightly hard to explain.
Any and all help is appreciated, thank you all in advance.
EDIT:// i want to know the amount of right values of the 'task' arrays e.g. OF => 12 , TA => 3
If I understood well, what you need is a recursive comparision. Here is my adhoc code, I didn't test it:
function compareArrays($array1, $array2) {
$retval = 0;
for($i = 0; $i < count($array1); $i++) {
// The values are arrays, we need a recursive comparation again
if (is_array($array1[$i]) && is_array($array2[$i])) {
$retval += compareArrays($array1[$i], $array2[$i]);
// The values are equal, we can increase the counter
} else if ($array1[$i] === $array2[$i]) {
$retval++;
// The values are different
} else {
// Is there anything to do?
}
}
return $retval;
}
Related
I have an array with files. And I want to sort them by their min value (I'll explain below).
This is an example of array:
$files = array(
array(
'name' => "DevTools/tools/files/design/js/custom/http_build_query.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "DevTools/tools/files/design/js/custom/Form.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "DevTools/tools/files/design/js/custom/VanillaJS.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/plugins/jquery.js"
),
array(
'name' => "resources/js/plugins/jquery-ui.js"
),
array(
'name' => "resources/js/plugins/popper.js"
),
array(
'name' => "resources/js/plugins/bootstrap-v4/index.js",
'range' => array(
'min' => 800
)
),
array(
'name' => "resources/js/plugins/bootstrap-v4/util.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/plugins/bootstrap-v4/dropdown.js",
'range' => array(
'min' => 200
)
),
array(
'name' => "resources/js/plugins/bootstrap-v4/collapse.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/custom/global.js"
),
array(
'name' => "layouts/cms/.js/+0.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/plugins/module/delete.js"
),
array(
'name' => "resources/js/plugins/module/form.submit.js"
),
array(
'name' => "resources/js/plugins/module/piece.image.js"
),
array(
'name' => "resources/js/plugins/select2.js"
)
);
It's easy to understand. We have names for all files. And some of them have [range][min].
What I need to do is keeping their order, but sorting only files from same folder which have the [range][min], so we reorder these ones by [min].
My code
So I use this function:
usort($files, function (array $a, array $b) {
if (!isset($a['range']) || !isset($b['range'])
|| dirname($a['name']) != dirname($b['name'])
|| $a['range']['min'] == $b['range']['min']) {
return 0;
}
return $a['range']['min'] <=> $b['range']['min'];
});
Which does exactly what I said above. So only those ones are correcly reordered, as you can see here:
[6] => Array
(
[name] => resources/js/plugins/bootstrap-v4/util.js
[range] => Array
(
[min] => 0
)
)
[7] => Array
(
[name] => resources/js/plugins/bootstrap-v4/collapse.js
[range] => Array
(
[min] => 0
)
)
[8] => Array
(
[name] => resources/js/plugins/bootstrap-v4/dropdown.js
[range] => Array
(
[min] => 200
)
)
[9] => Array
(
[name] => resources/js/plugins/bootstrap-v4/index.js
[range] => Array
(
[min] => 800
)
)
Which is great.
The problem
Now I add in my $files array this one:
array(
'name' => "resources/js/plugins/module/piece.images.js"
)
Append it anywhere you want in $files, the usort will reorder entire array with no logic:
Array
(
[0] => Array
(
[name] => DevTools/tools/files/design/js/custom/http_build_query.js
[range] => Array
(
[min] => 0
)
)
[1] => Array
(
[name] => resources/js/plugins/bootstrap-v4/collapse.js
[range] => Array
(
[min] => 0
)
)
[2] => Array
(
[name] => resources/js/plugins/select2.js
)
[3] => Array
(
[name] => resources/js/plugins/module/piece.image.js
)
[4] => Array
(
[name] => resources/js/plugins/module/form.submit.js
)
[5] => Array
(
[name] => resources/js/plugins/module/delete.js
)
[6] => Array
(
[name] => layouts/cms/.js/+0.js
[range] => Array
(
[min] => 0
)
)
[7] => Array
(
[name] => resources/js/plugins/bootstrap-v4/util.js
[range] => Array
(
[min] => 0
)
)
[8] => Array
(
[name] => resources/js/custom/global.js
)
[9] => Array
(
[name] => resources/js/plugins/bootstrap-v4/dropdown.js
[range] => Array
(
[min] => 200
)
)
[10] => Array
(
[name] => DevTools/tools/files/design/js/custom/Form.js
[range] => Array
(
[min] => 0
)
)
[11] => Array
(
[name] => resources/js/plugins/bootstrap-v4/index.js
[range] => Array
(
[min] => 800
)
)
[12] => Array
(
[name] => resources/js/plugins/popper.js
)
[13] => Array
(
[name] => resources/js/plugins/jquery-ui.js
)
[14] => Array
(
[name] => resources/js/plugins/jquery.js
)
[15] => Array
(
[name] => DevTools/tools/files/design/js/custom/VanillaJS.js
[range] => Array
(
[min] => 0
)
)
[16] => Array
(
[name] => resources/js/plugins/module/piece.images.js
)
)
I have no idea why is that happening. If you can find out why, can you please see if there is a solution please?
If I didn't explain well, please ask in comments for clarification.
So, what I need to do is keeping their order, but sorting only files from same folder which have the [range][min], so we reorder these ones by [min].
Array ( [0] => Array (
[date] => 01-06-2018
[nav] => 30.65100 )
[1] => Array (
[date] => 31-05-2018
[nav] => 30.84900 )
[2] => Array (
[date] => 30-05-2018
[nav] => 30.73200 )
[3] => Array (
[date] => 29-05-2018
[nav] => 30.81500 )
The above code is the Multi-array, we have added a common id like id_code = 0089 to every array in it without using any loops in PHP. Can anyone helps me and it is possible or not .....?
If you mean not manually loop through the array then yes, it is possible using array_walk:
$array = [
0 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
1 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
2 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
3 => [
"date" =>"01-06-2018",
"nav" => "30.65100"]
];
array_walk($array, function(&$item1) {
$item1['id_code'] = "0089";
});
print_r($array);
Output:
Array
(
[0] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[1] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[2] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[3] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
)
Demo https://3v4l.org/lCGIO
An effective solution will be to use array_map function as:
$keyValue = 'some value';
$data = array_map(function($d) use ($keyValue){
return $d + ['keyName' => $keyValue];
}, $data);
I think this is the closer you can get, but it is probably not your expected result. The exact result you need cannot be done without using a loop as far as I know.
$t = array( 0 => array( 'date' => '01-06-2018', 'nav' => '30.65100' ), 1 => array( 'date' => '31-05-2018', 'nav' => '30.84900' ), 2 => array( 'date' => '30-05-2018', 'nav' => '30.73200' ), 3 => array( 'date' => '29-05-2018', 'nav' => '30.81500' ));
$tt = array( 0 => array( 'id' => '648'), 1 => array( 'id' => '332'), 2 => array( 'id' => '889'), 3 => array( 'id' => '285') );
$final = array_map(null, $t, $tt);
print_r($final);
The output will look like
Array
(
[0] => Array
(
[0] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
)
[1] => Array
(
[id] => 648
)
)
[1] => Array
(
[0] => Array
(
[date] => 31-05-2018
[nav] => 30.84900
)
[1] => Array
(
[id] => 332
)
)
[2] => Array
(
[0] => Array
(
[date] => 30-05-2018
[nav] => 30.73200
)
[1] => Array
(
[id] => 889
)
)
[3] => Array
(
[0] => Array
(
[date] => 29-05-2018
[nav] => 30.81500
)
[1] => Array
(
[id] => 285
)
)
)
Assuming I have an array and recursively want to do some task. I have spent one day for this unable to solve this. May be my logic is not good.
Any help on how to do such thing in an efficient way would save my days.
I have an array with n level deep, looking like:
Array
(
[0] => Array
(
[name] => user1
[email] => user1#demo.com
[depth] => 1
)
[1] => Array
(
[name] => user2
[email] => user2#demo.com
[depth] => 1
[0] => Array
(
[0] => Array
(
[name] => user2.1
[email] => user2.1#demo.com
[depth] => 2
)
)
)
[2] => Array
(
[name] => user3
[email] => user3#demo.com
[depth] => 1
[0] => Array
(
[0] => Array
(
[name] => user3.1
[email] => user3.1#demo.com
[depth] => 2
[0] => Array
(
[0] => Array
(
[name] => user3.1.1
[email] => user3.1.1#demo.com
[depth] => 3
)
)
)
[1] => Array
(
[name] => user3.2
[email] => user3.2#demo.com
[depth] => 2
)
)
)
)
I want to change above array in exactly this format:
array(
0 => array(
'name' => 'user1',
),
1 => array(
'name' => 'user2',
'children' => array(
0 => array(
'name' => 'user2.1',
) ,
) ,
) ,
2 => array(
'name' => 'user3',
'children' => array(
0 => array(
'name' => 'user3.1',
'children' => array(
0 => array(
'name' => 'user3.1.1',
) ,
) ,
) ,
1 => array(
'name' => '3.2',
)
) ,
) ,
)
Edited:
I am using this code and working fine if i want to show data in tree format but unable to push data into array as i want.
function displayArrayRecursively($arr, $indent='') {
if ($arr) {
foreach ($arr as $key => $value) {
if (is_array($value)) {
displayArrayRecursively($value, $indent . '-->');
} else {
if ($key == 'name')
{
echo "$indent $value <br>";
}
else {
continue;
}
}
}
}
}
displayArrayRecursively($userListArray);
Can anyone suggest me how to do this?
Thank you
This function will do what you want:
function process_nodes($nodes) {
$new = array();
foreach ($nodes as $node) {
$new[] = array('name' => $node['name']);
if (isset($node[0])) {
$new[count($new)-1]['children'] = process_nodes($node[0]);
}
}
return $new;
}
print_r(process_nodes($data));
Output:
Array
(
[0] => Array
(
[name] => user1
)
[1] => Array
(
[name] => user2
[children] => Array
(
[0] => Array
(
[name] => user2.1
)
)
)
[2] => Array
(
[name] => user3
[children] => Array
(
[0] => Array
(
[name] => user3.1
[children] => Array
(
[0] => Array
(
[name] => user3.1.1
)
)
)
[1] => Array
(
[name] => user3.2
)
)
)
)
i want to get multiple value of multi array to put in one array
i creat this code but is doesn't work
it just push the last value the full array
the result must be
$rest=Array ( [OPEN] => Array ( ), [HAPPY] => Array ( ) , [ALIVE] => Array ( ) ,[GOOD] => Array ( ) )
<?
error_reporting(0);
$arr=array(
0 => array(
'OPEN' => 'understanding',
'HAPPY' => 'great',
'ALIVE' => 'playful',
'GOOD' => 'calm'
),
1 => array(
'OPEN' => 'confident',
'HAPPY' => 'gay',
'ALIVE' => 'courageous',
'GOOD' => 'peaceful'
),
2 => array(
'OPEN' => 'reliable',
'HAPPY' => 'joyous',
'ALIVE' => 'energetic',
'GOOD' => 'at ease'
));
$feeling = array();
$x=0;
foreach ($arr[0] as $key => $value) {
$rest = array_merge($feeling,array("$key" => array()));
}
//$rest=Array ( [OPEN] => Array ( ) ) Array ( [HAPPY] => Array ( ) ) Array ( [ALIVE] => Array ( ) ) Array ( [GOOD] => Array ( ) )
for ($i=0; $i <count($arr) ; $i++) {
foreach ($arr[$i] as $key => $value) {
array_push($rest[$key], $arr[$i][$key]);
}
}
print_r($rest);
?>
-----
result what is give me
-----
Array
(
[GOOD] => Array
(
[0] => calm
[1] => peaceful
[2] => at ease
)
[OPEN] =>
[HAPPY] =>
[ALIVE] =>
)
----
i want is like that
----
Array
(
[GOOD] => Array
(
[0] => calm
[1] => peaceful
[2] => at ease
)
[OPEN] =>Array
(
[0] => understanding
[1] => confident
[2] => reliable
)
[HAPPY] =>Array
(
[0] => great
[1] => gay
[2] => joyous
)
[ALIVE] =>Array
(
[0] => playful
[1] => courageous
[2] => energetic
)
)
I believe this is returning the desired output?
$arr=array(
0 => array(
'OPEN' => 'understanding',
'HAPPY' => 'great',
'ALIVE' => 'playful',
'GOOD' => 'calm'
),
1 => array(
'OPEN' => 'confident',
'HAPPY' => 'gay',
'ALIVE' => 'courageous',
'GOOD' => 'peaceful'
),
2 => array(
'OPEN' => 'reliable',
'HAPPY' => 'joyous',
'ALIVE' => 'energetic',
'GOOD' => 'at ease'
)
);
$output=array();
$keys=array_keys( $arr[0] );
for( $i=0; $i < count( $arr ); $i++ ){
foreach( $keys as $key )$output[$key][]=$arr[$i][$key];
}
echo '<pre>', print_r($output,true), '</pre>';
The result:
Array
(
[OPEN] => Array
(
[0] => understanding
[1] => confident
[2] => reliable
)
[HAPPY] => Array
(
[0] => great
[1] => gay
[2] => joyous
)
[ALIVE] => Array
(
[0] => playful
[1] => courageous
[2] => energetic
)
[GOOD] => Array
(
[0] => calm
[1] => peaceful
[2] => at ease
)
)
If I don't understood your requirement then this solution will work for you.I used the array_column() approach because your current code with for() and foreach() loop seems little bit messy.
$arr=array(
0 => array(
'OPEN' => 'understanding',
'HAPPY' => 'great',
'ALIVE' => 'playful',
'GOOD' => 'calm'
),
1 => array(
'OPEN' => 'confident',
'HAPPY' => 'gay',
'ALIVE' => 'courageous',
'GOOD' => 'peaceful'
),
2 => array(
'OPEN' => 'reliable',
'HAPPY' => 'joyous',
'ALIVE' => 'energetic',
'GOOD' => 'at ease'
));
$expected = [];
$keys = array_keys($arr[0]);
foreach($keys as $key){
$expected[$key] = array_column($arr,$key);
}
print '<pre>';
print_r($expected);
print '</pre>';
Output:
Array
(
[OPEN] => Array
(
[0] => understanding
[1] => confident
[2] => reliable
)
[HAPPY] => Array
(
[0] => great
[1] => gay
[2] => joyous
)
[ALIVE] => Array
(
[0] => playful
[1] => courageous
[2] => energetic
)
[GOOD] => Array
(
[0] => calm
[1] => peaceful
[2] => at ease
)
)
I would like remove all keys [Name] but the main problem is the number in the list key ([List1],[List2] etc.). The numbers at key [List] may be more but for example I gave only two.
I would like to change this because it is an old json file and in the new one it doesn't have a key, like a converter
Is there a way to go across the entire array and remove all [Name] keys?
Array(
[Values] => 1
[List1] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
[Name] => Nm1
)
)
[1]=> Array(
[Properties] => Array(
[Id] => 1
[Name] => Nm1
)
)
)
[List1] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
[Name] => Nm1
)
)
)
)
[List2] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
[Name] => 0
)
)
)
)
)
[List2] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
[Name] => Nm1
)
)
)
[List1] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
[Name] => Nm1
)
)
)
)
)
)
My goal is:
Array(
[Values] => 1
[List1] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
)
)
[1]=> Array(
[Properties] => Array(
[Id] => 1
)
)
)
[List1] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
)
)
)
)
[List2] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
)
)
)
)
)
[List2] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
)
)
)
[List1] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
)
)
)
)
)
)
I tried:
$ProductCount= count($array['List1']['Product1']);
for($i = 0;$i<$ProductCount;$i++){
unset($array['List1']['Product1'][$i][Properties][Name]);
}
But I also have a key[List2] and can be [List3] etc.
You can do it like this:
function remove_key($array, $key)
{
foreach($array as $k => $v) {
if(is_array($v)) {
$array[$k] = remove_key($v, $key);
} elseif($k == $key) {
unset($array[$k]);
}
}
return $array;
}
$array = remove_key($array, 'Name');
$array is your multidimensional array and $key is the key name that you want to remove.
NOTE: If the key represents an array (not a value) this method will ignore it but I assume that this is what you need. If you want to remove arrays if the key matches you need to switch the conditions order in the foreach loop.