How can I merge two different multidimensional arrays but copy the keys and values from the first array to the second array?
How I'm trying to get it to work is; the first array sets the input fields, which are then cloneable (cloning using jQuery, exactly like this: http://jsfiddle.net/8M98y/). Once you save field data, the second array is the resulting output. The problem is, the second array lacks the specific keys and values from the first array that are needed to output the saved data correctly.
Is there was a way to join or merge the arrays while copying the keys and values from the first array into the second array?
The first array that I need to copy the keys/values from looks like this:
Array
(
[group-1] => Array
(
[fields] => Array
(
[text-field-1] => Array
(
[name] => Text Field 1
[value] => Value 1
[comments] => true
)
[text-field-2] => Array
(
[name] => Text Field 2
[value] => Value 2
[comments] => false
)
)
)
)
The second array looks like this:
Array
(
[group-1] => Array
(
[fields] => Array
(
[text-field-1] => New value here
[text-field-2] => New value also
)
)
[group-2] => Array
(
[fields] => Array
(
[text-field-1] => Cloned group with new value
[text-field-2] => Cloned group with new value also
)
)
)
So if these two arrays were able to be merged, would need the output of the merged array to look like this: http://pastebin.com/uzuZs73B
I've tried using just array_merge( $array2, $array1 ), but the resulting output looks like this: http://pastebin.com/DucKGMN3 where they are in fact merged, but the keys and values aren't copied over.
EDIT: Should describe a use case here.
So how this works, the initial, unsaved output is two text inputs in a group which is created by the first array. The group is cloneable, using jQuery clone (this example here: http://jsfiddle.net/8M98y/). So if you add/clone one of more groups and then save, the resulting saved data would be the second array. The strings in the second array is the actual saved input values. Those would go into [value] in the first array.
However, the output of the fields is still based on the first array, meaning it can't output the cloned groups correctly as they're not an array and don't have the same keys and values from the first array.
If anyone can provide some insight on this, it would be hugely and greatly appreciated.
Sorry if I misunderstood the question, but is merging a requirement? If you have access to both arrays you could iterate over the second array, mapping the original key values, and overwriting with the new values as you go.
Your arrays:
$arr1 = Array
(
'group-1' => Array
(
'fields' => Array
(
'text-field-1' => Array
(
'name' => 'Text Field 1',
'value' => 'Value 1',
'comments' => 'true'
),
'text-field-2' => Array
(
'name' => 'Text Field 2',
'value' => 'Value 2',
'comments' => 'false'
)
)
)
);
$arr2 = Array
(
'group-1' => Array
(
'fields' => Array
(
'text-field-1' => 'New value here',
'text-field-2' => 'New value also'
)
),
'group-2' => Array
(
'fields' => Array
(
'text-field-1' => 'Cloned group with new value',
'text-field-2' => 'Cloned group with new value also'
)
)
);
Secret sauce:
foreach($arr2 as $k=>$v){
// Get the new values for this iteration
$val1 = $arr2[$k]['fields']['text-field-1'];
$val2 = $arr2[$k]['fields']['text-field-2'];
// Duplicate the original array
$arr2[$k]['fields'] = $arr1['group-1']['fields'];
// Insert the new values
$arr2[$k]['fields']['text-field-1']['value'] = $val1;
$arr2[$k]['fields']['text-field-2']['value'] = $val2;
}
echo '<pre>';
print_r($arr2);
echo '</pre>';
exit();
Which returns:
Array
(
[group-1] => Array
(
[fields] => Array
(
[text-field-1] => Array
(
[name] => Text Field 1
[value] => New value here
[comments] => true
)
[text-field-2] => Array
(
[name] => Text Field 2
[value] => New value also
[comments] => false
)
)
)
[group-2] => Array
(
[fields] => Array
(
[text-field-1] => Array
(
[name] => Text Field 1
[value] => Cloned group with new value
[comments] => true
)
[text-field-2] => Array
(
[name] => Text Field 2
[value] => Cloned group with new value also
[comments] => false
)
)
)
)
Related
I am using aws redis cache for quicker results instead of saving in db.
With this method
$result = $client->listTagsForResource([
'ResourceName' => '<string>', // REQUIRED
]);
Now it gives me result in given format.
Array
(
[0] => Array
(
[Key] => key1
[Value] => string1
)
[1] => Array
(
[Key] => status
[Value] => 1
)
)
I am unable to find a function in amazon docs which can give me direct results, so I decided to search in array , but finding in very large array with loops cost me in terms of time. So is there a way to convert it in following
Array
(
[key1] => string1,
[status] => 1
)
So I can directly access array index by using $array['key1']
You can try something like this to create new array:
$newArray = array_combine(
array_column($array, 'Key'),
array_column($array, 'Value')
);
echo $newArray['status'];
I want to create and initialize a multidimensional array with known possible keys for the second dimension but no values.
This array will store event_ids (populated dynamically) and for each event_id an array having exactly four different counts (also filled dynamically).
Structure I want to create
Array
(
[0] => Array =================> This index will be the event_id
(
[invitation_not_sent_count] =>
[no_response_yet_count] =>
[will_not_attend_count] =>
[will_attend_count] =>
)
)
What I did so far?
$DataArray = array();
$DataArray[] = array('invitation_not_sent_count' => '',
'no_response_yet_count' => '',
'will_not_attend_count' => '',
'will_attend_count' => '');
And inside the loop I am populating data dynamically like:
$DataArray[$result->getId()]['no_response_yet_count'] = $NRCount;
What I get is:
Array
(
[0] => Array
(
[invitation_not_sent_count] =>
[no_response_yet_count] =>
[will_not_attend_count] =>
[will_attend_count] =>
)
[18569] => Array
(
[no_response_yet_count] => 2
)
[18571] => Array
(
[no_response_yet_count] => 1
)
)
What I want is that if a value is not available in the iteration, its entry should be empty as defined at initialization time. So if all other counts are empty in the data except no_response_yet_count, the array should be:
Expected Output
Array
(
[18569] => Array
(
[invitation_not_sent_count] =>
[no_response_yet_count] => 2
[will_not_attend_count] =>
[will_attend_count] =>
)
[18571] => Array
(
[invitation_not_sent_count] =>
[no_response_yet_count] => 1
[will_not_attend_count] =>
[will_attend_count] =>
)
)
I usually resort to a mapping function at that point:
function pre_map(&$row) {
$row = array
(
'invitation_not_sent_count' => '',
'no_response_yet_count' => '',
'will_not_attend_count' => '',
'will_attend_count' => ''
);
}
Then in the while/for loops:
{
$id = $result->getId();
if (!isset($DataArray[$id])) { pre_map($DataArray[$id]); }
$DataArray[$id]['no_response_yet_count'] = $NRCount;
}
The if (!isset($DataArray[$id])) is to make sure it doesn't wipe out the same index row, in case you happen to re-loop on the same ID. Thus, it will only map it once, then never again in the loop.
There are some other one-line shortcuts like maybe even using array_map(), but I was showing the long version, for full flexibility just in case.
I'm having an issue with an array, I wish to put 2 elements into my array together in the same element within the array but keep the default index. Currently I have this in my array :
Array (
[0] => 2
[1] => MongoId Object ( [$id] => 57b99696ce2350100b000029 )
[2] => 1
[3] => MongoId Object ( [$id] => 57b998ccce2350181700002b )
[4] => 1
[5] => MongoId Object ( [$id] => 57b99a84ce2350100b00002b ) )
and I wish to have something like this, I think:
Array (
[0] => [number]=>2, MongoId Object ( [$id] => 57b99696ce2350100b000029 )
[1] => [number]=>1, MongoId Object ( [$id] => 57b998ccce2350181700002b )
[2] => [number]=>1,MongoId Object ( [$id] => 57b99a84ce2350100b00002b )
)
I wish to retain the default key as well as have two other values, I have tried a few methods but none resulted in the elements being paired together as I want:
Currently (above) I tried :
$array = array_merge( $array,array( $number,$doc[_id] ) );
I also tried :
$array = array_merge( $array,array( $number=>$doc[_id] ) );
and:
array_push($array, $doc[_id], $number);
//asked here on SO earlier but this adds onto the end of an array both elements rather than adding both together into a single element
Can anyone advise the correct way to add two elements as a pair together whilst maintaining the default key/index value of the array.
You can just append an array at the end containing the two elements
$array[] = array($n, $obj);
Or if you want to have it indexed by some name instead
$array[] = array('number' => $n, 'mongoid' => $obj);
If you wish to play with array functions:
array_push($array, [$number, $doc[_id]]);
I have the follow array:
$arrIni["ENV"]="US";
$arrIni["sap_db_server"] = "192.xxx.x.xx";
$arrIni["local_db_server"] = "localhost";
$arrIni["local_db_username"] = "root";
//Default settings
$arrIni["arrEnvSettings"]["UserTypeID"]=4;
$arrIni["arrEnvSettings"]["LocalizationID"]=1;
$arrIni["arrEnvSettings"]["LangLabels"] = array();
$arrIni["arrEnvSettings"]["pages"]["st1"]="st1.php";
$arrIni["arrEnvSettings"]["pages"]["st2"]="st2.php";
$arrIni["arrEnvSettings"]["pages"]["st3"]="st3.php";
And I want to merge with this one:
$setParam["arrEnvSettings"]["pages"]["st3"]="st3_V2.php";
This is what I am doing:
echo "<pre>";
print_r(array_merge($arrIni,$setParam));
echo "</pre>";
And this is what I am getting:
Array
(
[ENV] => US
[sap_db_server] => 192.xxx.x.xx
[local_db_server] => localhost
[local_db_username] => root
[arrEnvSettings] => Array
(
[pages] => Array
(
[st3] => st3_V2.php
)
)
)
In the php doc about merge, this is the comment " ...If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. ..."
So in this way, I suppose to get this output instead of the last one:
Array
(
[ENV] => US
[sap_db_server] => 192.xxx.x.xx
[local_db_server] => localhost
[local_db_username] => root
[arrEnvSettings] => Array
(
[UserTypeID] => 4
[LocalizationID] => 1
[LangLabels] => Array
(
)
[pages] => Array
(
[st1] => st1.php
[st2] => st2.php
[st3] => st3_V2.php
)
)
)
I do not understand why $setParam["arrEnvSettings"]["pages"]["st3"] is overriding the entire $arrIni["arrEnvSettings"].
Note:
If I use array_merge_recursive($arrIni,$setParam)) I will have the follow result but it is not what I want.
Array
(
[ENV] => US
[sap_db_server] => 192.xxx.x.xx
[local_db_server] => localhost
[local_db_username] => root
[arrEnvSettings] => Array
(
[UserTypeID] => 4
[LocalizationID] => 1
[LangLabels] => Array
(
)
[pages] => Array
(
[st1] => st1.php
[st2] => st2.php
[st3] => Array
(
[0] => st3.php
[1] => st3_V2.php
)
)
)
)
Is there a way to do this without iterate over the array? Only using merging? What am I doing wrong?
This should do the trick:
array_replace_recursive($arrIni,$setParam);
If you want to merge a given value, use this:
$arrIni["arrEnvSettings"]["pages"]["st3"] = $setParam["arrEnvSettings"]["pages"]["st3"];
But the way you're doing it is merging two arrays, not simply setting the value within an array. There's a gigantic difference between those two methods.
Otherwise, yes you will need to iteratively merge the arrays.
what you need is array_replace_recursive
print_r(array_replace_recursive($arrIni,$setParam));
didnt see the submitted answer..Felippe Duarte has given it already.....
I have this array:
Array
(
[0] => Array
(
[date] => 2016-03-08
[value] => Array
(
[key_1] => Array
(
[test_1] => 1
[test_2] => 10
[test_3] => 1000
[test_4] => 200
)
[key_2] => Array
(
[test_1] => 1
[test_2] => 15
[test_3] => 1500
[test_4] => 100
)
)
)
Now I have another array :
Array
(
[key_3] => Array
(
[test_1] =>
[test_2] =>
[test_3] =>
[test_4] => 1
)
)
I want to add this last array in the first array.
I try like this : array_push($ymlParsedData[]['value'], $a_big_gift); but not work. Can you help me please ?
You can't use $ymlParsedData[] for accessing specific element, it is a shorthand for pushing data to array.
You can use either
// NB! array_push() just adds the values, key 'key_3' is removed
array_push($ymlParsedData[0]['value'], $a_big_gift);
or
// will keep key 'key_3'
$ymlParsedData[0]['value']['key_3'] = $a_big_gift['key_3'];
or
// use array_merge() instead
$ymlParsedData[0]['value'] = array_merge($ymlParsedData[0]['value'], $a_big_gift);
A complicated answer, but this might solve your issue:
$key_name = array_keys($a_big_gift)[0];
$ymlParsedData[0]['value'][$key_name] = $a_big_gift[$key_name];
echo '<pre>'; print_r($ymlParsedData); exit;
Note: For making it dynamic and for more than one value of $a_big_gift, you need to loop it and achieve your result.
Try this
array_push($ymlParsedData[0]['value'], $a_big_gift['key_3']);