I want to merge those three arrays:
array (
[0] => zooey,
[1] => Robert,
[2] => james,
[3] => Alfred,
);
array (
[0] => city1,
[1] => city2,
[2] => city3,
[3] => city4,
);
array (
[1] => city1,
[2] => city2,
[3] => city3,
[4] => city4,
);
into this:
array (
[0] => array('id'=>user_id1,'name'=>username1, 'city'=>user city1),
[1] => array('id'=>user_id2,'name'=>username2, 'city'=>user city2),
// and so on
);
You need to write your own merge function ,if all 3 arrays are same size with correct positioning of relevant elements, the function would look like this
function combineUserArray($arrayNames, $arrayCities, $arrayIds){
$users = [];
for($i = 0; $i < count($arrayIds); $i++){
$users[$i] = [
'id'=>$arrayIds[$i],
'name'=>$arrayNames[$i],
'city'=>$arrayCities[$i],
];
}
return $users;
}
Related
This question already has answers here:
Get all permutations of a PHP array?
(7 answers)
Closed 6 months ago.
Is there any way I could pull something like this when I call the function:
perm(array("one", "two", "tree"));
that would result to:
Array ( [0] => one [1] => two )
Array ( [0] => one [1] => tree )
Array ( [0] => two [1] => one )
Array ( [0] => two [1] => tree )
Array ( [0] => tree [1] => one )
Array ( [0] => tree [1] => two )
Array ( [0] => one [1] => two [2] => tree )
Array ( [0] => two [1] => one [2] => tree )
Array ( [0] => one [1] => tree [2] => two )
Array ( [0] => tree [1] => one [2] => two )
Array ( [0] => two [1] => tree [2] => one )
Array ( [0] => tree [1] => two [2] => one )
The problem is the array is dynamic and the value could become something like perm(array("one", "two", "tree", "five","8"));
I wanted to generate all the permutation even if the it becomes.
perm(array("one", "two")); or perm(array("two", "tree", "five","8"));
The problem of what I have below is that it starts from 1 permutation instead of 2 permutation above.
</php
function perm($arr, $temp_string, &$collect) {
if ($temp_string != "")
$collect []= $temp_string;
for ($i=0, $iMax = sizeof($arr); $i < $iMax; $i++) {
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1);
if (sizeof($arrcopy) > 0) {
perm($arrcopy, $temp_string ." " . $elem[0], $collect);
} else {
$collect []= $temp_string. " " . $elem[0];
}
}
}
?>
This may not have the same order but this is the same result you are expecting. You just have to add another if condition. I am assuming someone smarter than me here could recreate your function to make it more efficient with lower benchmarks. Otherwise you would have to go for this:
$collect = array();
function permute($arr, $temp_string, &$collect){
for($i=0, $iMax = sizeof($arr); $i < $iMax; $i++){
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1);
$jval = $temp_string." ".$elem[0];
if(!empty($temp_string)){
$collect [] = explode(" ",$jval);
}
if(sizeof($arrcopy) > 0){
permute($arrcopy, $jval, $collect);
}
}
}
permute(array('one', 'two', 'tree'), "", $collect);
Would result to:
Array ( [0] => [1] => one [2] => two )
Array ( [0] => [1] => one [2] => two [3] => tree )
Array ( [0] => [1] => one [2] => tree )
Array ( [0] => [1] => one [2] => tree [3] => two )
Array ( [0] => [1] => two [2] => one )
Array ( [0] => [1] => two [2] => one [3] => tree )
Array ( [0] => [1] => two [2] => tree )
Array ( [0] => [1] => two [2] => tree [3] => one )
Array ( [0] => [1] => tree [2] => one )
Array ( [0] => [1] => tree [2] => one [3] => two )
Array ( [0] => [1] => tree [2] => two )
Array ( [0] => [1] => tree [2] => two [3] => one )
I'm trying to merge two arrays. One array has names, and other has values. I want to merge them in a single multidimensional array in a way that values are in same sub-array if their names are similar. I'm new to programming and haven't been able to do anything
Names:
Array
(
[0] => TestAgent
[1] => TestAgent1
[2] => TestAgent1
[3] => TestAgent2
[4] => TestAgent2
[5] => TestAgent2
)
Values:
Array
(
[0] => 2019-04
[1] => 2019-05
[2] => 2019-06
[3] => 2019-04
[4] => 2019-05
[5] => 2019-06
)
This is desired output
Array
(
[0] => Array
(
[0] => TestAgent
[1] => 2019-04
)
[1] => Array
(
[0] => TestAgent1
[1] => 2019-05
[2] => 2019-06
)
[2] => Array
(
[0] => TestAgent2
[1] => 2019-04
[2] => 2019-05
[3] => 2019-06
)
)
Get the different counts of values from array_count_values() on the $names array, then use that to define the number of elements to use in array_slice(). The number of sliced elements adds to the count of the next offset.
$counts = array_count_values($names);
$offset = 0;
foreach (array_values(array_unique($names)) as $key=>$name) {
$slice = array_slice($values, $offset, $counts[$name]);
$offset += count($slice);
$result[$name] = $slice;
}
print_r($result);
Live demo at https://3v4l.org/VtktR
You can use array_intersect_key to get the matching values from the names array.
This method does not need sorted array(s).
$names = ['TestAgent1', 'TestAgent', 'TestAgent1', 'TestAgent1', 'TestAgent2', 'TestAgent2', 'TestAgent2'];
$values = ['2019-01', '2019-04', '2019-05', '2019-06', '2019-04', '2019-05', '2019-06'];
$unique = array_unique($names);
foreach($unique as $name){
$res[] = array_merge([$name], array_intersect_key($values, array_intersect($names, [$name])));
}
print_r($res);
Output:
Array
(
[0] => Array
(
[0] => TestAgent1
[1] => 2019-01
[2] => 2019-05
[3] => 2019-06
)
[1] => Array
(
[0] => TestAgent
[1] => 2019-04
)
[2] => Array
(
[0] => TestAgent2
[1] => 2019-04
[2] => 2019-05
[3] => 2019-06
)
)
https://3v4l.org/MWjv0
<?php
$one =
[
'Testagent',
'Testagent1',
'Testagent1',
'Testagent2',
'Testagent2',
'Testagent2'
];
$two =
[
'2019-04',
'2019-05',
'2019-06',
'2019-04',
'2019-05',
'2019-06'
];
$n = -1;
$previous = null;
foreach($one as $k=>$v) {
if($v !== $previous)
$items[++$n][] = $v;
$items[$n][] = $two[$k];
$previous = $v;
}
var_export($items);
Output:
array (
0 =>
array (
0 => 'Testagent',
1 => '2019-04',
),
1 =>
array (
0 => 'Testagent1',
1 => '2019-05',
2 => '2019-06',
),
2 =>
array (
0 => 'Testagent2',
1 => '2019-04',
2 => '2019-05',
3 => '2019-06',
),
)
If the first array does not have grouped like values do an asort on them first.
I have an array in PHP as below:
`
Array (
[0] => Array
(
[0] => Some text
[1] => 6230.C3
)
[1] => Array
(
[0] => Some text
[1] => 6230.C3
)
[2] => Array
(
[0] =>
[1] =>
)
[3] => Array
(
[0] => Some text
[1] =>
)
[4] => Array
(
[0] => Some text
[1] =>
)
[5] => Array
(
[0] => Some text
[1] =>
)
[6] => Array
(
[0] =>
[1] =>
)
[7] => Array
(
[0] => Some text
[1] =>
)
[8] => Array
(
[0] => Some text
[1] =>
)
)
`
Array[2] and Array[6] are empty arrays. Please help me split my array to three arrays array1, array2, array3 as below (split at position of Array[2] and Array[6]).
array1 = [ Array[0], Array[1] ]
array2 = [ Array[3], Array[4], Array[5] ]
Aarray3 = [ Array[7], Array[8] ]
Thank you for your help.
This is a little more flexible than your original question because it is not limited to exactly 3 arrays as separate variables. The result of the following would be an multidimensional array, split in the way that you want but for an unlimited number of splits. I can rework it if necessary to fit exactly 3 variables if that is really what your input will always be.
<?php
$final = array();
$counter = 0;
$outerEmpty = false;
foreach($originalArray as $outer) {
$innerEmpty = true;
foreach($outer as $inner) {
if (!empty($inner)) {
$innerEmpty = false;
break;
}
}
if (!$innerEmpty)
$final[$counter][] = $outer;
else
$counter++;
}
Consider the array below:
//$allmembers
Array
(
[0] => Array
(
[id] => 7
[name] => John Smith
)
[1] => Array
(
[id] => 8
[name] => John Skeet
)
[2] => Array
(
[id] => 9
[name] => Chuck Norris
)
[3] => Array
(
[id] => 10
[name] => Bruce Lee
)
)
I have another array like this:
//$schedules
Array
(
[0] => Array
(
[id] => 24
[title] => DAMN DAMN DAMN!
[description] =>
[room] => 5022
[start] => 1362783300
[end] => 1362783300
[participants] => 7,8
[members] => Array
(
)
)
[1] => Array
(
[id] => 22
[title] => blah blah
[description] =>
[room] => 5022
[start] => 1365024780
[end] => 1365026280
[participants] => 9,10
[members] => Array
(
)
)
)
So I have to read the participants keys in the second array, and then find the name from first array and add it to the member of the second array.
I am trying the code below but I aint got any success so far:
$allmembers = $_DB->Query("SELECT id,name FROM members");
for($i = 0; $i < count($schedules); $i++)
{
$schedules[$i]["members"] = array() ;
$mems = array();
$mems = explode(',', $schedules[$i]["participants"]);
for($j = 0; $j < count($mems); $j++)
{
//How to search the first array?
}
}
given that the two arrays exist above this block as $schedules and $allmembers, the following should work.
foreach($schedules as &$event)
{
$participants = array_flip(explode(',', $event['participants']));
$addThese = array();
foreach($allmembers as $member)
{
if (isset($participants[$member['id']]))
$addThese[] = $member;
}
$event['participants'] = $addThese;
} unset($event);
print_r($schedules);
I'm not sure if there is some kind of php function that will help me do this fairly simply or not. I figured I'd ask.
Let's say I have 5 products [prod1, prod2, prod3, prod4, prod5]
All of these products are related to eachother, so I need to arrive at something like this:
prod1, prod2, prod3, prod4, prod5
prod2, prod3, prod4, prod5, prod1
prod3, prod4, prod5, prod1, prod2
prod4, prod5, prod1, prod2, prod3
prod5, prod1, prod2, prod3, prod4
echo, save as variables, it doesn't matter to me.
In my example I said 5, but in reality there could be any number of products. Is there a function that does this automatically up to n products?? I don't even know what to really call this other then I'm matching them together.
You can do this:
$arr = array($prod1, $prod2, $prod3, $prod4, $prod5);
for ($i = 0; $i < count($arr); $i++) {
array_push($arr, array_shift($arr));
print_r($arr);
}
Another solution:
$array = array('$prod1', '$prod2', '$prod3', '$prod4', '$prod5');
$result = array();
for ($i = 0; $i < count($array); $i++) {
$result[] = array_merge( array_slice($array, $i), array_slice($array, 0, $i) );
}
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => $prod1
[1] => $prod2
[2] => $prod3
[3] => $prod4
[4] => $prod5
)
[1] => Array
(
[0] => $prod2
[1] => $prod3
[2] => $prod4
[3] => $prod5
[4] => $prod1
)
[2] => Array
(
[0] => $prod3
[1] => $prod4
[2] => $prod5
[3] => $prod1
[4] => $prod2
)
[3] => Array
(
[0] => $prod4
[1] => $prod5
[2] => $prod1
[3] => $prod2
[4] => $prod3
)
[4] => Array
(
[0] => $prod5
[1] => $prod1
[2] => $prod2
[3] => $prod3
[4] => $prod4
)
)
Not sure I understood what you want exactly, but considering that your original data is a string, you can simply call 'explode' (http://php.net/manual/en/function.explode.php) or similar to turn it into an array.
Try this:
$products = array();
$line= array("prod1", "prod2", "prod3", "prod4", "prod5");
array_push($products , $line);
print_r($products);