PHP Associative Array Duplicate Keys - php

I have an associative array, however when I add values to it using the below function it seems to overwrite the same keys. Is there a way to have multiple of the same keys with different values? Or is there another form of array that has the same format?
I want to have:
42=>56
42=>86
42=>97
51=>64
51=>52
etc etc
Code:
function array_push_associative(&$arr) {
$args = func_get_args();
foreach ($args as $arg) {
if (is_array($arg)) {
foreach ($arg as $key => $value) {
$arr[$key] = $value;
$ret++;
}
}else{
$arr[$arg] = "";
}
}
return $ret;
}

No, you cannot have multiple of the same key in an associative array.
You could, however, have unique keys each of whose corresponding values are arrays, and those arrays have multiple elements for each key.
So instead of this...
42=>56 42=>86 42=>97 51=>64 51=>52
...you have this:
Array (
42 => Array ( 56, 86, 97 )
51 => Array ( 64, 52 )
)

A key is an extension of a variable. If you overwrite the variable... You overwrite the variable.

No, you cannot have. A workaround I use is to have each key/value pair as a new array with 2 elements:
$test = array(
array(42,56),
array(42,86),
array(42,97),
array(51,64),
array(51,52)
)
For example, you can access the second key (=42) using:
$test[1][0]
and the second value(=86) using:
$test[1][1]

I found this question while researching the exact opposite intended outcome, I have an array of data that has duplicate keys! Here's how I did it (still trying to figure out where in my process things are messing up).
$session = time();
$a = array();
$a[(string)$session] = 0;
$j = json_encode($a,JSON_FORCE_OBJECT);
print_r($a);
/* output:
Array
(
[1510768034] => 0
)
*/
var_dump($a);
/* output:
array(1)
(
[1510768034] => int(0)
)
*/
print_r($j);
/* output:
{"1510768034":0}
*/
$a = (array)json_decode($j);
$session = #array_pop(array_keys($a));
$a[(string)$session] = 10;
$j = json_encode($a,JSON_FORCE_OBJECT);
print_r($a);
/* output:
Array
(
[1510768034] => 0
[1510768034] => 10
)
*/
var_dump($a);
/* output:
array(2)
(
'1510768034' => int(0)
[1510768034] => int(10)
)
*/
print_r($j);
/* output:
{"1510768034":0,"1510768034":10}
*/
Yup....that just happened.
PHP 7.1
Edit: It's similar in PHP 7.2.10, except json_encode no longer entertains duplicate keys, encoded strings are correct. The array, however, can have matching string and integer keys.

i had the same need too create an array with the same keys, (just to keep performance by using two loops rather than 4 loops).
by using this : [$increment."-".$domain_id] => $article_id;
my list of articles in each domain looks like this after a print_r() :
$AllSa = Array
(
[1-5] => 143
[2-5] => 176
[3-5] => 992
[4-2] => 60
[5-2] => 41
[6-2] => 1002
[4-45] => 5
[5-45] => 18
[6-45] => 20
)
And then by looping through this table to associate article by domain :
$AssocSAPerDomain = array();
$TempDomain = "";
$TempDomain_first = 0;
foreach($tree_array as $id_domain => $id_sa){
if( !$TempDomain && $TempDomain_first == 0 ){ $TempDomain = substr(strrchr($id_domain, "-"), 1); $TempDomain_first = 1; }
$currentDomain = substr(strrchr($id_domain, "-"), 1);
//if($TempDomain == $currentDomain)
$AssocSAPerDomain[$currentDomain][] = $id_sa;
$TempDomain = substr(strrchr($id_domain, "-"), 1);
}
you get this
$assoc= Array
(
[5] => 143
=> 176
=> 992
[2] => 60
=> 41
=> 1002
[45]=> 5
=> 18
=> 20
)

it is not possible technically. But I created a fun way to do it. Be alert, this answer is just for the fun, the ultimate goal is to get the output anyway.
So here is the answer. You first need to convert to the whole array to the string. Then the rest of the things is just string replace and a little code.
<?php
$ary="array('a'=>123,'a'=>161,'a'=>195)";
$str = str_replace("array(", "", $ary);
$str = str_replace(")", "", $str);
$arr = explode(",",$str);
foreach($arr as $element){
$element = str_replace("=>", "", $element);
$element = str_replace("'", "", $element);
echo $element.'<br>';
}
?>
Here is the output

Related

FOR-EACH control structures with an array display only the last value [duplicate]

I have an associative array, however when I add values to it using the below function it seems to overwrite the same keys. Is there a way to have multiple of the same keys with different values? Or is there another form of array that has the same format?
I want to have:
42=>56
42=>86
42=>97
51=>64
51=>52
etc etc
Code:
function array_push_associative(&$arr) {
$args = func_get_args();
foreach ($args as $arg) {
if (is_array($arg)) {
foreach ($arg as $key => $value) {
$arr[$key] = $value;
$ret++;
}
}else{
$arr[$arg] = "";
}
}
return $ret;
}
No, you cannot have multiple of the same key in an associative array.
You could, however, have unique keys each of whose corresponding values are arrays, and those arrays have multiple elements for each key.
So instead of this...
42=>56 42=>86 42=>97 51=>64 51=>52
...you have this:
Array (
42 => Array ( 56, 86, 97 )
51 => Array ( 64, 52 )
)
A key is an extension of a variable. If you overwrite the variable... You overwrite the variable.
No, you cannot have. A workaround I use is to have each key/value pair as a new array with 2 elements:
$test = array(
array(42,56),
array(42,86),
array(42,97),
array(51,64),
array(51,52)
)
For example, you can access the second key (=42) using:
$test[1][0]
and the second value(=86) using:
$test[1][1]
I found this question while researching the exact opposite intended outcome, I have an array of data that has duplicate keys! Here's how I did it (still trying to figure out where in my process things are messing up).
$session = time();
$a = array();
$a[(string)$session] = 0;
$j = json_encode($a,JSON_FORCE_OBJECT);
print_r($a);
/* output:
Array
(
[1510768034] => 0
)
*/
var_dump($a);
/* output:
array(1)
(
[1510768034] => int(0)
)
*/
print_r($j);
/* output:
{"1510768034":0}
*/
$a = (array)json_decode($j);
$session = #array_pop(array_keys($a));
$a[(string)$session] = 10;
$j = json_encode($a,JSON_FORCE_OBJECT);
print_r($a);
/* output:
Array
(
[1510768034] => 0
[1510768034] => 10
)
*/
var_dump($a);
/* output:
array(2)
(
'1510768034' => int(0)
[1510768034] => int(10)
)
*/
print_r($j);
/* output:
{"1510768034":0,"1510768034":10}
*/
Yup....that just happened.
PHP 7.1
Edit: It's similar in PHP 7.2.10, except json_encode no longer entertains duplicate keys, encoded strings are correct. The array, however, can have matching string and integer keys.
i had the same need too create an array with the same keys, (just to keep performance by using two loops rather than 4 loops).
by using this : [$increment."-".$domain_id] => $article_id;
my list of articles in each domain looks like this after a print_r() :
$AllSa = Array
(
[1-5] => 143
[2-5] => 176
[3-5] => 992
[4-2] => 60
[5-2] => 41
[6-2] => 1002
[4-45] => 5
[5-45] => 18
[6-45] => 20
)
And then by looping through this table to associate article by domain :
$AssocSAPerDomain = array();
$TempDomain = "";
$TempDomain_first = 0;
foreach($tree_array as $id_domain => $id_sa){
if( !$TempDomain && $TempDomain_first == 0 ){ $TempDomain = substr(strrchr($id_domain, "-"), 1); $TempDomain_first = 1; }
$currentDomain = substr(strrchr($id_domain, "-"), 1);
//if($TempDomain == $currentDomain)
$AssocSAPerDomain[$currentDomain][] = $id_sa;
$TempDomain = substr(strrchr($id_domain, "-"), 1);
}
you get this
$assoc= Array
(
[5] => 143
=> 176
=> 992
[2] => 60
=> 41
=> 1002
[45]=> 5
=> 18
=> 20
)
it is not possible technically. But I created a fun way to do it. Be alert, this answer is just for the fun, the ultimate goal is to get the output anyway.
So here is the answer. You first need to convert to the whole array to the string. Then the rest of the things is just string replace and a little code.
<?php
$ary="array('a'=>123,'a'=>161,'a'=>195)";
$str = str_replace("array(", "", $ary);
$str = str_replace(")", "", $str);
$arr = explode(",",$str);
foreach($arr as $element){
$element = str_replace("=>", "", $element);
$element = str_replace("'", "", $element);
echo $element.'<br>';
}
?>
Here is the output

Remove duplicate keys from array? [duplicate]

I have an associative array, however when I add values to it using the below function it seems to overwrite the same keys. Is there a way to have multiple of the same keys with different values? Or is there another form of array that has the same format?
I want to have:
42=>56
42=>86
42=>97
51=>64
51=>52
etc etc
Code:
function array_push_associative(&$arr) {
$args = func_get_args();
foreach ($args as $arg) {
if (is_array($arg)) {
foreach ($arg as $key => $value) {
$arr[$key] = $value;
$ret++;
}
}else{
$arr[$arg] = "";
}
}
return $ret;
}
No, you cannot have multiple of the same key in an associative array.
You could, however, have unique keys each of whose corresponding values are arrays, and those arrays have multiple elements for each key.
So instead of this...
42=>56 42=>86 42=>97 51=>64 51=>52
...you have this:
Array (
42 => Array ( 56, 86, 97 )
51 => Array ( 64, 52 )
)
A key is an extension of a variable. If you overwrite the variable... You overwrite the variable.
No, you cannot have. A workaround I use is to have each key/value pair as a new array with 2 elements:
$test = array(
array(42,56),
array(42,86),
array(42,97),
array(51,64),
array(51,52)
)
For example, you can access the second key (=42) using:
$test[1][0]
and the second value(=86) using:
$test[1][1]
I found this question while researching the exact opposite intended outcome, I have an array of data that has duplicate keys! Here's how I did it (still trying to figure out where in my process things are messing up).
$session = time();
$a = array();
$a[(string)$session] = 0;
$j = json_encode($a,JSON_FORCE_OBJECT);
print_r($a);
/* output:
Array
(
[1510768034] => 0
)
*/
var_dump($a);
/* output:
array(1)
(
[1510768034] => int(0)
)
*/
print_r($j);
/* output:
{"1510768034":0}
*/
$a = (array)json_decode($j);
$session = #array_pop(array_keys($a));
$a[(string)$session] = 10;
$j = json_encode($a,JSON_FORCE_OBJECT);
print_r($a);
/* output:
Array
(
[1510768034] => 0
[1510768034] => 10
)
*/
var_dump($a);
/* output:
array(2)
(
'1510768034' => int(0)
[1510768034] => int(10)
)
*/
print_r($j);
/* output:
{"1510768034":0,"1510768034":10}
*/
Yup....that just happened.
PHP 7.1
Edit: It's similar in PHP 7.2.10, except json_encode no longer entertains duplicate keys, encoded strings are correct. The array, however, can have matching string and integer keys.
i had the same need too create an array with the same keys, (just to keep performance by using two loops rather than 4 loops).
by using this : [$increment."-".$domain_id] => $article_id;
my list of articles in each domain looks like this after a print_r() :
$AllSa = Array
(
[1-5] => 143
[2-5] => 176
[3-5] => 992
[4-2] => 60
[5-2] => 41
[6-2] => 1002
[4-45] => 5
[5-45] => 18
[6-45] => 20
)
And then by looping through this table to associate article by domain :
$AssocSAPerDomain = array();
$TempDomain = "";
$TempDomain_first = 0;
foreach($tree_array as $id_domain => $id_sa){
if( !$TempDomain && $TempDomain_first == 0 ){ $TempDomain = substr(strrchr($id_domain, "-"), 1); $TempDomain_first = 1; }
$currentDomain = substr(strrchr($id_domain, "-"), 1);
//if($TempDomain == $currentDomain)
$AssocSAPerDomain[$currentDomain][] = $id_sa;
$TempDomain = substr(strrchr($id_domain, "-"), 1);
}
you get this
$assoc= Array
(
[5] => 143
=> 176
=> 992
[2] => 60
=> 41
=> 1002
[45]=> 5
=> 18
=> 20
)
it is not possible technically. But I created a fun way to do it. Be alert, this answer is just for the fun, the ultimate goal is to get the output anyway.
So here is the answer. You first need to convert to the whole array to the string. Then the rest of the things is just string replace and a little code.
<?php
$ary="array('a'=>123,'a'=>161,'a'=>195)";
$str = str_replace("array(", "", $ary);
$str = str_replace(")", "", $str);
$arr = explode(",",$str);
foreach($arr as $element){
$element = str_replace("=>", "", $element);
$element = str_replace("'", "", $element);
echo $element.'<br>';
}
?>
Here is the output

Combine Indexed arrays with the same keys

I am using the Flot jQuery plugin to create a graph on how many visitors there have been per platform. I would like to create a 4th line with total visitors, calculated by previously retrieved data.
I need to combine several multi-dimensional Indexed arrays, but not simply merging them recursively. I.E:
$arr1 = [[2016/05/04,2],[2016/05/03,4],[2016/05/02,6]];
$arr2 = [[2016/05/04,1],[2016/05/03,3],[2016/05/02,2]];
$arr3 = [[2016/05/04,6],[2016/05/03,7],[2016/05/02,8]];
The output should be:
$arrTotal = [[2016/05/04,9],[2016/05/03,14],[2016/05/02,16]];
How do I accomplish this in a (fairly) simple way?
First of all, you cannot declare your dates the way you did:
$arr1 = [[2016/05/04,2],[2016/05/03,4],[2016/05/02,6]];
Because it's going to take 2016, divide it by 5 then divide it by 4. You need to put them into quotes.
$arr1 = [['2016/05/04',2],['2016/05/03',4],['2016/05/02',6]];
But to create an associative array, you should do it this way:
$arr1 = array('2016/05/04' => 2, '2016/05/03' => 4, '2016/05/02' => 6);
$arr2 = array('2016/05/04' => 1, '2016/05/03' => 3, '2016/05/02' => 2);
$arr3 = array('2016/05/04' => 6, '2016/05/03' => 7, '2016/05/02' => 8);
Now all you want to do, is loop through each array and sum them up.
$merge = array();
function mergeArray(Array &$merge, Array $array){
// Loop through each key and value
foreach($array as $key => $value)
// Make sure the value is numeric
if(is_numeric($value)){
if(!isset($merge[$key]))
$merge[$key] = $value;
else
$merge[$key] += $value;
}
}
mergeArray($merge, $arr1);
mergeArray($merge, $arr2);
mergeArray($merge, $arr3);
And now if you dump the $merge:
array(3) {
["2016/05/04"]=>
int(9)
["2016/05/03"]=>
int(14)
["2016/05/02"]=>
int(16)
}
Build a method that will sum the values by respecting the keys of existing values.
$arr1 = array('2016/05/04'=>2,'2016/05/03'=>4,'2016/05/02'=>6);
$arr2 = array('2016/05/04'=>1,'2016/05/03'=>3,'2016/05/02'=>2);
$arr3 = array('2016/05/04'=>2,'2016/05/03'=>7,'2016/05/02'=>8);
function array_sum(&$new_arr,$arr) {
foreach ($arr as $date_key => $num_value) {
// initialize date in new array with 0, if not done previously
if (! isset($new_arr[$date_key])) { $new_arr[$date_key] = 0; }
// add number for indexed element of array
$new_arr[$date_key] += $num_value;
}
}
$new_arr = array();
array_sum($new_array,$arr1);
array_sum($new_array,$arr2);
array_sum($new_array,$arr3);
You are trying to sum up every second value from each nested array relatively to their position in the parent array.There's a short and simple solution using array_map, array_sum and array_column functions:
$groupped = array_map(null, $arr1,$arr2,$arr3);
$result = array_map(function($v){
return [$v[0][0], array_sum(array_column($v, 1))];
}, $groupped);
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => 2016/05/04
[1] => 9
)
[1] => Array
(
[0] => 2016/05/03
[1] => 14
)
[2] => Array
(
[0] => 2016/05/02
[1] => 16
)
)

Dynamically create multidimensional array

I am trying to generate a multidimensional array, with a depth equal to the number of matches found in a regular expression. The array keys to be the string value of each match.
For example:
preg_match('/([A-Z])\-?([0-9])\-?([0-9]{1,3})/i', 'A-1-001', $matches);
Returns:
Array (
[0] => A-1-001
[1] => A
[2] => 1
[3] => 001
)
Which I want to convert to:
$foo = array(
'A' => array(
'1' => array(
'001' => array('some', 'information')
)
)
);
So that I can merge it with another multidimensional array like this:
$bar['A']['1']['001'] = array('some', 'other', 'information');
The process needs to handle any number of matches/dimensions.
Below is my current approach. I'm failing to grasp the concept, because this attempt falls way short of my goal.
$foo = array();
$j = count($matches);
for ($i = 1; $i < $j; $i++) {
$foo[ $matches[$i - 1] ] = $matches[$i];
}
/*
$foo's structure becomes:
Array (
[A-1-001] => A
[A] => 1
[1] => 001
)
*/
It's only swapping array keys, and not creating new children arrays I need.
Any suggestions or solutions would be greatly appreciated. Thanks!
With some PHP-Fu:
$matches = array('A-1-001', 'A', 1, '001');
$info = array('some', 'information');
print_r(tree($matches, $info));
function tree($array, $info){
$max = count($array)-1;
$result = array($array[$max] => $info);
for($i=$max-1;$i>0;$result = array($array[$i--] => $result));
return $result;
}
Output:
Array
(
[A] => Array
(
[1] => Array
(
[001] => Array
(
[0] => some
[1] => information
)
)
)
)
Well this could be done in a couple of ways, use of recursion for example. Now I had an other idea in my head. You flip the array with array_reverse. This way we can build from the back to the front.
And then we can setup everything. So the code would be something as followed
$foo = array();
$reversed = array_reverse($matches);
$some_info_array = array('some', 'information');
foreach($reversed as $key) {
if(empty($foo)) {
$foo[$key] = $some_info_array;
} else {
$foo[$key] = $foo;
}
}
code not tested but should give you a nive idea/start
Given the original array:
$original_array = array( 'A-1-001', 'A', '1', '001' );
You can convert it to the format you described like this:
$new_array[ $original_array[1] ] = array( $original_array[2] => $original_array[3] );
This yields
array(1) {
["A"]=>
array(1) {
[1]=>
string(3) "001"
}
}
You can then assign values to that array as you indicated:
$new_array['A']['1']['001'] = array('some', 'information');
You could use a function like this ... it should cover any depth. Bonus is that you can keep passing the value of $tree to more branches and get a single tree with multiple products.
function arrayToTree($array, &$tree, $addlAttribs){
$node = array_shift($array);
if (count($array)){
// this is a branch
$tree[$node] = array();
return arrayToTree($array, $tree[$node]);
} else {
$tree[$node] = $addlAttribs;
return;
}
}
You would use it like
$t = array();
array_shift($matches); // remove the first match "A-1-001"
arrayToTree($matches, $tree, $productInfo);
print_r($tree);

weird php array

my php array looks like this:
Array (
[0] => dummy
[1] => stdClass Object (
[aid] => 1
[atitle] => Ameya R. Kadam )
[2] => stdClass Object (
[aid] => 2
[atitle] => Amritpal Singh )
[3] => stdClass Object (
[aid] => 3
[atitle] => Anwar Syed )
[4] => stdClass Object (
[aid] => 4
[atitle] => Aratrika )
) )
now i want to echo the values inside [atitle].
to be specific i want to implode values of atitle into another variable.
how can i make it happen?
With PHP 5.3:
$result = array_map(function($element) { return $element->atitle; }, $array);
if you don't have 5.3 you have to make the anonymous function a regular one and provide the name as string.
Above I missed the part about the empty element, using this approach this could be solved using array_filter:
$array = array_filter($array, function($element) { return is_object($element); });
$result = array_map(function($element) { return $element->atitle; }, $array);
If you are crazy you could write this in one line ...
Your array is declared a bit like this :
(Well, you're probably, in your real case, getting your data from a database or something like that -- but this should be ok, here, to test)
$arr = array(
'dummy',
(object)array('aid' => 1, 'atitle' => 'Ameya R. Kadam'),
(object)array('aid' => 2, 'atitle' => 'Amritpal Singh'),
(object)array('aid' => 3, 'atitle' => 'Anwar Syed'),
(object)array('aid' => 4, 'atitle' => 'Aratrika'),
);
Which means you can extract all the titles to an array, looping over your initial array (excluding the first element, and using the atitle property of each object) :
$titles = array();
$num = count($arr);
for ($i=1 ; $i<$num ; $i++) {
$titles[] = $arr[$i]->atitle;
}
var_dump($titles);
This will get you an array like this one :
array
0 => string 'Ameya R. Kadam' (length=14)
1 => string 'Amritpal Singh' (length=14)
2 => string 'Anwar Syed' (length=10)
3 => string 'Aratrika' (length=8)
And you can now implode all this to a string :
echo implode(', ', $titles);
And you'll get :
Ameya R. Kadam, Amritpal Singh, Anwar Syed, Aratrika
foreach($array as $item){
if(is_object($item) && isset($item->atitle)){
echo $item->atitle;
}
}
to get them into an Array you'd just need to do:
$resultArray = array();
foreach($array as $item){
if(is_object($item) && isset($item->atitle)){
$resultArray[] = $item->atitle;
}
}
Then resultArray is an array of all the atitles
Then you can output as you'd wish
$output = implode(', ', $resultArray);
What you have there is an object.
You can access [atitle] via
$array[1]->atitle;
If you want to check for the existence of title before output, you could use:
// generates the title string from all found titles
$str = '';
foreach ($array AS $k => $v) {
if (isset($v->title)) {
$str .= $v->title;
}
}
echo $str;
If you wanted these in an array it's just a quick switch of storage methods:
// generates the title string from all found titles
$arr = array();
foreach ($array AS $k => $v) {
if (isset($v->title)) {
$arr[] = $v->title;
}
}
echo implode(', ', $arr);
stdClass requires you to use the pointer notation -> for referencing whereas arrays require you to reference them by index, i.e. [4]. You can reference these like:
$array[0]
$array[1]->aid
$array[1]->atitle
$array[2]->aid
$array[2]->atitle
// etc, etc.
$yourArray = array(); //array from above
$atitleArray = array();
foreach($yourArray as $obj){
if(is_object($obj)){
$atitleArray[] = $obj->aTitle;
}
}
seeing as how not every element of your array is an object, you'll need to check for that.

Categories