Combining array of array with another array in PHP using array_combine - php

I'm playing around with array functions to get a better understanding.
Below I have three arrays:
$cardNumber = array(2, 4, 5, 8, 9);
$playerName = array('Julian', 'Brad', 'Chloe', 'Laura', 'Paul');
$playerWins = array(4, 5, 1, 2, 6);
I am trying to achieve the following array structure:
array (size=5)
2 =>
array (size=2)
0 => string 'Julian' (length=6)
1 => int 4
4 =>
array (size=2)
0 => string 'Brad' (length=4)
1 => int 5
5 =>
array (size=2)
0 => string 'Chloe' (length=5)
1 => int 1
8 =>
array (size=2)
0 => string 'Laura' (length=5)
1 => int 2
9 =>
array (size=2)
0 => string 'Paul' (length=4)
1 => int 6
I decided to combine $playerName and $playerWins with array_combine(), and then combine the resulting array with $cardNumber, which does not yield the output I expect. Am I understanding array_combine() incorrectly?

You can use array_map with null as a callback and the two data arrays to give you the array structure that you want. Then combine that with the array for the keys using array_combine:
$result = array_combine($cardNumber, array_map(null, $playerName, $playerWins));
For illustration:
$result = array_map(null, $playerName, $playerWins);
Yields:
Array
(
[0] => Array
(
[0] => Julian
[1] => 4
)
[1] => Array
(
[0] => Brad
[1] => 5
)
[2] => Array
(
[0] => Chloe
[1] => 1
)
[3] => Array
(
[0] => Laura
[1] => 2
)
[4] => Array
(
[0] => Paul
[1] => 6
)
)
Then combine to get the keys:
$result = array_combine($cardNumber, $result);
Yields:
Array
(
[2] => Array
(
[0] => Julian
[1] => 4
)
[4] => Array
(
[0] => Brad
[1] => 5
)
[5] => Array
(
[0] => Chloe
[1] => 1
)
[8] => Array
(
[0] => Laura
[1] => 2
)
[9] => Array
(
[0] => Paul
[1] => 6
)
)

Related

Merging two array with same values in php

I have two arrays.
Array 1
Array
(
[1] => 111,
[id1] => 1,
[2] => 11231,
[id2] => 2,
[3] => 12311,
[id3] => 3,
[4] => 11981,
[id4] => 4,
[5] => 11761,
[id5] => 5,
[6] => 11561,
[id6] => 6
)
Array 2
Array
(
[1] => 2,
[id1] => 1,
[2] => 2,
[id2] => 2,
[3] => 3,
[id3] => 3,
[4] => 4,
[id4] => 4,
[5] => 4,
[id5] => 5,
[6] => 6,
[id6] => 6
)
Id key is user id in both arrays
And numerical key in second array is manager id
I want to merge these two array in below format.
Merge array
Array
(
[2] => Array
(
[0] => 111
[1] => 1
),
Array
(
[0] => 11231
[1] => 2
),
[3] => Array
(
[0] => 12311
[1] => 3
),
[4] => Array
(
[0] => 11981
[1] => 4
),
Array
(
[0] => 11761
[1] => 5
),
[6] => Array
(
[0] => 11561
[1] => 6
)
)
Array inside array is the value of first array.
2,3,4,6 key is values from second array.
Those users who have same manager id will be merge in single array.
Array Walk Apply a user supplied function to every member of an array
array_walk($array2, function($value,$key) use($array1,&$result){
if(is_integer($key))
$result[$value][]=[$array1[$key],$array1['id'.$key]];
});
output
array (size=4)
2 =>
array (size=2)
0 =>
array (size=2)
0 => int 111
1 => int 1
1 =>
array (size=2)
0 => int 11231
1 => int 2
3 =>
array (size=1)
0 =>
array (size=2)
0 => int 12311
1 => int 3
4 =>
array (size=2)
0 =>
array (size=2)
0 => int 11981
1 => int 4
1 =>
array (size=2)
0 => int 11761
1 => int 5
6 =>
array (size=1)
0 =>
array (size=2)
0 => int 11561
1 => int 6

Php array - merge only specific keys in single array,

(
[0] => Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 3
[1] => 4
)
)
)
I want to merge only specific keys as below, Key 0 and 1 are merged as a single array. Like this I want.
In the below example, keys 0 and 1 are merged, it can be changing, so I want custom.. please help me
Array
(
[0] => Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[1] => Array
(
[0] => 3
[1] => 4
)
)
)
This script will help you
<?php
$array = [
[
[
1,
2,
],
[
3,
4,
],
[
3,
4,
]
]
];
array_unshift($array[0], array_merge($array[0][0], $array[0][1]));
unset($array[0][1]);
unset($array[0][2]);
print_r($array);
output
Array
(
[0] => Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[3] => Array
(
[0] => 3
[1] => 4
)
)
)
First use array_merge to merge the elements and then use array_unshift to push the new element to array and then use unset to remove the old elements

merge two array result wrong array which contain second array as array rather a complete of both

I tries to array_merge in php but resultant array is not correct
1. Array ( [id] => 12 [name] => Popular )
2. Array ( [0] => Array ( [id] => 8 [name] => Flowers ) [1] => Array ( [id] => 10 [name] => Chocolates ) [2] => Array ( [id] => 11 [name] => Sweets and Dry Fruits ) )
Resultant Array
Array ( [id] => 12 [name] => Popular [0] => Array ( [id] => 8 [name] => Flowers ) [1] => Array ( [id] => 10 [name] => Chocolates ) [2] => Array ( [id] => 11 [name] => Sweets and Dry Fruits ) )
If you just want to add the new data in the same format as the existing data then use [] rather than array_merge().
$array1 = array( 'id' => 12, 'name' => 'Popular');
$array2 = array(array( 'id' => 8, 'name' => 'Flowers'),
array( 'id' => 10, 'name' => 'Chocolates'),
array( 'id' => 11, 'name' => 'Sweets and Dry Fruits')
);
$array2[] = $array1;
print_r($array2);
outputs...
Array
(
[0] => Array
(
[id] => 8
[name] => Flowers
)
[1] => Array
(
[id] => 10
[name] => Chocolates
)
[2] => Array
(
[id] => 11
[name] => Sweets and Dry Fruits
)
[3] => Array
(
[id] => 12
[name] => Popular
)
)
If you want the data to be at the front, then you need to create an array of that data and then use array_merge()...
$array3 = array_merge(array($array1), $array2);
print_r( $array3);

PHP preg-match-all regex

I would like to capture each of these in their own group with preg_match_all in PHP.
The chapter, section, or page
The number (or letter if it has one) of the specified chapter, section, or page. If there is a single space between them it should be taken into account
The words "and", "or"
Keeping in mind that the number of items in the string may be dynamic, the regex should work on all the examples below:
Ch1 and Sect2b
Ch 4 x blahunwantedtext and Sect 5y and Sect6 z and Ch7 or Ch8
This is what I managed to come up with so far:
<?php
$str = 'Ch 1 a and Sect 2b and Pg3';
preg_match_all ('/([a-z]+)([\s]?[0-9]+)([\s]?[a-z]*)([\s]?and*[\s]?)/is', $str, $matches);
Array
(
[0] => Array
(
[0] => Ch 1 a and
[1] => Sect 2b and
)
[1] => Array
(
[0] => Ch
[1] => Sect
)
[2] => Array
(
[0] => 1
[1] => 2
)
[3] => Array
(
[0] => a
[1] => b
)
[4] => Array
(
[0] => and
[1] => and
)
)
I'm unable to match the last portions of the string (Pg3) in my array.
The expected result should be:
Array
(
[0] => Array
(
[0] => Ch 1 a and
[1] => Sect 2b and
[2] => Pg3
)
[1] => Array
(
[0] => Ch
[1] => Sect
[2] => Pg
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[3] => Array
(
[0] => a
[1] => b
[2] =>
)
[4] => Array
(
[0] => and
[1] => and
[2] =>
)
)
This regex should work /(ch|sect|pg)\s*(\d)\s*([a-z]?\b)\s*(and|or)?/i:
$str = 'Ch 1 a and Sect 2b and Pg3';
preg_match_all('/(ch|sect|pg)\s*(\d)\s*([a-z]?\b)\s*(and|or)?/i', $str, $matches);
array (size=5)
0 =>
array (size=3)
0 => string 'Ch 1 a and' (length=10)
1 => string 'Sect 2b and' (length=11)
2 => string 'Pg3' (length=3)
1 =>
array (size=3)
0 => string 'Ch' (length=2)
1 => string 'Sect' (length=4)
2 => string 'Pg' (length=2)
2 =>
array (size=3)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
3 =>
array (size=3)
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string '' (length=0)
4 =>
array (size=3)
0 => string 'and' (length=3)
1 => string 'and' (length=3)
2 => string '' (length=0)

convert multidimensional array into two separated

I have a multidimensional Array like this:
Array (
[0] => Array (
[id] => 1
[name] => privilages1
)
[1] => Array (
[id] => 2
[name] => privilages2
)
[2] => Array (
[id] => 3
[name] => privilages3
)
[3] => Array (
[id] => 4
[name] => privilage4 )
[4] => Array (
[id] => 5
[name] => privilages5 )
)
and i want to compare it with another array, which looks like this:
Array (
[0] => Array (
[id] => 1 )
[1] => Array (
[id] => 2)
)
if the value of id matches, then i want to all values from the first example.
How can i do this?
You can use array_filter to filter the array elements you want by supplying a user defined callback function.
Here is the code:
$arr = array( array('id' => 1, 'name' => 'foo'),
array('id' => 2, 'name' => 'bar'),
array('id' => 3, 'name' => 'baz'),
array('id' => 4, 'name' => 'wow'));
$ret = array_filter($arr, create_function('$el',
'static $search=array(array("id" => 1), array("id" => 2));
$n=array("id" => $el["id"]);
return (array_search($n, $search) !== false);'));
print_r($ret);
OUTPUT
Array
(
[0] => Array
(
[id] => 1
[name] => foo
)
[1] => Array
(
[id] => 2
[name] => bar
)
)

Categories