Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Looking for a way to add a couple of Simple XML objects together. The goal is to output them as a single XML doc but as separate entries in the XML dom. I'm not sure how to accomplish this. The objects are in an array like this:
Array (
[0] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[no] => 23432423
[type] => Array
)
[id] => 40043030
[title] => Cars
[cinemadate] => 2011-07-06
[changedate] => 2011-07-27T10:19:00
[year] => 2011
[length] => 112
[genres] => SimpleXMLElement Object
(
[genre] => animatie
)
[1] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[no] => 48050593
[type] => Array
)
[id] => 1231676
[title] => Arrietty
[cinemadate] => 2011-07-06
[changedate] => 2011-06-21T10:39:00
[genres] => SimpleXMLElement Object
(
[genre] => animatie
)
I would revert to DOM for this, there is only so much Simple XML can do:
$node = new SimpleXMLElement('<root/>');
$domnode = dom_import_simplexml($node);
foreach($arr as $simplexmlelement){
$domnode->appendChild(
$domnode->ownerDocument->importNode(dom_import_simplexml($simplexmlelement),true)
);
}
$node = simplexml_import_dom($node);
echo $node->asXML();
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
I want the array to be processed into my final array how am I supposed to achieve this? Below is the array that I have which I got from the loop.
Array
(
Array
(
[part_id] => 2338117
[supplier] => COOLDRIVE DISTRIBUTION
[quantity] => 12
)
Array
(
[part_id] => 2338117
[supplier] => ROLAN
[quantity] => 20
)
Array
(
[part_id] => 51154
[supplier] => ROLAN
[quantity] => 20
)
)
into the final array.
Array
(
[COOLDRIVE DISTRIBUTION] => Array
(
[proudctID] => Array
(
[0] => 2338117
)
)
[ROLAN] => Array
(
[productID] => Array
(
[0] => 2338117
[1] => 51154
)
)
)
You can use something like this in your case. Considering your old array as $oldarr
$newarr = array();
foreach($oldarr as $value){
$newarr[$value['supplier']]['productID'][] = $value['part_id'];
}
print_r($newarr);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have the following array:
Array (
[ids] => Array (
[0] => e348ae92
[1] => 193ba701
[2] => 58695854
)
[name] => Array (
[0] => Customers
[1] => Suppliers
[2] => Users
)
[subs] => Array (
[0] => 614
[1] => 65
[2] => 99
)
)
I want to take each array key and turn it into a it's own array e.g.
array(
[0] = array(
[0] => e348ae92
[1] => custommers
[2] => 614
)
[1] = array(
[0] => 193ba701
[1] => Suppliers
[2] => 65
)
[2] = array (
[0] => 58695854
[1] => Users
[2] => 99
)
)
I have looked at array_merge, array_combine and a few other things but I have been unsuccessful so far, any pointers in the right direction would be appreciated.
You can use array_map
$f = array_map(null, $a['ids'], $a['name'], $a['subs']);
print_r($f);
Working example:https://3v4l.org/dDG0Y-
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a PHP array like the following one and I would like to combine a multi array to one array:
Array
(
[0] => ADB_DW2017
[1] => LM9
[2] => MS_OF2013
)
Array
(
[0] => NK
[1] => PV
[2] => NK
)
Array
(
[0] => 15
[1] => 25
[2] => 10
)
Array
(
[0] => 250
[1] => 111
[2] => 150
)
Array
(
[0] => 450
[1] => 123
[2] => 250
)
Array
(
[0] => 0
[1] => Mien thue
[2] => 5
)
Array
(
[0] => 200
[1] => 12
[2] => 100
)
Array
(
[0] => 6.750
[1] => 3.075
[2] => 2.500
)
I want to combine like this, with the highest performance :
Array
(
[ADB_DW2017]=>array([suppiler]=>NK
[min_pro]=>15
[max_pro]=>250
[avg_pro]=>450
[tax]=>0
[com]=>200
[sum]=>6.750
)
)
My question is : How to combine a multi array to one array in PHP with the highest performance ?
I assume array names are $first_array,$second_array,...... (so change varabile names accordingly).
Do like below:-
$final_array = array();
foreach ($first_array as $key=> $arr){
$final_array[$arr] =array(
'suppiler'=>(isset($second_array[$key]))? $second_array[$key]: '',
'min_pro'=>(isset($third_array[$key]))? $third_array[$key]: 0,
'max_pro'=>(isset($fourth_array[$key]))? $fourth_array[$key]: 0,
'avg_pro'=>(isset($fifth_array[$key]))? $fifth_array[$key]: 0,
'tax'=>(isset($sixth_array[$key]))? $sixth_array[$key]: '',
'com'=>(isset($seventh_array[$key]))? $seventh_array[$key]: 0,
'sum'=>(isset($eigth_array[$key]))? $eigth_array[$key]: 0
);
}
echo "<pre/>";print_r($final_array);
Output:-https://eval.in/829938
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need array merge with value concat if array title is same name. my array print is->
Array
(
[0] => Array
(
[id] => 7867867
[title] => Title1
)
[1] => Array
(
[id] => 3452342
[title] => Title2
)
[2] => Array
(
[id] => 1231233
[title] => Title2
)
[3] => Array
(
[id] => 5867867
[title] => Title1
)
[4] => Array
(
[id] => 7867777
[title] => Title1
)
)
and i want to this format like if title is same concat the array value in one array and other array is removing.
like that->
Array
(
[0] => Array
(
[id] => 7867867,5867867,7867777
[title] => Title1,Title1,Title1
)
[1] => Array
(
[id] => 3452342,1231233
[title] => Title2,Title2
)
)
If you know that how to solve it please help me!
Thanks
Try this,
foreach($array as $val)
{
$titlearray[] = $val['title'];
}
$titlearray = (array_unique($titlearray));
//print_r($titlearray);
foreach($array as $val)
{
$key = array_search($val['title'], $titlearray);
$newarray[$key]['id'][] = $val['id'];
$newarray[$key]['title'][] = $val['title'];
}
DEMO
It's pretty straightforward, I have an array with a number of nodes of the same structure available as Simple XML objects (extracted from different XML documents). What's the easiest way to add them to a single XML document, so I can output the combined XML? I've searched, but I can't find a good solution.
Edit: Array looks like this, how do I combine these objects in one single XML object?
Array (
[0] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[no] => 23432423
[type] => Array
)
[id] => 40043030
[title] => Cars
[cinemadate] => 2011-07-06
[changedate] => 2011-07-27T10:19:00
[year] => 2011
[length] => 112
[genres] => SimpleXMLElement Object
(
[genre] => animatie
)
[1] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[no] => 48050593
[type] => Array
)
[id] => 1231676
[title] => Arrietty
[cinemadate] => 2011-07-06
[changedate] => 2011-06-21T10:39:00
[genres] => SimpleXMLElement Object
(
[genre] => animatie
)
Iterate over the array and add the data to the existing simplexml object. You have not provided much information in your question how the array data looks like nor have you provided an example simplexml object / xml chunk, so there is not much more to say specifically.