Merging and combining two arrays in PHP - php

I have two arrays one created from a table in my database and one created from a HTML form (lets call them array_db and array_form).
I want to insert array_db to a table in my mySQL database, but before inserting I must merge array_db with array_form and remove duplicates. When there are a duplicates I want the values from array_form to be inserted to array_db
NOTE: array_form are not always the same meaning it could have more or less values than in the below ex.
What is the best way to do this?
Array_db
Array
(
[task_id] => task_id
)
Array
(
[user_id] => user_id
)
Array
(
[customer_id] => customer_id
)
Array
(
[created] => created
)
Array
(
[pickup] => pickup
)
Array
(
[tire_front] => tire_front
)
Array
(
[tire_back] => tire_back
)
Array
(
[tire_reg] => tire_reg
)
Array
(
[tire_indl] => tire_indl
)
Array
(
[tube_front] => tube_front
)
Array
(
[tube_back] => tube_back
)
Array
(
[hub_front] => hub_front
)
Array
(
[hub_back] => hub_back
)
Array
(
[hub_adjust] => hub_adjust
)
Array
(
[rim_front] => rim_front
)
Array
(
[rim_back] => rim_back
)
Array
(
[rim_adjust] => rim_adjust
)
Array
(
[spoke_missing_front] => spoke_missing_front
)
Array
(
[spoke_missing_back] => spoke_missing_back
)
Array
(
[spoke_comp_front] => spoke_comp_front
)
Array
(
[spoke_comp_back] => spoke_comp_back
)
Array
(
[break_adjust_front] => break_adjust_front
)
Array
(
[break_adjust_back] => break_adjust_back
)
Array
(
[break_cable_front] => break_cable_front
)
Array
(
[break_cable_back] => break_cable_back
)
Array
(
[break_pad_front] => break_pad_front
)
Array
(
[break_pad_back] => break_pad_back
)
Array
(
[gear_adj_front] => gear_adj_front
)
Array
(
[gear_adj_back] => gear_adj_back
)
Array
(
[gear_cable_front] => gear_cable_front
)
Array
(
[gear_cable_back] => gear_cable_back
)
Array
(
[gear_shift_front] => gear_shift_front
)
Array
(
[gear_shift_back] => gear_shift_back
)
Array
(
[bicy_chain] => bicy_chain
)
Array
(
[cog_wheel] => cog_wheel
)
Array
(
[cassette] => cassette
)
Array
(
[chainwheel] => chainwheel
)
Array
(
[crankset] => crankset
)
Array
(
[crank] => crank
)
Array
(
[fp_service] => fp_service
)
Array
(
[status] => status
)
Array
(
[service_1] => service_1
)
Array
(
[service_2] => service_2
)
Array
(
[service_3] => service_3
)
Array
(
[service_4] => service_4
)
Array
(
[price_approx] => price_approx
)
Array
(
[price_max] => price_max
)
Array_form
(
[tire_back] => tire_back
[tube_back] => tube_back
[gear_shift_front] => gear_shift_front
[user_id] => 0
[customer_id] => 6
)

As I said in my comment, this should be solveable by first flattening $array_db, eg like this
$flat_array_db = array();
foreach ($array_db as $subArray) {
foreach ($subArray as $key=>$value) {
$flat_array_db[$key] = $value;
}
}
And once you've done that, a simple array_merge($flat_array_db, $array_form) should do the trick.

Related

Combine/Merge arrays that have the same keys PHP

I have an array that has some keys that are repeating multiple times. Would like to combine them so that I can have them in one array, within the same array.
I have an array of this type;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
)
)
Array
(
[Shoes] => Array
(
[FUBU] => Array
(
[0] => size6
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => White
)
)
)
I would like an output of the following kind;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
I have tried array_merge, array_merge_recursive but all are not working.
foreach ($county as $value) { print_r(array_merge($value)); } //$value contains the array
foreach ($county as $value) { print_r(array_merge_recursive($value)); } //$value contains the array
Kindly assist if you have an idea on how to solve this in PHP.
You could do it with a few nested array_walk() calls:
$arr=array(array("Shoes" => array("POLO" => array("Size5"))),
array("Shoes" => array("FUBU" => array("size6"))),
array("Bag" => array("HPBAG" => array("Black"))),
array("Bag" => array("HPBAG" => array("White"))));
$res=[];
array_walk($arr,function($a) use (&$res){
array_walk($a, function($ar,$type) use (&$res){
array_walk($ar, function ($arr,$brand) use (&$res,$type){
$res[$type][$brand]=array_merge($res[$type][$brand]??[],$arr);
});
});
});
print_r($res);
See the demo here: https://rextester.com/RFLQ18142
It produces this result:
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)

Read data from a single record OR more records

I have a query which gives data from one person or from more persons.
The data for one person is presented with JSON as:
Array ( [0] => Array (
[a2a_Person] => Array (
[pid] => Person1
[a2a_PersonName] => Array (
[a2a_PersonNameFirstName] => Array ( [a2a_PersonNameFirstName] => Aagje )
[a2a_PersonNameLastName] => Array ( [a2a_PersonNameLastName] => Baltus ) ) ....
If there are more persons then the data is presented as
Array ( [0] => Array (
[a2a_Person] => Array (
[0] => Array (
[pid] => Person1 [a2a_PersonName] => Array (
[a2a_PersonNameFirstName] => Array ( [a2a_PersonNameFirstName] => Walig )
[a2a_PersonNamePatronym] => Array ( )
[a2a_PersonNamePrefixLastName] => Array ( )
[a2a_PersonNameLastName] => Array ( [a2a_PersonNameLastName] => Verdugt ) ) [a2a_Gender] => Array ( [a2a_Gender] => Man )
[1] => Array (
[pid] => Person2
[a2a_PersonName] => Array (
[a2a_PersonNameFirstName] => Array ( [a2a_PersonNameFirstName] => Huibert ) [a2a_PersonNamePatronym.....
My question is now : how can i determine if I have more Person-records so i can acces the data correct way.
And what is the best way to acces the data. Must I first check the definition and write different code.
Thanks,
Fred

How can I sort through multidimensional arrays within multidimensional arrays to eliminate repetitions?

Here is an example output from when I print out its contents:
Array
(
[0] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbA] =>
)
)
)
)
[1] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbB] =>
)
)
)
)
[2] => Array
(
[CountryA] => Array
(
[ProvinceB] => Array
(
[CityB] => Array
(
[SuburbC] =>
)
)
)
)
[3] => Array
(
[CountryB] => Array
(
[ProvinceD] => Array
(
[CityE] => Array
(
[SuburbE] =>
)
)
)
)
What I would like to do is create a function that parses it in some way (and perhaps creates a new array) so that the result will look something like:
Array
(
[0] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbA] =>
[SuburbB} =>
)
)
[ProvinceB] =>
(
[CityB] => Array
(
[SuburbC] =>
)
)
)
)
[1] => Array
(
[CountryB] => Array
(
[ProvinceD] => Array
(
[CityE] => Array
(
[SuburbE] =>
)
)
)
)
Thanks in advance!!
Change you structure, your array should not look like this :
Array(
[0] => Array([Country A] => data),
[1] => Array([Country B] => data)
)
But more like this :
Array(
[Country A] => data,
[Country B] => data
)
Once you've done this, it will be trivial to add a city in your array :
If the country exists in the array, add to the country, else add it to the array and stop
[Add to country :] If the province exists in the country, add to the province, else add the province to the array and stop
Same for city, suburb... you get the idea

How to store _attr key values in an array using php?

I have parsed my xml into array using xml2array.php.
It gave array like
Array(
[HotelImage] => Array
(
[0] => Array
(
)
[1] => Array
(
)
[0_attr] => Array
(
[Type] => Bar/Lounge
[URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-11.jpg
)
[1_attr] => Array
(
[Type] => Lobby
[URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-8.jpg
)
[2] => Array
(
)
[2_attr] => Array
(
[Type] => Exterior
[URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-1.jpg
)
[3] => Array
(
)
[3_attr] => Array
(
[Type] => Recreational Facilities
[URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-12.jpg
)
)
)
how can i store all _attr key values in a separate array.
// this may cause a dereference error.. if it does just run array_keys
// and stro it in a sperate var that is then passed to preg_grep
$attrKeys = preg_grep('#\d+_attr#', array_keys($xmlArray));
$attrKeys = array_flip($attrKeys);
$attributes = array_intersect_key($xmlArray, $attrKeys);
$tags = array_diff_key($xmlArray, $attributes);

Why is this output blank?

I know I must be doing something simple wrong. When I do this:
echo '<pre>';
print_r($event->when);
echo '</pre>';
I get this:
Array
(
[0] => Zend_Gdata_Extension_When Object
(
[_rootElement:protected] => when
[_reminders:protected] =>
[_startTime:protected] => 2011-06-16T10:00:00.000-05:00
[_valueString:protected] =>
[_endTime:protected] => 2011-06-17T11:00:00.000-05:00
[_rootNamespace:protected] => gd
[_rootNamespaceURI:protected] =>
[_extensionElements:protected] => Array
(
)
[_extensionAttributes:protected] => Array
(
)
[_text:protected] =>
[_namespaces:protected] => Array
(
[atom] => Array
(
[1] => Array
(
[0] => http://www.w3.org/2005/Atom
)
)
[app] => Array
(
[1] => Array
(
[0] => http://purl.org/atom/app#
)
[2] => Array
(
[0] => http://www.w3.org/2007/app
)
)
[gd] => Array
(
[1] => Array
(
[0] => http://schemas.google.com/g/2005
)
)
[openSearch] => Array
(
[1] => Array
(
[0] => http://a9.com/-/spec/opensearchrss/1.0/
)
[2] => Array
(
[0] => http://a9.com/-/spec/opensearch/1.1/
)
)
[rss] => Array
(
[1] => Array
(
[0] => http://blogs.law.harvard.edu/tech/rss
)
)
)
)
)
I'm then trying to get startTime by doing this:
$StartTime = $event->when->startTime;
But I'm not getting anything.
And yet, when I do this:
pr($event->published);
I get this:
Zend_Gdata_App_Extension_Published Object
(
[_rootElement:protected] => published
[_rootNamespace:protected] => atom
[_rootNamespaceURI:protected] =>
[_extensionElements:protected] => Array
(
)
[_extensionAttributes:protected] => Array
(
)
[_text:protected] => 2011-06-15T03:32:14.000Z
[_namespaces:protected] => Array
(
[atom] => Array
(
[1] => Array
(
[0] => http://www.w3.org/2005/Atom
)
)
[app] => Array
(
[1] => Array
(
[0] => http://purl.org/atom/app#
)
[2] => Array
(
[0] => http://www.w3.org/2007/app
)
)
)
)
and I can do this:
$dateAdded = $event->published->text;
echo $dateAdded;
and I see an output...
According to to the official Zend_Gdata_Extension_When documentation, there's a method called getStartTime() which will give you the time.
If you do $event->when[0]->getStartTime() or $event->when[0]->startTime, you'll get the start time.
startTime is marked protected. You can't reference it from outside like you did. There must be a getter function 'getStartTime()' function in that object that would allow you to reference it publicly.
EDIT: Also it is returning an object array - not an single object, so you would need to reference the it like: $event[0]->getterFunction() or loop through the array with a foreach accessing the individual objects in the loop

Categories