Find duplicate key in key/value pair - php

I'm looking for some help finding the best solution to the following problem.
I have several php arrays in their own files looking like this:
<?php
$language_array = Array(
'key'=>'value',
'key'=>'value'
);
?>
What I'm trying to do is get a list of duplicate keys which exists in all the php array files.
I though about checking the first file and first key, and check this against all the other files. Then jump to the next key and continue.
So that I might end up with a list like this:
key = 'key' exist in all files
But I'm not sure about the performance, if I have to open each file x-times I have keys in it.
Any help much appreciated.
EDIT:
I solved it now using a temp array, and then comparing the current key with the temp, and if i have seen it already, add it to an array with just the duplicates.
Performance wise I'm not sure if this is the best solution, but using array_intersect was not working for me as I have no master array with all key=>values

$aOne = Array(
'key1' =>'value1',
'key2' =>'value2',
'key3' =>'value3',
'keyunique1' =>'uniquevalue1'
);
$aTwo = Array(
'key2' =>'value2',
'key3' =>'value3',
'keyunique2' =>'uniquevalue2'
);
$aThree = Array(
'key1' =>'value1',
'key2' =>'value2',
'key3' =>'value3',
'keyunique3' =>'uniquevalue3'
);
$aFour = Array(
'key2' =>'value2',
'keyunique4' =>'uniquevalue4'
);
// Expected result key2 being the only one across all arrays.
$aUniques = array_intersect_key( $aOne, $aTwo, $aThree, $aFour );

Related

Is it possible to get value from specified key in the same array?

I wonder if I can select any key inside my array, and set it as the value of another key. in order to be more clear (because my question may not clear enough), I try to do something like this:
$variable = array(
'key' => 'value',
'key2' => $variable['key']
);
As you can see, it won't work (unless I do something like: $variable['key2'] = $variable['key'] out of the array, but this is not what i'm looking for and i'll use it only if I won't be able at all to do it inside the same array).
I searched for any solution but still haven't found one...
There is any way to do such thing inside the same array?
Thank you very much
This way you can`t do it, because this key does not exist yet.
Why to store two same variables in same array? Maybe show us what you are trying to do in bigger picture, so we can help you some way.
Think of it as the code doing what is in the parenthesis FIRST. Since $variable isn't set yet, you'll get an error on $variable['key'], since $variable isn't an array yet.
You must set $variable before
See
$variable = new array();
$variable['key'] = $variable->key2 = 'value';
Also
//create array
$variable = array(
'key' => 'value'
);
//then override
$variable = array(
'key2' => $variable['key'],
'key' => 'new_value'
);

PHP multiple values in one key of associative Array

I stuck with a problem: I have an array with IDs and want to assign theses IDs to a key of a associative array:
$newlinkcats = array( 'link_id' => $linkcatarray[0], $linkcatarray[1], $linkcatarray[2]);
this works fine, but I don't know how many entries in $linkcatarray. So I would like to loop or similar. But I don't know how.
no push, cause it is no array
no implode, cause it is no string
no =, cause it overrides the value before
Could anyone help?
Thanks
Jim
Why not just implode it ?
$newlinkcats = array(
'link_id' => implode(
',',
$linkcatarray
)
);
Or just do this:
// Suggested by Tularis
$newlinkcats = array(
'link_id' => $linkcatarray
);
If your $linkcatarray array is only comprised of the values you wish to assign to the link_id key, then you can simply point the key at that array:
$newlinkcats = array('link_id' => $linkcatarray);
If that array contains more values that you don't want included, then take a look at array_slice() to only grab the indexes you need:
// Grabs the first 3 values from $linkcatarray
$newlinkcats = array('link_id' => array_slice($linkcatarray, 0, 3));
If your desired indexes aren't contiguous, it may be easier to cherry-pick them and use a new array:
$newlinkcats = array('link_id' => array(
$linkcatarray[7],
$linkcatarray[13],
$linkcatarray[22],
// ...
));

Merge/update associative arrays with overlapping keys

I have two associative arrays with string keys, like this:
$base_array = array(
'foo' => '42',
'bar' => '13');
and
$update_array = array(
'bar' => '14',
'blah' => '3.1415');
Question 1: I want to update my $base_array with data from $update_array in such a way that:
If a key is only in $base_array, but not in $update_array, its value be kept intact;
If a key is in both arrays, its value be updated from $update_array;
If a key is only in $update_array, both the key and its value be copied into $base_array.
Is there a short way to accomplish this? Any hint or piece of code is very welcome.
Question 2: Besides this, is there a quick way to visualize a joint list of keys from both arrays, without duplicates? Just keys, no values.
Question 1:
That is exactly what array_merge() does:
$new_array = array_merge($base_array,$update_array);
Question 2:
To get an array of the unique keys, you can merge the arrays and then use array_keys():
$keys = array_keys(array_merge($base_array,$update_array));

Call an array element before its declared

Sorry for this question but I just curious with this case. Okay for example I have an array elements :
<?php
$data = array(
'key1' => 'val1',
'key2' => 'val2',
'key3' => $data['key2'] //the point
);
?>
I know it will get an error because I called an element whereas an array has not declared yet. But is it possible to do that? The fact, value for 'key2' is dynamically.
No, but you can easily do this:
$data = [
"key1"=>"val1",
"key2"=>"val2"
];
$data["key3"] = $data["key2"];
Or even $data["key3"] = &$data["key2"]; to link them by reference.
PHP processes that in this order:
Create the array with its initialized values.
Assign the array to variable $data.
So no, you can't do it in one line. Of course, you can assign 'val2' to a variable, then assign to both keys.

Array filter php multi dimensional array

Is there a built in array method in php to filter a nested associative array?
As an example:
$myArray = array(
array('key1' => ''),
array('key1' => 'value 1'),
array('key1' => 'value 2'),
);
I want to remove any with and empty value - in this example the first element.
I know array_filter would do something similar with a flat array but cant find anything apart from looping over and creating my own new array. If that is the best solution then thats ok, i can do that myself. I just didnt want to possibly overlook a built in method for this.
$myArray = array_filter($myArray, function($el){ return !empty($el['key1']); });
There are native PHP functions you can use to do this, which is a bit simpler:
remove all the keys from nested arrays that contain no value, then
remove all the empty nested arrays.
$postArr = array_map('array_filter', $postArr);
$postArr = array_filter( $postArr );

Categories