My array look like this :
[cart_seller] => Array
(
[3] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
)
)
[4] => Array
(
[# 801] => Array
(
)
)
)
[cart_product] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
[id] => 1705
[name] => ทดสอบสินค้า
[image] => p1534937865-VASAVAT LAB N MEDIA LOGO W.png
[price] => 1111
[option] => หมวด1เลือก1 หมวด2เลือก1
[amount] => 1
)
[# 801] => Array
(
[id] => 801
[name] => โบว์แพรแถบ ร.9 ชนมพรรษา 84 พรรษา ปีพุทธศักราช 2554
[image] => p1498062217-ส.jpg
[price] => 90
[option] =>
[amount] => 1
)
)
I want unset '# 801' in cart_seller and cart_product
in cart_product use unset($cart['cart_product'][# 801]);
but in cart_seller it in array [4] what can i do without reference value (4) ?
exapmle unset($cart['cart_seller'][xxxx][# 801]);
Just loop cart seller array til you find #801.
foreach($cart['cart_seller'] as $key => $c){
if(array_key_exists("#801",$c)){
unset($cart['cart_seller'][$key]['#801']);
}
}
I've that xml structure retrieving from device
<packet>
<info action="fiscalmemory" fiscalmemorysize="1048576" recordsize="464" fiscal="1" uniqueno="ABC12345678" nip="123-456-78-90" maxrecordscount="2144" recordscount="7" maxreportscount="1830" reportscount="4" resetmaxcount="200" resetcount="0" taxratesprglimit="30" taxratesprg="1" currencychangeprglimit="4" currencychangeprg="0" fiscalstartdate="dd-mm-yyyy hh:dd:ss" fiscalstopdate="dd-mm-yyyy hh:dd:ss" currencyname="PLN" />
<ptu name="A" bres="Nobi">123.23</ptu>
<ptu name="B">123.23</ptu>
<ptu name="D">8</ptu>
<sale>999.23</sale>
</packet>
simpleXml does't see ptu's attributes
$array = simplexml_load_string($xml);
print_r($array);
It prints
SimpleXMLElement Object
(
[info] => SimpleXMLElement Object
(
[#attributes] => Array
(
[action] => fiscalmemory
[fiscalmemorysize] => 1048576
[recordsize] => 464
[fiscal] => 1
[uniqueno] => ABC12345678
[nip] => 123-456-78-90
[maxrecordscount] => 2144
[recordscount] => 7
[maxreportscount] => 1830
[reportscount] => 4
[resetmaxcount] => 200
[resetcount] => 0
[taxratesprglimit] => 30
[taxratesprg] => 1
[currencychangeprglimit] => 4
[currencychangeprg] => 0
[fiscalstartdate] => dd-mm-yyyy hh:dd:ss
[fiscalstopdate] => dd-mm-yyyy hh:dd:ss
[currencyname] => PLN
)
)
[ptu] => Array
(
[0] => 123.23
[1] => 123.23
[2] => 8
)
[sale] => 999.23
)
As we can see ptu doesn't contain attributes :/
I also tried parse it with recursive function because children also may contain chilren but without success :/
Could anybody point to me why SimpleXML doesn't take ptu's attributes and also share any parsing function?
Thanks in advance.
edited
As regards Nigel Ren I made that function
function parseXMLtoArray($xml){
$x = simplexml_load_string($xml);
$result = [];
function parse($xml, &$res){
$res['name'] = $xml->getName();
$res['value'] = $xml->__toString();
foreach ($xml->attributes() as $k => $v){
$res['attr'][$k] = $v->__toString();
}
foreach($xml->children() as $child){
parse($child, $res['children'][]);
}
}
parse($x, $result);
return $result;
}
$resArray = parseXMLtoArray($rawXml);
print_r($resArray);
this returns such array
Array
(
[name] => packet
[value] =>
[attr] => Array
(
[crc] => BKJFKHKD54
)
[children] => Array
(
[0] => Array
(
[name] => info
[value] =>
[attr] => Array
(
[action] => fiscalmemory
[fiscalmemorysize] => 1048576
[recordsize] => 464
[fiscal] => 1
[uniqueno] => ABC12345678
[nip] => 123-456-78-90
[maxrecordscount] => 2144
[recordscount] => 7
[maxreportscount] => 1830
[reportscount] => 4
[resetmaxcount] => 200
[resetcount] => 0
[taxratesprglimit] => 30
[taxratesprg] => 1
[currencychangeprglimit] => 4
[currencychangeprg] => 0
[fiscalstartdate] => dd-mm-yyyy hh:dd:ss
[fiscalstopdate] => dd-mm-yyyy hh:dd:ss
[currencyname] => PLN
)
)
[1] => Array
(
[name] => ptu
[value] => 123.23
[attr] => Array
(
[name] => A
)
)
[2] => Array
(
[name] => ptu
[value] => 123.23
[attr] => Array
(
[name] => B
)
)
[3] => Array
(
[name] => ptu
[value] => 8
[attr] => Array
(
[name] => D
)
)
[4] => Array
(
[name] => sale
[value] => 999.23
)
)
)
Is this correct?
Thanks again Nigel
When loading with SimpleXML, using print_r() gives only an idea of the content and doesn't have the full content. If you want to see the full content then use ->asXML()...
$array = simplexml_load_string($xml);
echo $array->asXML();
To check the attributes of <ptu> element, (this just gives the first one)...
echo $array->ptu[0]['name'];
This uses ->ptu to get the <ptu> element and takes the first one [0] and then takes the name attribute using ['name'].
how i can get WP_Widget_Archives from array,
This is my array:
$control = Array
(
[name] => Archives
[id] => archives-6
[callback] => Array
(
[0] => WP_Widget_Archives Object
(
[id_base] => archives
[name] => Archives
[widget_options] => Array
(
[classname] => widget_archive
[description] => A monthly archive of your site’s Posts.
)
[control_options] => Array
(
[id_base] => archives
)
[number] => 8
[id] => archives-8
[updated] =>
[option_name] => widget_archives
)
[1] => form_callback
)
[params] => Array
(
[0] => Array
(
[number] => 6
)
)
[width] => 250
[height] => 200
[id_base] => archives
)
i have try with this code
`echo '<pre>'; print_r(array_keys($control['callback'])); echo '</pre>';`
but I get result like this
Array
(
[0] => 0
[1] => 1
)
where I think the result will be like this
$result = Array
(
[0] => WP_Widget_Archives Object
[1] => form_callback
)
so i can write $result[0] for get WP_Widget_Archives, please help me and thank you for your help :)
Probably you misunderstood array_key function. It will give you keys of the array not value. In your case you require value which is an object 'WP_Widget_Archives', so you can directly use $control['callback'][0].
This question already has answers here:
How to convert XML into array in PHP?
(12 answers)
Closed 7 years ago.
I am working on property site. My requirement is to import property info with its images and amenities and store it in to database. I've an xml array and i want to convert it into php array. I've xml array like this:-
SimpleXMLElement Object
(
[Listing] => Array
(
[0] => SimpleXMLElement Object
(
[count] => 1
[Ad_Type] => Rent
[Unit_Type] => Office
[Unit_Model] => SimpleXMLElement Object
(
)
[Primary_View] => SimpleXMLElement Object
(
)
[Unit_Builtup_Area] => 7593.00
[No_of_Bathroom] => 2
[Property_Title] => Business Center / BANKS offices/ Call Centre Offices
[Web_Remarks] => SimpleXMLElement Object
(
)
[Emirate] => Dubai
[Community] => Sheikh Zayed Road
[Property_Name] => Millennium Plaza
[Property_Ref_No] => AMB-R-1142
[Listing_Agent] => Janette Ceniza
[Listing_Agent_Phone] => 0564843282
[Listing_Date] => 2015-03-26 4:53:51 pm
[Last_Updated] => 2015-07-29 1:44:23 pm
[Bedrooms] => SimpleXMLElement Object
(
)
[Listing_Agent_Email] => consult1#khalidalattar.com
[Price] => 1200000
[Frequency] => per year
[Unit_Reference_No] => AMB-R-1142
[No_of_Rooms] => SimpleXMLElement Object
(
)
[Latitude] => 25.062252
[Longitude] => 55.130672
[unit_measure] => Sq.Ft.
[Featured] => 0
[Images] => SimpleXMLElement Object
(
[image] => Array
(
[0] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_19-1605-6d8e8f3aa6e53b45154eac91e2109ba4.jpg
[1] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_35-1605-660a612b520978e1249c29b7fb591b45.jpg
[2] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_36-1605-01d394a6a096b9a0252b30a10fd59c13.jpg
[3] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_38-1605-80df60708c007c5a85721b89c97836d8.jpg
[4] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_38-1605-1f9ffdde85af52dfc6f3548264c34ce0.jpg
[5] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_44-1605-322badce946a4883abe602bf8c221a93.jpg
[6] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_44-1605-117288432442fd54dae0e68238f39537.jpg
[7] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_54-1605-914e38a1abe6418ea10885b94e4f26de.jpg
[8] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_55-1605-fc756a57f7280678b41b07340e3d1e60.jpg
[9] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_55-1605-53eb6e2277f1b19ee40a36660086a20e.jpg
[10] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_58_36-1605-9e54faba2c9f96e31646c0660f3f0153.jpg
)
)
[Facilities] => SimpleXMLElement Object
(
[facility] => Array
(
[0] => Built in wardrobes
[1] => Covered parking
[2] => Public parking
)
)
[company_name] => Amber Real Estate
[Web_Tour] => SimpleXMLElement Object
(
)
[Threesixty_Tour] => SimpleXMLElement Object
(
)
[Audio_Tour] => SimpleXMLElement Object
(
)
[Virtual_Tour] => SimpleXMLElement Object
(
)
[QR_Code] => SimpleXMLElement Object
(
)
[company_logo] => http://crm.propspace.com/application/views/pictures/logos/16051408971828.png
[Parking] => 2
[Strno] => SimpleXMLElement Object
(
)
[PreviewLink] => http://crm.propspace.com/preview/index/14273740230306248/1605/?l_id=1576257622628120
)
)
)
There are multiple entries like this. I'd given first entry for the referrence. For converting this xml to php array I've written the below code:-
function convert_xml($xml){
$arr = array();
foreach($xml->children() as $property){
if(count($property->children()) == 0){
$arr[$property->getName()] = strval($property);
}else{
$arr[$property->getName()][] = xml2array($property);
}
}
return $arr;
}
$xml_Arr = convert_xml($xml);
echo "<pre>"; print_r($xml_Arr); exit;
I m getting the result as php array given below.
Array
(
[Listing] => Array
(
[0] => Array
(
[count] => 1
[Ad_Type] => Rent
[Unit_Type] => Office
[Unit_Model] =>
[Primary_View] =>
[Unit_Builtup_Area] => 7593.00
[No_of_Bathroom] => 2
[Property_Title] => Business Center / BANKS offices/ Call Centre Offices
[Emirate] => Dubai
[Community] => Sheikh Zayed Road
[Property_Name] => Millennium Plaza
[Property_Ref_No] => AMB-R-1142
[Listing_Agent] => Janette Ceniza
[Listing_Agent_Phone] => 0564843282
[Listing_Date] => 2015-03-26 4:53:51 pm
[Last_Updated] => 2015-07-29 1:44:23 pm
[Bedrooms] =>
[Listing_Agent_Email] => consult1#khalidalattar.com
[Price] => 1200000
[Frequency] => per year
[Unit_Reference_No] => AMB-R-1142
[No_of_Rooms] =>
[Latitude] => 25.062252
[Longitude] => 55.130672
[unit_measure] => Sq.Ft.
[Featured] => 0
[Images] => Array
(
[0] => Array
(
[image] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_58_36-1605-9e54faba2c9f96e31646c0660f3f0153.jpg
)
)
[Facilities] => Array
(
[0] => Array
(
[facility] => Public parking
)
)
[company_name] => Amber Real Estate
[Web_Tour] =>
[Threesixty_Tour] =>
[Audio_Tour] =>
[Virtual_Tour] =>
[QR_Code] =>
[company_logo] => http://crm.propspace.com/application/views/pictures/logos/16051408971828.png
[Parking] => 2
[Strno] =>
[PreviewLink] => http://crm.propspace.com/preview/index/14273740230306248/1605/?l_id=1576257622628120
)
)
)
There are 10 element inside image array, but it is returning only the last image. I want image array as well in my result.
foreach($xml->children() as $key=>$property){
if(count($property->children()) == 0){
$arr[$property->getName()] = strval($property);
}else{
$arr[$property->getName()][$key] = xml2array($property);
}
}
I have a script that loops through a bunch of data collected from database tables. I've read other similar posts on StackOverflow about merging duplicate array keys, but none of them seem to work for me. Using the code below, I'm building all of the compiled data into an array:
$sql = 'SELECT * FROM '.$qstTable.' WHERE '.$type.'_qst_id = '.$answer['answer_qst'];
$result = $db->sql_query($sql);
$q = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$sql = 'SELECT * FROM '.$catTable.' WHERE '.$type.'_cat_clean = "'.$q[$type.'_qst_cat'].'"';
$result = $db->sql_query($sql);
$cat = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$daField = $cat[$type.'_cat_name'];
if(count($allQsts)){
if(array_key_exists($daField, $allQsts)){
$daData = array(
'question' => array(
'id' => $q[$type.'_qst_id'],
'qst' => $q[$type.'_qst_qst'],
),
'answer' => array(
'id' => $answer['answer_id'],
'type' => $answer['answer_input'],
'content' => $answer['answer_content'],
'q_type' => $type,
)
);
array_push($allQsts[$daField], $daData);
}else{
$allQsts[$cat[$type.'_cat_name']][] = array(
'question' => array(
'id' => $q[$type.'_qst_id'],
'qst' => $q[$type.'_qst_qst'],
),
'answer' => array(
'id' => $answer['answer_id'],
'type' => $answer['answer_input'],
'content' => $answer['answer_content'],
'q_type' => $type,
)
);
}
}else{
$allQsts[$cat[$type.'_cat_name']][] = array(
'question' => array(
'id' => $q[$type.'_qst_id'],
'qst' => $q[$type.'_qst_qst'],
),
'answer' => array(
'id' => $answer['answer_id'],
'type' => $answer['answer_input'],
'content' => $answer['answer_content'],
'q_type' => $type,
)
);
}
And this is how my array turns out looking when it's all processed:
Array (
[Ancestry] => Array (
[0] => Array (
[question] => Array (
[id] => 1
[qst] => Has your family always lived in this country? Where did your family come from? How did they come here?
)
[answer] => Array (
[id] => 28
[type] => text
[content] => idk
[q_type] => life
)
)
)
)
Array (
[High School] => Array (
[0] => Array (
[question] => Array (
[id] => 158
[qst] => Who were your best friends in high school? Were they the same ones from grade school? Do you still keep in touch with them?
)
[answer] => Array (
[id] => 30
[type] => video
[content] => v-0bd3d270-2f24-0132-cd89-12313914f10b
[q_type] => life
)
)
)
)
Array (
[High School] => Array (
[0] => Array (
[question] => Array (
[id] => 124
[qst] => What year did you start high school? What high school did you go to? Did you like it? Did you ever wish you would've gone to a different high school?
)
[answer] => Array (
[id] => 36
[type] => text
[content] => Started HS in 1987
[q_type] => life
)
)
)
)
Array (
[Young Adult] => Array (
[0] => Array (
[question] => Array (
[id] => 213
[qst] => As a young adult did you stay in the same town as your friends or did you move to a new place and had to make new friends?
)
[answer] => Array (
[id] => 39
[type] => video
[content] => v-7d59df50-3bda-0132-cda7-12313914f10b
[q_type] => life
)
)
)
)
Array (
[Young Adult] => Array (
[0] => Array (
[question] => Array (
[id] => 207
[qst] => After high school - did you go to college, join the miltary, or did you get a job?
)
[answer] => Array (
[id] => 40
[type] => text
[content] => went to college at ASU
[q_type] => life
)
)
)
)
Array (
[Multiple Sclerosis] => Array (
[0] => Array (
[question] => Array (
[id] => 1278
[qst] => Do you know of any potential new drugs or treatments that are in development to treat multiple sclerosis? Are you optomistic?
)
[answer] => Array (
[id] => 33
[type] => text
[content] => vg hjc
[q_type] => pack
)
)
)
)
However, What I would like to do is combine nodes in the array that already exist, like so:
Array (
[Ancestry] => Array (
[0] => Array (
[question] => Array (
[id] => 1
[qst] => Has your family always lived in this country? Where did your family come from? How did they come here?
)
[answer] => Array (
[id] => 28
[type] => text
[content] => idk
[q_type] => life
)
)
)
)
Array (
[High School] => Array (
[0] => Array (
[question] => Array (
[id] => 158
[qst] => Who were your best friends in high school? Were they the same ones from grade school? Do you still keep in touch with them?
)
[answer] => Array (
[id] => 30
[type] => video
[content] => v-0bd3d270-2f24-0132-cd89-12313914f10b
[q_type] => life
)
)
[1] => Array (
[question] => Array (
[id] => 124
[qst] => What year did you start high school? What high school did you go to? Did you like it? Did you ever wish you would've gone to a different high school?
)
[answer] => Array (
[id] => 36
[type] => text
[content] => Started HS in 1987
[q_type] => life
)
)
)
)
Array (
[Young Adult] => Array (
[0] => Array (
[question] => Array (
[id] => 213
[qst] => As a young adult did you stay in the same town as your friends or did you move to a new place and had to make new friends?
)
[answer] => Array (
[id] => 39
[type] => video
[content] => v-7d59df50-3bda-0132-cda7-12313914f10b
[q_type] => life
)
)
[1] => Array (
[question] => Array (
[id] => 207
[qst] => After high school - did you go to college, join the miltary, or did you get a job?
)
[answer] => Array (
[id] => 40
[type] => text
[content] => went to college at ASU
[q_type] => life
)
)
)
)
Array (
[Multiple Sclerosis] => Array (
[0] => Array (
[question] => Array (
[id] => 1278
[qst] => Do you know of any potential new drugs or treatments that are in development to treat multiple sclerosis? Are you optomistic?
)
[answer] => Array (
[id] => 33
[type] => text
[content] => vg hjc
[q_type] => pack
)
)
)
)
How can I modify the code above to do this?
EDIT - Updating to include some of the code I've tried to compress the array:
After the full $allQsts array is created, I looped it through this and tested the output, but each entry was duplicated even more.
$sortedIt = array();
foreach($allQsts as $m => $n){
if(!isset($sortedIt[$m])){
$sortedIt[$m] = array();
}
$sortedIt[$m] = $n;
}
I've also been playing around with array_merge_recursive but have yet to get anywhere close.
Assuming this is run in a loop and $allQsts points to the same array each time, you can simplify the code drastically:
$daField = $cat[$type.'_cat_name'];
$daData = array(
'question' => array(
'id' => $q[$type.'_qst_id'],
'qst' => $q[$type.'_qst_qst'],
),
'answer' => array(
'id' => $answer['answer_id'],
'type' => $answer['answer_input'],
'content' => $answer['answer_content'],
'q_type' => $type,
)
);
if (array_key_exists($daField, $allQsts)) {
$allQsts[$daField][] = $daData;
} else {
$allQsts[$daField] = array($daData);
}
This by itself should be enough to get the desired data structure.