Create multidimensional array from rows - php

The result of a query in my database returns something like this (a record for each row):
1.
1.1.
1.1.01.
1.1.01.001
1.2.
1.2.01.
1.2.02.
I'm trying to create something that returns me a multidimensional array in a tree format, like this:
array(
'1.' => array(
'1.1' => array(
'1.1.01.' => array(
(int) 0 => '1.1.01.001'
)
),
'1.2' => array(
(int) 0 => '1.2.01.',
(int) 1 => '1.2.02.'
)
)
)
All I could think to do was reverse the order of elements using explode().
I appreciate any suggestions.

Your format is very tricky because of :
1.2.
1.2.01. |
1.2.02. V Making this array instead of value
You can try
$string = "1.
1.1.
1.1.01.
1.1.01.001
1.2.
1.2.01.
1.2.02.";
$array = explode("\n", $string);
$data = array();
$temp = &$data;
$it = new CachingIterator(new ArrayIterator($array), CachingIterator::FULL_CACHE);
$continue = false;
foreach ( $it as $v ) {
$v = trim($v);
if ($it->hasNext()) {
$next = trim($it->getInnerIterator()->current());
if (stripos($next, $v) === 0) {
$temp = &$temp[$v];
} else {
$temp[] = $v;
if (strlen($next) != strlen($v)) {
$temp = &$data;
}
}
} else {
$temp[] = $v;
}
}
print_r($data);
Output
Array
(
[1.] => Array
(
[1.1.] => Array
(
[1.1.01.] => Array
(
[0] => 1.1.01.001
)
)
)
[1.2.] => Array
(
[0] => 1.2.01.
[1] => 1.2.02.
)
)
Here is a move COMPLEX demo

Related

Count duplicates in treemap

I'm trying to build a tree-map from categories. I have the categories (I have a lot of categories and I want to remove duplicates and show them in a tree-map view with count) and I have the following code:
<?php
$cat = array(
"Sneakers/Men",
"Sneakers/Women",
"Accessories/Jewellery/Men",
"Accessories/Jewellery/Men",
"Accessories/Jewellery/Women",
"Accessories/Jewellery/Men/Bvlgari"
);
$out = [];
foreach ($cat as $str) {
$lookup = &$out;
$parts = explode("/", $str);
foreach ($parts as $part) {
$lookup = &$lookup[$part];
if (!isset($lookup))
$lookup = [];
if ($part == end($parts))
$lookup = is_array($lookup) ? 1 : ++$lookup;
}
}
print_r($out);
OUTPUT:
Array
(
[Sneakers] => Array
(
[Men] => 1
[Women] => 1
)
[Accessories] => Array
(
[Jewellery] => Array
(
[Men] => 3
[Women] => 1
)
)
)
I would like to be:
Array
(
[Sneakers] => Array
(
[Men] => 1
[Women] => 1
)
[Accessories] => Array
(
[Jewellery] => Array
(
[Men] => Array (
[Bvlgari] => 1
)
[Women] => 1
)
)
)
You're losing information with both of those formats and the suggested answer is not going to cut it. You do need recursion and each item needs to hold a count and children.
$cat_paths = [
'Sneakers/Men',
...
];
$cat_counts = $item = [# template of each item
'count' => 0,
'children' => [],
];
foreach ($cat_paths as $cat_path) {
$level = &$cat_counts;# use a reference for recursion
if ($cat_path) {# allow uncategorized to be in root of tree
$cat_path = explode('/', $cat_path);
do {
$cat = array_shift($cat_path);
if (!isset($level['children'][$cat])) {
$level['children'][$cat] = $item;
}
$level = &$level['children'][$cat];# step down into tree
} while ($cat_path);
}
$level['count']++;
}
unset($level);

Update Multidimentional Array with another Array for existing Keys PHP

I have two arrays:
('admin','admin2', 'admin3' can be too many, and names can also differ other than 'admin')
$old = array(
array('admin'=>array('a'=>'aaa','b'=>'bbb')),
array('admin2'=>array('c'=>'ccc','d'=>'ddd'))
);
$new = array(
array('admin2'=>array('e'=>'eee','f'=>'fff')),
array('admin3'=>array('g'=>'ggg','h'=>'hhh'))
);
I want to have this array from both above arrays:
(new array with all different keys in both PLUS similar keys from new array)
$output = array(
array('admin'=>array('a'=>'aaa','b'=>'bbb')),
array('admin2'=>array('e'=>'eee','f'=>'fff')),
array('admin3'=>array('g'=>'ggg','h'=>'hhh'))
);
// Remove one level of array to make arrays as ['admin'=>array, 'admin2'=>array]
$old1 = call_user_func_array('array_merge', $old);
$new1 = call_user_func_array('array_merge', $new);
// Make replacement
$ready = array_replace($old1, $new1);
// Return level making every item as array
$result = array();
foreach($ready as $k=>&$v)
$result[] = array($k=>$v);
print_r($result);
demolink
This code will solve your problem :
<?php
$array1 = array(
array('admin'=>array('a'=>'aaa','b'=>'bbb')),
array('admin2'=>array('c'=>'ccc','d'=>'ddd'))
);
$array2 = array(
array('admin2'=>array('e'=>'eee','f'=>'fff')),
array('admin3'=>array('g'=>'ggg','h'=>'hhh'))
);
$output = $array1; ///merge array1 into output array
foreach($array2 as $key => $val)
{
$is_present_key = false;
$first_key = key($val);
foreach($output as $k => $v)
{
if(array_key_exists($first_key,$output[$k])) ////check if key exit in $output array
{
$output[$k] = $val; ///push new value if key exists in $output
$is_present_key = true;
}
}
if($is_present_key == false)///skip for duplicate of new values if key exists in $output
{
$output[] = $val;
}
}
echo "<pre>"; print_r($output);
?>
This will give you :
Array
(
[0] => Array
(
[admin] => Array
(
[a] => aaa
[b] => bbb
)
)
[1] => Array
(
[admin2] => Array
(
[e] => eee
[f] => fff
)
)
[2] => Array
(
[admin3] => Array
(
[g] => ggg
[h] => hhh
)
)
)
LIVE EXAMPLE
$old = array(
array('admin'=>array('a'=>'aaa','b'=>'bbb')),
array('admin2'=>array('c'=>'ccc','d'=>'ddd'))
);
$new = array(
array('admin2'=>array('e'=>'eee','f'=>'fff')),
array('admin3'=>array('g'=>'ggg','h'=>'hhh'))
);
$arr=array_merge($new,$old);
$new_arr=array();
foreach($arr as $key=>$val){
foreach($val as $k=>$v){
if(array_key_exists($k, $new_arr)){
continue;
}else{
$new_arr[$k]=$v;
}
}
}
echo "<pre>";print_r($new_arr); echo "</pre>";

combine arrays and concat value?

Little complex to explain , so here is simple concrete exemple :
array 1 :
Array
(
[4] => bim
[5] => pow
[6] => foo
)
array 2 :
Array
(
[n] => Array
(
[0] => 1
)
[m] => Array
(
[0] => 1
[1] => 2
)
[l] => Array
(
[0] => 1
[1] => 4
[2] => 64
)
And i need to output an array 3 ,
array expected :
Array
(
[bim] => n-1
[pow] => Array
(
[0] => m-1
[1] => m-2
)
[foo] => Array
(
[0] => l-1
[1] => l-4
[2] => l-64
)
Final echoing OUTPUT expected:
bim n-1 , pow m-1 m-2 ,foo l-1 l-4 l-64 ,
I tried this but seems pity:
foreach($array2 as $k1 =>$v1){
foreach($array2[$k1] as $k => $v){
$k[] = $k1.'_'.$v);
}
foreach($array1 as $res =>$val){
$val = $array2;
}
Thanks for helps,
Jess
CHALLENGE ACCEPTED
<?php
$a = array(
4 => 'bim',
5 => 'pow',
6 => 'foo',
);
$b = array(
'n' => array(1),
'm' => array(1, 2),
'l' => array(1, 4, 64),
);
$len = count($a);
$result = array();
$aVals = array_values($a);
$bKeys = array_keys($b);
$bVals = array_values($b);
for ($i = 0; $i < $len; $i++) {
$combined = array();
$key = $aVals[$i];
$prefix = $bKeys[$i];
$items = $bVals[$i];
foreach ($items as $item) {
$combined[] = sprintf('%s-%d', $prefix, $item);
};
if (count($combined) === 1) {
$combined = $combined[0];
}
$result[$key] = $combined;
}
var_dump($result);
?>
Your code may be very easy. For example, assuming arrays:
$one = Array
(
4 => 'bim',
5 => 'pow',
6 => 'foo'
);
$two = Array
(
'n' => Array
(
0 => 1
),
'm' => Array
(
0 => 1,
1 => 2
),
'l' => Array
(
0 => 1,
1 => 4,
2 => 64
)
);
You may get your result with:
$result = [];
while((list($oneKey, $oneValue) = each($one)) &&
(list($twoKey, $twoValue) = each($two)))
{
$result[$oneValue] = array_map(function($item) use ($twoKey)
{
return $twoKey.'-'.$item;
}, $twoValue);
};
-check this demo Note, that code above will not make single-element array as single element. If that is needed, just add:
$result = array_map(function($item)
{
return count($item)>1?$item:array_shift($item);
}, $result);
Version of this solution for PHP4>=4.3, PHP5>=5.0 you can find here
Update: if you need only string, then use this (cross-version):
$result = array();
while((list($oneKey, $oneValue) = each($one)) &&
(list($twoKey, $twoValue) = each($two)))
{
$temp = array();
foreach($twoValue as $item)
{
$temp[] = $twoKey.'-'.$item;
}
$result[] = $oneValue.' '.join(' ', $temp);
};
$result = join(' ', $result);
As a solution to your problem please try executing following code snippet
<?php
$a=array(4=>'bim',5=>'pow',6=>'foo');
$b=array('n'=>array(1),'m'=>array(1,2),'l'=>array(1,4,64));
$keys=array_values($a);
$values=array();
foreach($b as $key=>$value)
{
if(is_array($value) && !empty($value))
{
foreach($value as $k=>$val)
{
if($key=='n')
{
$values[$key]=$key.'-'.$val;
}
else
{
$values[$key][]=$key.'-'.$val;
}
}
}
}
$result=array_combine($keys,$values);
echo '<pre>';
print_r($result);
?>
The logic behind should be clear by reading the code comments.
Here's a demo # PHPFiddle.
//omitted array declarations
$output = array();
//variables to shorten things in the loop
$val1 = array_values($array1);
$keys2 = array_keys($array2);
$vals2 = array_values($array2);
//iterating over each element of the first array
for($i = 0; $i < count($array1); $i++) {
//if the second array has multiple values at the same index
//as the first array things will be handled differently
if(count($vals2[$i]) > 1) {
$tempArr = array();
//iterating over each element of the second array
//at the specified index
foreach($vals2[$i] as $val) {
//we push each element into the temporary array
//(in the form of "keyOfArray2-value"
array_push($tempArr, $keys2[$i] . "-" . $val);
}
//finally assign it to our output array
$output[$val1[$i]] = $tempArr;
} else {
//when there is only one sub-element in array2
//we can assign the output directly, as you don't want an array in this case
$output[$val1[$i]] = $keys2[$i] . "-" . $vals2[$i][0];
}
}
var_dump($output);
Output:
Array (
["bim"]=> "n-1"
["pow"]=> Array (
[0]=> "m-1"
[1]=> "m-2"
)
["foo"]=> Array (
[0]=> "l-1"
[1]=> "l-4"
[2]=> "l-64"
)
)
Concerning your final output you may do something like
$final = "";
//$output can be obtained by any method of the other answers,
//not just with the method i posted above
foreach($output as $key=>$value) {
$final .= $key . " ";
if(count($value) > 1) {
$final .= implode($value, " ") .", ";
} else {
$final .= $value . ", ";
}
}
$final = rtrim($final, ", ");
This will echo bim n-1, pow m-1 m-2, foo l-1 l-4 l-64.

Nested for each's looping through multi-dimensional array - how to do a conditional on the last value

After a rollercoaster ride, I am "this" close to finalising a script I have been working on.
I have a multi-dimensional array stored in $newarray as below. I have built this array myself so the code to build it can be changed if needs be. But after creation I then loop through it picking out the values I want. I build a new array for each key in the upper array (3 in this case, 111, 222 & 333) and populate each with a a bunch of data objects from next array key down along with some other data.
However, what I need in the case below is to generate each of the 3 arrays (111, 222, 333) twice, once where the final array value = 0 ($the_action) and once where it = '1'. Where it = 1, print it, else where it = 0, do something else.
I also think that the way I loop through arrays with a single value in it is probably not very efficient, and the same goes for using key names as values.
Grateful for any help.
Array
(
[111] => Array
(
[1234] => Array
(
[100000] => Array
(
[20000] => 0
)
)
[1244] => Array
(
[100001] => Array
(
[20001] => 1
)
)
[1255] => Array
(
[100002] => Array
(
[20002] => 1
)
)
)
[222] => Array
(
[1233] => Array
(
[100013] => Array
(
[20013] => 0
)
)
[1241] => Array
(
[100014] => Array
(
[20014] => 1
)
)
)
[333] => Array
(
[15633] => Array
(
[100026] => Array
(
[20026] => 0
)
)
[12144] => Array
(
[100028] => Array
(
[20028] => 0
)
)
)
)
Code to build $newarray ($stack comes from CSV with 5 columns):
$newarray = array();
foreach($stack as $val){
$lineid = $val[0]; $segmentid = $val[1]; $action = $val[2]; $recency = $val[3]; $frequency = $val[4];
$newarray[$lineid][$segmentid][$recency][$frequency] = $action;
}
Code to loop through array:
foreach($newarray as $key => $value) {
$target_pixels = array();
$owner_id = $key;
foreach($value as $key2 => $value2){
$target_pixel = new stdClass;
$target_pixel->conversion_id = $key2;
$target_pixel->negated = false;
foreach($value2 as $key3 => $value3){
$target_pixel->seconds_since_conversion = $key3 * 24 * 60 * 60;
foreach($value3 as $key4 => $value4){
$target_pixel->frequency_min = $key4;
$the_action = $value4;
}
}
$target_pixels[] = $target_pixel;
}
print_r($target_pixels);
}
Since you say you can change the structure of the array, I would go with something along the lines of:
$newarray = array();
foreach($stack as $val){
$lineid = $val[0];
$segmentid = $val[1];
$action = $val[2];
$recency = $val[3];
$frequency = $val[4];
$newarray[$lineid][$segmentid] = array(
'recency' => $recency,
'frequency' => $frequency
'action' => $action
);
}
Then your code would look like:
foreach ($newarray as $lineid => $line) {
$target_pixels = array();
$owner_id = $lineid;
foreach ($line as $segmentid => $segment){
$target_pixel = new stdClass;
$target_pixel->conversion_id = $segmentid;
$target_pixel->negated = false;
$target_pixel->seconds_since_conversion = $segment['recency'] * 24 * 60 * 60;
$target_pixel->frequency_min = $segment['frequency'];
$target_pixels[$segment['action']][] = $target_pixel;
}
var_dump($target_pixels);
}

Combine arrays to form multidimensional array in php

I know there's a ton of answers but I can't seem to get it right. I have the following arrays and what I've tried:
$a = array ( 0 => '1421' , 1 => '2241' );
$b = array ( 0 => 'teststring1' , 1 => 'teststring2' );
$c = array ( 0 => 'teststring3' , 1 => 'teststring4' );
$d = array ( 0 => 'teststring5' , 1 => 'teststring6' );
$e = array_combine($a, array($b,$c,$d) );
But with this I get the error array_combine() [function.array-combine]: Both parameters should have an equal number of elements.
I know it's because the $a's array values aren't keys. That's why I'm coming here to see if I could get some help with an answer that can help me make it look something like this:
array(2) {
[1421]=>array( [0] => teststring1
[1] => teststring3
[2] => teststring5
)
[2241]=>array( [0] => teststring2
[1] => teststring4
[2] => teststring6
)
}
If you have control over creating the arrays, you should create them like:
$a = array ('1421' ,'2241');
$b = array ('teststring1', 'teststring3', 'teststring5');
$c = array ('teststring2', 'teststring4', 'teststring6');
$e = array_combine($a, array($b,$c) );
If not, you have to loop over them:
$result = array();
$values = array($b, $c, $d);
foreach($a as $index => $key) {
$t = array();
foreach($values as $value) {
$t[] = $value[$index];
}
$result[$key] = $t;
}
DEMO
Here is a one-liner in a functional coding style. Calling array_map() with a null function parameter followed by the "values" arrays will generate the desired subarray structures. array_combine() does the key=>value associations.
Code (Demo)
var_export(array_combine($a, array_map(null, $b, $c, $d)));
Output:
array (
1421 =>
array (
0 => 'teststring1',
1 => 'teststring3',
2 => 'teststring5',
),
2241 =>
array (
0 => 'teststring2',
1 => 'teststring4',
2 => 'teststring6',
),
)
Super clean, right? I know. It's a useful little trick when you don't have control of the initial array generation step.
Here's a new version of array_merge_recursive which will handle integer keys. Let know how it goes.
$a = array ( 0 => '1421' , 1 => '2241' );
$b = array ( 0 => 'teststring1' , 1 => 'teststring2' );
$c = array ( 0 => 'teststring3' , 1 => 'teststring4' );
$d = array ( 0 => 'teststring5' , 1 => 'teststring6' );
$e = array_combine($a, array_merge_recursive2($b, $c, $d));
echo "<pre>";
print_r($e);
function array_merge_recursive2() {
$args = func_get_args();
$ret = array();
foreach ($args as $arr) {
if(is_array($arr)) {
foreach ($arr as $key => $val) {
$ret[$key][] = $val;
}
}
}
return $ret;
}

Categories