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;
}
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 2 product id and my code is:
$Final=array();
foreach ($ids as $me)
{
$op=DB::table('product')->where(['id'=>$me])->get();
//$Final[]= $op;
array_push($Final,$op);
}
This code returns:
array (size=1)
0 =>
array (size=1)
0 =>
array (size=15)
'id' => string '34' (length=2)
'title' => string 'گوسفند' (length=12)
'title_url' => string 'sheep' (length=5)
'code' => string 'eerer' (length=5)
'code_url' => string 'eerer' (length=5)
'content' => string '<p>sheep</p>
' (length=14)
'cat' => string '68' (length=2)
'price' => string '50000' (length=5)
'product_state' => string '1' (length=1)
'date' => string '' (length=0)
'order_number' => string '0' (length=1)
'Special' => string '0' (length=1)
'View' => string '0' (length=1)
'number_product' => string '1' (length=1)
'discounts' => string '' (length=0)
I need to remove
array (size=2) 0 => array (size=1) 0 =>
$ids => filter id
for get product number for example (22,34)
I Think you should try this.
$Final=array();
foreach ($ids as $me){
$op=DB::table('product')->where(['id'=>$me])->get();
if($op) {
array_push($Final,$op[0]);
}
}
Then you will get these values.
array (size=2)
0 =>
array (size=15)
'id' => string '34' (length=2)
1 =>
array (size=15)
'id' => string '22' (length=2)
If you are using Any framework then framwork provide us some methods to run query with where in to get all the records in single query.
$op=DB::table('product')->whereIn('id'=>$ids)->get();
you will get array of collection for all the products.
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 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);