I am making a mailing system, and my input is array of arrays, i need to combine them to one, i already aggregated based on the email.
input example:
array(2) {
[0]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(6) "200122"
["email"]=>
string(21) "jon#gmail.com"
["content"]=>
string(34) "{"Notice":827,"co":3241,"Co":1555}"
}
[1]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(6) "592024"
["email"]=>
string(21) "jon#gmail.com"
["content"]=>
string(97) "{"Co":388,"co":5564,"xml":2982,"CO":6,"Warning":1957,"warning":42,"Notice":13,"cO":9,"Connect":6}"
}
}
array(1) {
[0]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(5) "19116"
["email"]=>
string(22) "kelly#gmail.com"
["content"]=>
string(8) "{"co":1}"
}
}
input array to the mail function should look like:
array(1) {
[0]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(6) "200122"
["email"]=>
string(21) "jon#gmail.com"
["content"]=>
string(34) "{"Notice":827,"co":3241,"Co":1555}"
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(6) "592024"
["email"]=>
string(21) "jon#gmail.com"
["content"]=>
string(97) " {"Co":388,"co":5564,"xml":2982,"CO":6,"Warning":1957,"warning":42,"Notice":13,"cO":9,"Conne ct":6}"
}
}
array(1) {
[0]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(5) "19116"
["email"]=>
string(22) "kelly#gmail.com"
["content"]=>
string(8) "{"co":1}"
}
}
its basically spouse to combine the two arrays that are in the same array.
how can i do that? thanks :)
The required outcome is not possible since you have duplicate keys in the array which won't be possible in PHP.
e.g.
["content"] => string(34) "{"Notice":827,"co":3241,"Co":1555}"
will be replaced by
["content"] => string(97) "{"Co":388,"co":5564,"xml":2982,"CO":6,"Warning":1957,"warning":42,"Notice":13,"cO":9,"Connect":6}"
You can do this simply using the array_merge function.
In their answer, Maarten suggests this is not possible because duplicate keys would be overwritten. However this only occurs when the keys aren't numeric. In your example above the keys of the first array are 0 and 1 and the second just 0. All numeric.
So all you need to do is:
array_merge($array1,$array2);
Related
Hello i have an array_dif function between 2 arrays and the result it's not as it should.I don't understand why it does not return the status as difference. First array is data second is row and the third is the result. In the result it should also be status because the value is different.
$result = array_diff($data,$row );
array(9) {
["scooter_id"]=>
string(6) "RO0001"
["battery_lvl"]=>
string(2) "80"
["lat"]=>
string(9) "44.312150"
["lng"]=>
string(9) "23.872900"
["alt"]=>
string(1) "0"
["speed"]=>
string(1) "0"
["status"]=>
string(1) "2"
["ip"]=>
string(14) "213.233.101.62"
["port"]=>
int(24600)
}
array(11) {
["battery_lvl"]=>
string(2) "80"
["nr_satelites"]=>
string(1) "1"
["lat"]=>
string(9) "44.312154"
["longi"]=>
string(9) "23.873007"
["alt"]=>
string(1) "0"
["speed"]=>
string(1) "0"
["status"]=>
string(1) "1"
["location"]=>
string(7) "romania"
["ip"]=>
string(14) "213.233.101.62"
["port"]=>
string(5) "24600"
["status_intermediar"]=>
string(1) "2"
}
array(3) {
["scooter_id"]=>
string(6) "RO0001"
["lat"]=>
string(9) "44.312150"
["lng"]=>
string(9) "23.872900"
}
array_diff checks only the values.
Because your 2nd array contains ["status_intermediar"]=> string(1) "2" it finds the value so it doesn't see it as a difference
If you want to check both keys and values you should use array_diff_assoc
Also if you want to find all the different values from BOTH arrays you should run it twice
$difference1=array_diff_assoc($array1,$array2);
$difference2=array_diff_assoc($array2,$array1);
array_dif is one way function ("Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays."- https://www.php.net/manual/en/function.array-diff.php).
If you want all diffs, you have to call it twice: array_dif($first, $second) and array_dif($second, $one) and optionally merge results.
$array_difference1 = array_merge(array_diff($array1, $array2),
array_diff($array2, $array1));
$array_differnce = array_merge(array_diff($array_difference1, $array3),
array_diff($array3, $array_difference1));
array(2) {
[0]=>
object(stdClass)#21 (7) {
["id"]=>
string(1) "1"
["title"]=>
string(7) "cvxzcvd"
["con"]=>
string(10) "gvsdvgsdfg"
["is_important"]=>
string(1) "1"
["date"]=>
string(3) "123"
["image"]=>
string(2) "55"
["cat_id"]=>
string(1) "1"
}
[1]=>
object(stdClass)#22 (7) {
["id"]=>
string(1) "2"
["title"]=>
string(4) "fsdf"
["con"]=>
string(9) "dfdsfvfds"
["is_important"]=>
string(1) "1"
["date"]=>
string(4) "5145"
["image"]=>
string(7) "5454124"
["cat_id"]=>
string(1) "2"
}
}
I passed this array into a view
$this->load->view('home/index',$news_data);
but I wanna use the data separately.
I mean if I wanna use the second title
or the second data.
how can I express that in the view
thanks
You can go like this:-
$news_data[1]->title;
$news_data[1]->data;
Store the array into variable like
$news_data['arr'] = { } ;
,and in view file access it by $arr;
print_r($arr);
I'm trying to combine two array in PHP with array_combine() function, but sometimes it working fine and sometimes it's not. I can't understand why it's working like this!
My Code:
var_dump($selectedDuretion);
var_dump($selectedDuretionType);
$combination = array_combine($selectedDuretion, $selectedDuretionType);
return $combination;
Expected OUTPUT:
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"days","12":"days","3":"weeks","4":"weeks"}
Actual OUTPUT :
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"weeks","12":"days","4":"weeks"}
The combination of arrays it shocking, I'll be thankful if anyone tell me why is this happening and how to solve it.
PHP Does not allow you to have duplicate indices in an array while JSON does allow you to have that for whatever reasons.
Since you are trying to convert PHP arrays to JSON your duplicate key gets eliminated. Hence you will have to manually build the JSON string.
$json="";
for($i=0;$i<count($selectedDuration);$i++)
{
$json.='"'.$selectedDuration[$i].'":"'.$selectedDurationType[$i].'",';
}
$json=rtrim($json,",");
$json="{".$json."}";
echo $json;
Output
{"3":"days","12":"days","4":"weeks","3":"weeks"}
Fiddle
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I'm sending the HTML FORM data with AJAX to PHP script and I'm converting the data to associative array like this:
$json = json_decode($request->get('json'),true);
so, when I'm dumping the variable I get this array:
array(11) {
[0]=>
array(2) {
["name"]=>
string(21) "form[pickupDate][day]"
["value"]=>
string(1) "4"
}
[1]=>
array(2) {
["name"]=>
string(23) "form[pickupDate][month]"
["value"]=>
string(1) "1"
}
[2]=>
array(2) {
["name"]=>
string(22) "form[pickupDate][year]"
["value"]=>
string(4) "2016"
}
[3]=>
array(2) {
["name"]=>
string(22) "form[pickupTime][hour]"
["value"]=>
string(1) "0"
}
[4]=>
array(2) {
["name"]=>
string(21) "form[returnDate][day]"
["value"]=>
string(1) "1"
}
[5]=>
array(2) {
["name"]=>
string(23) "form[returnDate][month]"
["value"]=>
string(1) "1"
}
[6]=>
array(2) {
["name"]=>
string(22) "form[returnDate][year]"
["value"]=>
string(4) "2016"
}
[7]=>
array(2) {
["name"]=>
string(22) "form[returnTime][hour]"
["value"]=>
string(1) "0"
}
[8]=>
array(2) {
["name"]=>
string(19) "form[pickupAddress]"
["value"]=>
string(0) ""
}
[9]=>
array(2) {
["name"]=>
string(12) "form[agency]"
["value"]=>
string(1) "1"
}
[10]=>
array(2) {
["name"]=>
string(12) "form[_token]"
["value"]=>
string(43) "9dh6ghpMv5K9LUdSLvh6y2NOzqTzUrOfVriL8C63Ybs"
}
}
how can I get the value of form[pickupDate][day] without loop?
I was able to do it like this:
echo array_column($json, null, 'name')['form[pickupDate][day]']['value'];
I've been trying to merge this array ONLY WHERE $array[4] exists more than one time, example: $array[4] == 'red' exactly twice. How can I merge only those arrays while keeping the others? I have made several attempts at this and I am willing to include my efforts if asked.
Consider this array:
array(3) {
[0]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(1) "2"
[3]=>
string(9) "Domain(s)"
[4]=>
string(20) "red"
}
[1]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(20) "red"
}
[2]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "536"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(16) " University Test"
}
}
TRYING TO ACHIEVE:
array(3) {
[0]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(1) "2"
[3]=>
string(9) "Domain(s)"
[4]=>
string(20) " red"
[5]=>
string(3) "Fee"
[6]=>
string(7) "License"
}
[1]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "536"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(16) " University Test"
}
}
foreach ($test as $item) {
if (!isset($merged[$item[4]])) {
// add the item to the merged array using key=$item[4]
$merged[$item[4]] = $item;
} else {
// merge the item with the item that is already in the array at key=$item[4]
$merged[$item[4]] = array_unique(array_merge($merged[$item[4]], $item));
// array_unique is necessary because array_merge will not overwrite numeric keys
}
}
// convert the keys back to numeric (if you care to)
$merged = array_values($merged);