i have this array structure and i want to create other array structure with that:
array (size=7)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
1 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
2 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
3 =>
array (size=9)
0 => string 'Verde' (length=5)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
4 =>
array (size=9)
0 => string 'Azul' (length=4)
1 => string '128GB' (length=5)
2 => string 'Cristal' (length=7)
5 =>
array (size=9)
0 => string 'Azul' (length=4)
1 => string '128GB' (length=5)
2 => string 'Plastico' (length=8)
6 =>
array (size=9)
0 => string 'Azul' (length=4)
1 => string '64GB' (length=4)
2 => string 'Cristal' (length=7)
Each array item have a group of characteristics like:
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
....
0 => string 'Azul' (length=4)
1 => string '128GB' (length=5)
2 => string 'Cristal' (length=7)
I need an array that have this values like
Azul=>64GB=>Plastico
128GB=>Cristal
if sub item repeats only write the children value
Azul=>64GB=>Cristal Only adds Cristal because 64GB exists
I have this code in php:
foreach ($arrCaracteristicas as $itemMadre => $valueMadre) {
$NombreNodo = $valueMadre[0];
if ($NumeroCaracteristicas > 1) {
for ($i = 1; $i <= $NumeroCaracteristicas - 1; $i++) {
$NombreCaract = $valueMadre[$i];
echo "$NombreNodo: $NombreCaract <br>";
array_push( $arrSelectMain[$NombreNodo], $NombreCaract);
if (!isset($arrSelectMain[$NombreNodo][$NombreCaract])) {
//$arrSelectMain[$NombreNodo]["d"] = $NombreCaract;
}
}
}
}
Where $arrCaracteristicas have the long array, and $arrSelectMain have the first level of the array keys:
Code of first level:
foreach ($arrCaracteristicas as $item => $value) {
$arrSelectMain[$value[0]] = $value[0];
}
asort($arrSelectMain);
$arrSelectMain = array_unique($arrSelectMain);
This is an image with structure that i want:
https://ibb.co/Q8k198Y
If somebody can help me i will invite a beer in paypal :)
This should work, first separate the array into columns and then loop over the items.
$first = array_column($items, 0);
$second = array_column($items, 1);
$third = array_column($items, 2);
$output = [];
for ($i = 0; $i < count($first); $i++) {
$output[$first[$i]][$second[$i]][] = $third[$i];
}
Have a look at the sample
Related
I ‘ve got an array like this =>
this is a result from a google analytics request. I asked for the amout of visits for the last three months.
$statPerMonth
array (size=2)
'08' => // The month (August)
array (size=34)
0 =>
array (size=3)
0 => string '08' (length=2) // Month again
1 => string 'admin.testweb.fr' (length=19) // host
2 => string '1' (length=1) // amount of visits
1 =>
array (size=3)
0 => string '08' (length=2)
1 => string 'audigie-espace-auto.reseau-fivestar.fr' (length=38)
2 => string '6' (length=1)
2 =>
array (size=3)
0 => string '08' (length=2)
1 => string 'www.audigie-espace-auto.reseau-fivestar.fr' (length=31)
2 => string '9' (length=1)
3 =>
array (size=3)
0 => string '08' (length=2)
1 => string 'carrosserie-abberis.reseau-fivestar.fr' (length=38)
2 => string '7' (length=1)
'07' =>
array (size=47)
0 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'www.anothersite.testweb.fr' (length=13)
2 => string '1' (length=1)
1 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'admin.testweb.fr' (length=16)
2 => string '2' (length=1)
2 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'admin.testweb.fr' (length=19)
2 => string '1' (length=1)
3 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'audigie-espace-auto.reseau-fivestar.fr' (length=38)
2 => string '20' (length=2)
4 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'www.admin.testweb.fr' (length=19)
2 => string '1' (length=1)
This array respresent the amount of visits for my websites but
you can see that the values [‘08’][‘1’] and [‘08’][‘2’] are identicals (only ‘www.’ differs)
I want to merge those cells and add their value (because it’s the same site !) in order to get the total amount of visits for a site with it’s two hostnames.
Consider $sites as an array of Site Object (websites).
the getHost() method will return the site host for exemple ‘my-host.fr’ without the ‘www’
consider $statsPerMonth array explained above
finally consider this algorithm
foreach ($statsPerMonth as $actualMonth => $stats) {
foreach($sites as $site) {
$siteHost = $site->getHost();
foreach ($stats as $row) {
if (strstr($row['1'], $siteHost)) {
if(isset($globalStats[$actualMonth][$siteHost])) {
$globalStats[$actualMonth][$siteHost] = $globalStats[$actualMonth][$siteHost] + $row['2'];
} else {
$globalStats[$actualMonth][$siteHost] = 0;
$globalStats[$actualMonth][$siteHost] = $globalStats[$actualMonth][$siteHost] + $row['2'];
}
}
if(!isset($globalStats[$actualMonth][$siteHost])) {
$globalStats[$actualMonth][$siteHost] = 0;
}
}
}
}
This algorithm return the $globalStats array in this form
array (size=3)
'08' =>
array (size=43)
'carrosserie-la-cascade.reseau-fivestar.fr' => int 1
'audigie-espace-auto.reseau-fivestar.fr' => int 15
'carrosserie-abberis-fivestar.fr' => int 16
'carrosserie-arenales-jonathan.reseau-fivestar.fr' => int 0
'07' =>
array (size=43)
'carrosserie-la-cascade.reseau-fivestar.fr' => int 2
'audigie-espace-auto.reseau-fivestar.fr' => int 20
'carrosserie-abberis-fivestar.fr' => int 0
'carrosserie-arenales-jonathan.reseau-fivestar.fr' => int 4
'06' =>
array (size=43)
'carrosserie-la-cascade.reseau-fivestar.fr' => int 0
'audigie-espace-auto.reseau-fivestar.fr' => int 29
'carrosserie-abberis-fivestar.fr' => int 0
'carrosserie-arenales-jonathan.reseau-fivestar.fr' => int 4
This is exaclty what I want but I think we can improve this algorithm to make it more efficient (because the arrays are big). Have you any idea in order to make this algorithm better ?
Thank you.
Try to replace all "foreach" by "for", such as :
foreach ($array as $key => $value) {
}
by
$keys = array_keys($array);
$keyCount = count($keys);
for($i = 0; $i < $keyCount; $i++) {
$key = $keys[$i];
$value = $array[$key];
}
I have the following multidimensional array:
array (size=2)
10 =>
array (size=7)
1 =>
array (size=4)
0 => string '10view1' (length=7)
1 => string '10ins1' (length=6)
2 => string '10edit1' (length=7)
3 => string '10del1' (length=6)
2 =>
array (size=4)
0 => string '10view2' (length=7)
1 => string '10ins2' (length=6)
2 => string '10edit2' (length=7)
3 => string '10del2' (length=6)
3 =>
array (size=4)
0 => string '10view3' (length=7)
1 => string '10ins3' (length=6)
2 => string '10edit3' (length=7)
3 => string '10del3' (length=6)
11 =>
array (size=7)
1 =>
array (size=4)
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
2 =>
array (size=4)
0 => string '11view2' (length=7)
1 => string '11ins2' (length=6)
2 => string '11edit2' (length=7)
3 => string '11del2' (length=6)
3 =>
array (size=4)
0 => string '11view3' (length=7)
1 => string '11ins3' (length=6)
2 => string '11edit3' (length=7)
3 => string '11del3' (length=6)
I want to convert it to the array like:
1 => array(10 => array(
0 => string '10view1' (length=7)
1 => string '10ins1' (length=6)
2 => string '10edit1' (length=7)
3 => string '10del1' (length=6)),
11 => array(
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
)
),
2 => array(
10 => array(
0 => string '10view2' (length=7)
1 => string '10ins2' (length=6)
2 => string '10edit2' (length=7)
3 => string '10del2' (length=6)
),
11 => array(
0 => string '11view2' (length=7)
1 => string '11ins2' (length=6)
2 => string '11edit2' (length=7)
3 => string '11del2' (length=6)
)
),
3 => array(
10 => array(
0 => string '11view3' (length=7)
1 => string '11ins3' (length=6)
2 => string '11edit3' (length=7)
3 => string '11del3' (length=6)
),
11 => array(
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
)
)
please help me i confuse to solve this problem thanks in advance.
It's quite obvious for me how to implement this using foreach loop, but I wonder if it's possible to do this with PHP array functions like array_map or array_walk. Please use PHP 5.3 for the callback function. Thank you!
Using array map you can achieve like this
<?php
function merge_array($n, $m)
{
$data = array();
$data[10] = $n;
$data[11] = $m;
return $data;
}
$c = array_map("merge_array", $array[10], $array[11]);
print_r($c);
Using for loop it is very simple you can achieve like this
$count_10 = count($array[10]);
$count_11 = count($array[11]);
$count = ($count_10>$count_11)?$count_10:$count_11;
$data = array();
for($i=0; $i<$count; $i++)
{
if(isset($array[10][$i]))
{
$data[$i][] = $array[10][$i];
}
if(isset($array[11][$i]))
{
$data[$i][] = $array[11][$i];
}
}
I've been trying to work with the array_search function.
This is my array :
array (size=23)
0 =>
array (size=3)
0 => string '15' (length=2)
1 => float 115.08386533184
2 => string '2014-02-06 21:00:00' (length=19)
1 =>
array (size=3)
0 => string '81' (length=2)
1 => float 100.41587590619
2 => string '2014-03-28 00:00:00' (length=19)
2 =>
array (size=3)
0 => string '65' (length=2)
1 => float 99.096448338334
2 => string '2014-02-08 21:00:00' (length=19)
3 =>
array (size=3)
0 => string '53' (length=2)
1 => float 98.752479251378
2 => string '2014-03-05 12:00:00' (length=19)
4 =>
array (size=3)
0 => string '24' (length=2)
1 => float 98.303557178126
2 => string '2014-02-07 21:00:00' (length=19)
5 =>
array (size=3)
0 => string '23' (length=2)
1 => float 98.270536817788
2 => string '2014-02-08 21:30:00' (length=19)
6 =>
array (size=3)
0 => string '37' (length=2)
1 => float 98.139812350661
2 => string '2014-02-06 23:00:00' (length=19)
7 =>
array (size=3)
0 => string '13' (length=2)
1 => float 97.810954038756
2 => string '2014-02-09 19:45:00' (length=19)
8 =>
array (size=3)
0 => string '22' (length=2)
1 => float 95.975221202728
2 => string '2014-02-11 20:30:00' (length=19)
9 =>
array (size=3)
0 => string '66' (length=2)
1 => float 94.115075316114
2 => string '2014-02-04 19:00:00' (length=19)
10 =>
array (size=3)
0 => string '52' (length=2)
1 => float 93.957291067159
2 => string '2014-03-15 00:00:00' (length=19)
11 =>
array (size=3)
0 => string '12' (length=2)
1 => float 89.188128768086
2 => string '2014-05-17 10:00:00' (length=19)
12 =>
array (size=3)
0 => string '51' (length=2)
1 => float 88.381986169995
2 => string '2014-03-27 12:00:00' (length=19)
13 =>
array (size=3)
0 => string '98' (length=2)
1 => float 85.240471279545
2 => string '2014-02-17 18:00:00' (length=19)
14 =>
array (size=3)
0 => string '92' (length=2)
1 => float 82.721601210972
2 => string '2014-02-06 20:15:00' (length=19)
15 =>
array (size=3)
0 => string '82' (length=2)
1 => float 82.473535719129
2 => string '2014-02-27 17:00:00' (length=19)
16 =>
array (size=3)
0 => string '121' (length=3)
1 => float 56.833974620724
2 => string '2014-02-07 22:30:00' (length=19)
17 =>
array (size=3)
0 => string '120' (length=3)
1 => float 36.269423317467
2 => string '2014-02-06 21:30:00' (length=19)
18 =>
array (size=3)
0 => string '83' (length=2)
1 => float 35.863780709688
2 => string '2014-02-21 07:00:00' (length=19)
19 =>
array (size=3)
0 => string '35' (length=2)
1 => float 24.322967350005
2 => string '2014-02-07 21:30:00' (length=19)
20 =>
array (size=3)
0 => string '6' (length=1)
1 => float 19.71405484708
2 => string '2014-02-14 22:00:00' (length=19)
21 =>
array (size=3)
0 => string '45' (length=2)
1 => float 19.142717452481
2 => string '2014-03-15 00:00:00' (length=19)
22 =>
array (size=3)
0 => string '117' (length=3)
1 => float 17.641278106673
2 => string '2014-02-18 17:30:00' (length=19)
Now - The thing is I'm using array_search in order to get the key of a specific ID (Note : The ID is the first value in each array). The thing is, it doesn't work, probably because it can't get the key of the array itself.
Let me show you an example : ($array = The array above)
$r = array_search(53, array_keys($array));
I want $r to be equal to 3, because thats they array's key position.
Any idea how I can do it?
$r = array_search('53', array_map(function($x) { return $x[0]; }, $array));
array_map will construct an array of element 0 of each element of the array. Then you search for 53 in this, and $r will be its position.
Or you could simply write a loop:
foreach ($array as $i => $el) {
if ($el[0] == '53') {
$r = $i;
$result = $el;
break;
}
}
I searched SO and found a similar topic: PHP Multidimensional Array - Search for value and get the sub-array.
I slightly modified one of the solutions provided above.
$id=53;
$r=null;
$data=null;
foreach ($array as $idx=>$subArray) {
if ( $subArray[0] == $id ) {
$r=$idx;
$target=$subArray;
break;
}
}
use array_map function and array_filter function.
function find($a, $id) {
return ($a[0] == $id) ? $a : false;
}
function filter($a) {
return $a;
}
$r = array_filter(array_map("find", $array, array(53)), "filter");
A better call than array_map() to isolate the first column of values is: array_column(). This is the task that the function was created for.
Use this implementation:
$r = array_search('53', array_column($array,0));
Done.
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;
}
I have a array:
$array1 = array
0 =>
array
0 => string 'biodata' (length=7)
1 => string 'family_name' (length=11)
1 =>
array
0 => string 'biodata' (length=7)
1 => string 'first_name' (length=10)
2 =>
array
0 => string 'biodata_education' (length=17)
1 => string 'subject' (length=7)
3 =>
array
0 => string 'biodata_education' (length=20)
1 => string 'year' (length=5)
which need to converted like:
array
biodata =>
array
0 => string 'family_name' (length=7)
1 => string 'first_name' (length=11)
biodata_education =>
array
0 => string 'subject' (length=7)
1 => string 'year' (length=10)
as it can be done by simple iteration, I tried this one and done.
foreach($array1 as $tbl):
$table[$tbl[0]][] = $tbl[1];
endforeach;
<?php
//map the array using a foreach loop
foreach($array1 as $tbl)
{
$table[ $tbl[0] ][] = $tbl[1];
}