I'm trying to match two regex using a single preg_match_all and I need to get 2 arrays containing the matches.
This is my current code:
$s = 'abab';
preg_match_all('/(a)|(b)/', $s, $a);
var_dump($a);
It works quite well, if don't consider that the resulting multi-dimensional array is:
array (size=3)
0 =>
array (size=4)
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'a' (length=1)
3 => string 'b' (length=1)
1 =>
array (size=4)
0 => string 'a' (length=1)
1 => string '' (length=0)
2 => string 'a' (length=1)
3 => string '' (length=0)
2 =>
array (size=4)
0 => string '' (length=0)
1 => string 'b' (length=1)
2 => string '' (length=0)
3 => string 'b' (length=1)
So, I could use $a[1] and $a[2] but as you can see, these array have empty values on the "not matched" keys.
I'd need:
array (size=3)
0 =>
array (size=4)
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'a' (length=1)
3 => string 'b' (length=1)
1 =>
array (size=2)
0 => string 'a' (length=1)
1 => string 'a' (length=1)
2 =>
array (size=2)
0 => string 'b' (length=1)
1 => string 'b' (length=1)
There is some parameter to pass to preg_match_all to get a clean output without empty values? I've already tried array_filter() but it doesn't remove them and I'm looking for a short solution so I can't use the callback of array_filter (need it for a code-golf challenge).
No idea, why do you need it this way, but shortest I have come up with to satisfy your request.
$s = 'abab';
preg_match_all('/(a)|(b)/', $s, $a);
// filter all at once
$a = array_map('array_filter', $a);
var_dump($a);
Related
I want to change the key of my array in php.
Here an exemple :
array (size=5)
0 =>
array (size=2)
'iden' => string '01' (length=8)
'don' => string '17' (length=2)
1 =>
array (size=2)
'iden' => string '02' (length=8)
'don' => string '17' (length=2)
2 =>
array (size=2)
'iden' => string '03' (length=8)
'don' => string '17' (length=2)
And I want to change my array like this :
array (size=5)
0 =>
array (size=2)
0 => string '01' (length=8)
1 => string '17' (length=2)
1 =>
array (size=2)
0 => string '02' (length=8)
1 => string '17' (length=2)
2 =>
array (size=2)
0 => string '03' (length=8)
1 => string '17' (length=2)
Thanks in advance
You can use the array_values function to remove named keys:
foreach($array as &$item) {
$item = array_values($item);
}
unset($item); // Remove reference
Note the & in the foreach. This creates a reference in the $item variable to the corresponding array element which means you can edit it in your loop.
If you want, you can also write this in a single line using array_map:
$array = array_map("array_values", $array);
i have this array structure, and i want to find and compare with other array that i have.
This is the array that i want search:
array (size=7)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
2 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
3 =>
array (size=9)
0 => string 'Verde' (length=5)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
An this is the array with search values:
Array
(
[0] => Blanco
[1] => 32GB
[2] => Plastico
)
I have the key and value, but i need find, in this example the main key is 1 in the long and main array, how can i get that?
PD: the number of search values are the numbers inside main arrays
Let's say that elements is the name of the array that you want to iterate over and find the index whose first three values match the search values. You can simply iterate over the elements, checking if the values match and if they do, then you can store the index in a variable called $wantedKey:
$target = array(
'0' => Blanco
'1' => 32GB
'2' => Plastico
);
$wantedKey = null;
foreach($elements as $key => $elem){
if($elem[0] == $target[0] && $elem[1] == $target[1] && $elem[2] == $target[2]){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
In case you have an arbitrary amount of values, you can use a foreach to iterate over them and check if they match:
foreach($elements as $key => $elem){
$sameValues = true;
foreach($target as $t_key => $t_value){
if($elem[$t_key] != $t_value){
$sameValues = false;
break;
}
}
if($sameValues){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
I have this array $mergeArr:
array (size=5)
'facebook' =>
array (size=3)
'facebook_enabled' => string '1' (length=1)
'facebook_url' => string 'https://www.facebook.com/' (length=25)
'facebook_order' => string '7' (length=1) //order element
'twitter' =>
array (size=3)
'twitter_enabled' => string '1' (length=1)
'twitter_url' => string 'https://www.twitter.com/' (length=24)
'twitter_order' => string '9' (length=1) //order element
'instagram' =>
array (size=3)
'instagram_enabled' => string '1' (length=1)
'instagram_url' => string 'https://www.instagram.com/' (length=26)
'instagram_order' => string '2' (length=1) //order element
'linkedin' =>
array (size=3)
'linkedin_enabled' => string '1' (length=1)
'linkedin_url' => string 'https://www.linkedin.com/' (length=25)
'linkedin_order' => string '5' (length=1) //order element
'pintrest' =>
array (size=3)
'pinterest_enabled' => string '1' (length=1)
'pinterest_url' => string 'https://www.pinterest.com/' (length=26)
'pinterest_order' => string '3' (length=1) //order element
I need to sort it according the *_order element in each array.
I tried the code below:
CODE PHP:
array_multisort(array_column($mergeArr, '2'), SORT_ASC, $mergeArr);
Expected output order is: Instagram, Pinterest, Linkedin, Facebook, Twiter.
The error that I receive is the following
array_multisort(): Array sizes are inconsistent
Can you please tell me how can I sort this array so I get what I want?
Thanks in advance!
If the field to compare is always in the 3-th key you can do that with usort and array-values as:
usort($array, function($a, $b) {
$a = array_values($a);
$b = array_values($b);
return $a[2] > $b[2];
});
Live example: 3v4l
I got a array of array in PHP that look like this :
array (size=3)
0 =>
array (size=3)
0 => string 'abc' (length=3)
1 => string 'def' (length=3)
2 => string 'ghi' (length=3)
1 =>
array (size=3)
0 => string '01234' (length=5)
1 => string '01234' (length=5)
2 => string '01234' (length=5)
2 =>
array (size=3)
0 => string '98765' (length=5)
1 => string '98765' (length=5)
2 => string '98765' (length=5)
Now I want the first array to be the key of a assosiative array for the rest of the parent array, or kind :
array (size=2)
0 =>
array (size=3)
'abc' => string '01234' (length=5)
'def' => string '01234' (length=5)
'ghi' => string '01234' (length=5)
1 =>
array (size=3)
'abc' => string '98765' (length=5)
'def' => string '98765' (length=5)
'ghi' => string '98765' (length=5)
EDIT: But I can only get the first array like this to define the header :
$header = reset($tabOfTabs);
You can try this -
$indexes = array_shift($your_array); // pop out the first array to set the indexes
foreach($your_array as $key => $array) {
$your_array[$key] = array_combine($indexes, $array); // combine the keys & sub-arrays
}
Demo
I'm really stuck at a certain point in my project. I'm trying to convert a two-dimensional array into a three-dimensional array. Currently I´m at this point:
array (size=4)
0 =>
array (size=4)
0 => string 'foo' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '0' (length=1)
1 =>
array (size=4)
0 => string 'bar' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '1' (length=1)
2 =>
array (size=4)
0 => string 'bas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '1' (length=1)
3 =>
array (size=4)
0 => string 'tas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '0' (length=1)
What I would like to do is make a new array with key values of array[ ][3] (I hope this makes any sense) so something like this:
array (size=2)
0 => array (size=2)
0 => array (size=2)
0 => string 'foo' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => string 'tas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => array (size=2)
0 => string 'bar' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => string 'bas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
Right now this is the best way I can explain it, I'm not sure if I make any sense or if this is even possible, but if anyone could shed a light on this it would be much appreciated!
Thanks in advance,
Thomas
$array = array();
$array[] = array('foo','foo','foo',1);
$array[] = array('a','a','a',1);
$array[] = array('b','a','a',0);
$array[] = array('c','a','a',0);
$new_array =group_by_sub_array_key($array,3);
print_r($new_array);
function group_by_sub_array_key($array,$kn){
$new_array = array();
foreach($array AS $v){
$key=$v[$kn];
if(!array_key_exists($v[$kn],$new_array)){
$new_array[$key]=array();
}
unset($v[$kn]);
$new_array[$key][] = $v;
}
return $new_array;
}