Multiple array merge with concat in php [closed] - php

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

Related

PHP multi-dimensional array, merge duplicate keys into new arrays [closed]

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-

Combine multi array to one array [closed]

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

What would be the output of this script? [closed]

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
In the following script
<?php
$people = array("Joe", "Pete", "Mike", "Mark");
print_r (each($people));
?>
if the 3rd line was reiterated?
Input:
$people = array("Joe", "Pete", "Mike", "Mark");
print_r(each($people));
print_r(each($people));
print_r(each($people));
print_r(each($people));
var_dump(each($people));
Output:
Array
(
[1] => Joe
[value] => Joe
[0] => 0
[key] => 0
)
Array
(
[1] => Pete
[value] => Pete
[0] => 1
[key] => 1
)
Array
(
[1] => Mike
[value] => Mike
[0] => 2
[key] => 2
)
Array
(
[1] => Mark
[value] => Mark
[0] => 3
[key] => 3
)
bool(false)

Convert object/array to array with comma and bracket [closed]

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 7 years ago.
Improve this question
Our Result:
Array (
[0] => Array ( [0] => Province [1] => Party [2] => Age [3] => Name [4] => Gender )
[1] => Array ( [0] => Quebec [1] => NDP [2] => 22 [3] => Liu, Laurin [4] => Female )
[2] => Array ( [0] => Quebec [1] => Bloc Quebecois [2] => 22 [3] => Mourani, Maria [4] => Female )
)
I want a result looking like this: How to convert like this?
array(
['Province'=>'Quebec','Party'=>'NDP','Age'=>22,'Name'=>'Liu, Laurin','Gender'=>'Female'],
['Province'=>'Quebec','Party'=>'Bloc Quebecois','Age'=>43,'Name'=>'Mourani, Maria','Gender'=>'Female']
)
OR
array(
['Province', 'Party', 'Age', 'Name', 'Gender'],
['Quebec', 'NDP', 22, 'Liu, Laurin', 'Female'],
['Quebec', 'Bloc Quebecois', 43, 'Mourani, Maria', 'Female']
)
Pretty simple using an array_combine() to set the headers for each row, and just walking the array setting those headers:
$headers = array_shift($myArray);
array_walk(
$myArray,
function(&$row) use ($headers) {
$row = array_combine($headers, $row);
}
);

grouping XML objects [closed]

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();

Categories