multidimensional array from nested array - php

I have a script that goes through a CSV file and puts each row as an array into another array (nested array?). Each row has 2-3 fields for the category of the item in that row. I'm trying to work through how to create a multidimensional array out of these categories. Here is the source I currently have:
$csv = new File_CSV_DataSource;
if ($csv->load($file)) {
$items = $csv->getHeaders();
$csv->getColumn($items[2]);
if ($csv->isSymmetric()) {
$items = $csv->connect();
} else {
$items = $csv->getAsymmetricRows();
}
$items = $csv->getrawArray();
}
$mainCats = array();
$subCats = array();
$subSubs = array();
foreach($items as $item){
if(!in_array($item[10], $mainCats)){
$mainCats[] = $item[10];
}
}
foreach($items as $item){
if(!array_key_exists($item[11], $subCats)){
$parent = array_search($item[10], $mainCats);
$subCats[$item[11]] = $parent;
}
}
foreach($items as $item){
if(!array_key_exists($item[12], $subSubs)){
$parent = array_search($item[11], array_keys($subCats));
$subSubs[$item[12]] = $parent;
}
}
What this does so far is create 3 arrays with the format of:
$mainCats = Array(
[0] => Main Cat 1,
[1] => Main Cat 2,
[2] => Main Cat 3
);
$subCats = Array(
[Sub Cat 1] => 0,
[Sub Cat 2] => 1,
[Sub Cat 3] => 2
);
$subSubs = Array(
[Sub Sub 1] => 0,
[Sub Sub 2] => 1,
[Sub Sub 3] => 2
);
The numeric values of each of the last 2 arrays are the index of their parent category in the previous array. What I would like to do is to merge them all into one large array in the format of:
$cats = Array(
[0] => Array(
'name' => Main Cat 1,
'subs' => Array(
[0] => Array(
'name' => Sub Cat 1,
'subs' => Array(
'name' => Sub Sub 1
)
)
)
),
[1] => Array(
'name' => Main Cat 2,
'subs' => Array(
[0] => Array(
'name' => Sub Cat 2,
'subs' => Array(
'name' => Sub Sub 2
)
)
)
),
[2] => Array(
'name' => Main Cat 3,
'subs' => Array(
[0] => Array(
'name' => Sub Cat 3,
'subs' => Array(
'name' => Sub Sub 3
)
)
)
),
);
I know there has to be a far more efficient way of doing this, but I can't figure it out.
EDIT - I should also mention that not all rows have a 3rd category field value.

I prefer to index them by name:
$cats=array();
//--------
foreach($items as $item){
$main=$item[10];
$subCat=$item[11];
$subSub[$item[12]];
$cats[$main]['subs'][$subCat]['subsubs'][$subSub]['name']=$subSub;
$cats[$main]['subs'][$subCat]['name']=$subCat;
$cats[$main]['name']=$main;
}

Related

Loop into multidimensional array from top to bottom

I have this tree :
Array
(
[0] => Array
(
[id] => 1
[parent_id] => 0
[title] => Parent Page
[children] => Array
(
[0] => Array
(
[id] => 2
[parent_id] => 1
[title] => Sub Page
),
[1] => Array
(
[id] => 5
[parent_id] => 1
[title] => Sub Page 2
)
)
)
[1] => Array
(
[id] => 4
[parent_id] => 0
[title] => Another Parent Page
)
)
And I'm looking for a display from top to bottom.
And display something like this :
1
1.2
1.5
4
But if I have id 3 which is a leaf from 5 I would like this :
1
1.2
1.5
1.5.3
4
I have search a lot and my brain is limited when i'm using recursivity..
I have tried this :
function printAll($a){
foreach ($a as $v){
if (!array_key_exists('children', $v)){
debugLog($v['id']);
return;
}
else{
$arrayChildrens = $v['children'];
foreach($arrayChildrens as $c){
$arrayChildrens = $c['children'];
$this->printAll($arrayChildrens);
}
}
}
}
But doesn't work..
I tried to begin just to display
1
2
5
4
But my goal is to display id parents before id ( like Ishowed you before)
Thanks a lot !
This function should give you your expected output.
function printAll($a, $prefix = '') {
//loop through $a
foreach($a as $v) {
//echo current level `id` with previous `$prefix`
echo "{$prefix}{$v['id']}\n";
//check if current level contains children
if(!empty($v['children'])) {
//clean up prefix to remove extra `.` at the end of prefixes
$prev_prefix = rtrim($prefix, '.');
//recurse printAll again passing the children as `$a` and a `$prefix` being the previous levels combined e.g `1.5`
//also clean up extra periods at the start of the prefix
printAll($v['children'], ltrim("{$prev_prefix}.{$v['id']}.", "."));
}
}
}
Output:
1
1.2
1.5
1.5.3
4
Using a proper return
Usually with a function you actually want the function to return values instead of echoing them automatically to your page. If you want this function to return an array of values instead of echoing them, you could do this:
function printAll($a, $level = '', $values = []) {
foreach($a as $v) {
$values[] = $value = "{$level}{$v['id']}";
if(!empty($v['children'])) {
$values = printAll($v['children'], "{$value}.", $values);
}
}
return $values;
}
Which will have a result like this:
Array
(
[0] => 1
[1] => 1.2
[2] => 1.5
[3] => 1.5.3
[4] => 4
)
This should do the job.
$arr = array(
array(
'id' => 1,
'parent_id' => 0,
'title' => 'Parent Page',
'children' => array(
array(
'id' => 2,
'parent_id' => 1,
'title' => 'Sub Page',
),
array(
'id' => 5,
'parent_id' => 1,
'title' => 'Sub Page 2',
'children' => array(
array(
'id' => 7,
'parent_id' => 5,
'title' => 'Sub Page',
),
array(
'id' => 8,
'parent_id' => 5,
'title' => 'Sub Page 2',
)
)
)
)
),
array(
'id' => 4,
'parent_id' => 0,
'title' => 'Another Parent Page',
)
);
function printAll($arr, $parent = [])
{
if (is_array($arr)) {
foreach ($arr as $k => $v) {
if (isset($v['id'])) {
$parent[] = $v['id'];
echo implode('.', $parent) . PHP_EOL;
}
if (isset($v['children'])) {
printAll($v['children'], $parent);
}
array_pop($parent);
}
}
}
printAll($arr);
Output
1
1.2
1.5
1.5.7
1.5.8
4
Working demo.

php convert array into a hierarchical nested set for database

So I read this:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
And i'm using mysql and php
What I need is script that convert only for one time the old data into data with the left/right values.
I don't need to add/update things.
The php data
$in_array = array (
array(
'id' => 400,
'n' => 'Sub 1a',
's' => array (
array (
'n' => 'Sub 1b',
'id' => 421,
's' => array (
array (
'n' => 'Sub 1c',
'id' => 422,
)
)
)
)
),
array(
'id' => 500,
'n' => 'Sub 2a',
's' => array (
array (
'n' => 'Sub 2b',
'id' => 521,
's' => array (
array (
'n' => 'Sub 3b',
'id' => 522,
)
)
)
)
)
);
At the moment i'm trying to solve this with this script but that don't work like it should.
$a_newTree = array();
function rebuild_tree($parent, $left) {
global $a_newTree;
$indexed = array();
// the right value of this node is the left value + 1
$right = $left+1;
// get all children of this node
foreach($parent as $cat){
// recursive execution of this function for each
// child of this node
// $right is the current right value, which is
// incremented by the rebuild_tree function
if(is_array($cat) && array_key_exists('s',$cat)){
$indexed['n'] = $cat['n'];
$right = rebuild_tree($cat, $right);
}
}
// we've got the left value, and now that we've processed
// the children of this node we also know the right value
array_push($a_newTree,array(':lft'=>$left,':rgt'=>$right,':name'=>'bla'));
// return the right value of this node + 1
return $right+1;
}
rebuild_tree($in_array, 1);
I can access mysql and query so the output is like this
Sub 1a | Sub 1b | Sub 1c
Sub 1a | Sub 1b | Sub 1d
Sub 1a | Sub 1b | Sub 1e
Sub 1a | Sub 1f | Null
Sub 1a | Sub 1g | Null
Sub 2a | Sub 2b | Sub 2c
With that data I made the array above.
The array was not oke.
This is working
$in_array = array (
// array(
'id' => 400,
'n' => 'Sub 1a',
's' => array (
// array (
'n' => 'Sub 1b',
'id' => 421,
's' => array (
// array (
'n' => 'Sub 1c',
'id' => 422,
// )
)
// )
// )
),
array(
'id' => 500,
'n' => 'Sub 2a',
's' => array (
// array (
'n' => 'Sub 2b',
'id' => 521,
's' => array (
// array (
'n' => 'Sub 3b',
'id' => 522,
// )
)
// )
)
)
);
$a_newTree = array();
function rebuild_tree($parent, $left) {
global $a_newTree;
$indexed = array();
$right = $left+1;
if(array_key_exists('n',$parent)){
$indexed['n'] = $parent['n'];
}else{
$indexed['n'] = '?';
}
foreach($parent as $cat){
if(is_array($cat)){
$right = rebuild_tree($cat, $right);
}
}
array_push($a_newTree,array(':lft'=>$left,':rgt'=>$right,':name'=>$indexed['n']));
return $right+1;
}
rebuild_tree($in_array, 1);

Building multidimensional arrays in PHP with foreach

How would I go about building output like this in PHP:
$items[] = array(
'section_name' => 'Section 1 Name',
'items' => array(
array(
'item_name' => 'Item 1 Name - Section 1'
// there will be more in here
),
array(
'item_name' => 'Item 2 Name - Section 1'
// there will be more in here
)
)
);
$items[] = array(
'section_name' => 'Section 2 Name',
'items' => array(
array(
'item_name' => 'Item 1 Name - Section 2'
// there will be more in here
),
array(
'item_name' => 'Item 2 Name - Section 2'
// there will be more in here
)
)
);
// and so on
with this as the input
[section] => Array (
[0] => Array (
[name] => Section 1 Name
[item] => Array (
[0] => Array (
[name] => Item 1 Name - Section 1
// there will be more in here
)
[1] => Array (
[name] => Item 2 Name - Section 1
// there will be more in here
)
)
)
[1] => Array (
[name] => Section 2 Name
[item] => Array (
[0] => Array (
[name] => Item 1 Name - Section 2
// there will be more in here
)
)
)
)
There will never be a set number of items in a section and the number of sections will vary too so I need something iterative then a fixed number.
Something like this ? :
$sections = [];
for ($sectionIndex = 0; $sectionIndex < 10; ++$sectionIndex) {
$items = [];
for ($itemIndex = 0; $itemIndex < 10; ++$itemIndex) {
$items[] = [
'item_name' => sprintf('Item %d Name - Section %d', $itemIndex, $sectionIndex);
];
}
$sections[] = [
'section_name' => sprintf("Section %d Name", $sectionIndex),
'items' => $items
];
}
Replace [] by array, since I don't know which PHP version you're using.
Pass this your input array $section
$items=array();
$item=array();
foreach ($section as $row) {
$tmp=array();
$tmp['section_name']=$row['name'];
foreach($row['item'] as $key){
$item[]['item_name']=$key['name'];
}
$tmp['items']=$item;
$items[]=$tmp;
}
print_r($items);
Solved with a slightly modified version of the answer from #NoDataFound
$sections = [];
$s = 0;
foreach ($_POST['section'] as $section) {
$s++;
$items = [];
$i = 0;
foreach ($section['item'] as $item) {
$i++;
$items[] = [
'item_name' => sprintf('Item %d Name - Section %d', $i, $s)
];
}
$sections[] = [
'section_name' => sprintf("Section %d Name", $s),
'items' => $items
];
}

Method to convert an associative array into an indexed array

I have an associative array with indexed sub-arrays each containing associative arrays which contain content and an index. So like this (in PHP):
$assoc_arr =
array("second" => array(
array("position" => 4,
"content" => "Valiant"),
array("position" => 5,
"content" => "Hail")
),
"first" => array(
array("position" => 0,
"content" => "Hail"),
array("position" => 3,
"content" => "Victors"),
array("position" => 2,
"content" => "the"),
array("position" => 1,
"content" => "to")
)
);
I want to put all of those into an indexed array where their index is their "position" in the associative array. So final array should be:
Array ( [0] => Hail [1] => to [2] => the [3] => Victors [4] => Valiant [5] => Hail )
Currently, I'm merging all of the arrays inside the highest level array, then sorting it by each of those sub-array's positions, then creating an indexed array by pushing the content in order onto a new array. As so:
$pos_arr = array_merge($assoc_arr["second"], $assoc_arr["first"]);
usort($pos_arr, function($a, $b) {
return $a["position"] >= $b["position"] ? 1 : -1;
});
$indexed_arr = array();
foreach ($pos_arr as $elem) {
array_push($indexed_arr, $elem["content"]);
}
It seems like there must be a better way to do this! Can anyone think of one?
The data is coming from a poorly structured XML document which I can't change.
$idxar=array();
foreach ($assoc_arr as $subar)
foreach ($subar as $item)
$idxar[$item['position']+0]=$item['content'];
will work, if your input data is flawless.
Edit
If you need the keys not only to be numerically correct, but also in the correct order, you must suffix this with ksort($idxar)
here is another solution:
<?php
$assoc_arr =
array("second" => array(
array("position" => 4,
"content" => "Valiant"),
array("position" => 5,
"content" => "Hail")
),
"first" => array(
array("position" => 0,
"content" => "Hail"),
array("position" => 3,
"content" => "Victors"),
array("position" => 2,
"content" => "the"),
array("position" => 1,
"content" => "to")
)
);
$order = array('first','second'); // this helps you create your own sort. E.g. position1, position2, etc
$arr = array();
foreach($order as $ord) {
if(isset($assoc_arr[$ord])) {
$arr = array_merge($arr, $assoc_arr[$ord]);
}
}
$finalArray = array();
foreach($arr as $a) {
$finalArray[$a['position']] = $a['content'];
}
ksort($finalArray);
print_r($finalArray);
and a fiddle that works for you here

(PHP) Converting an array of arrays from one format into another

I currently have an array, created from a database, an example of which looks like the following:
Array(
[0] => Array (
objectid => 2,
name => title,
value => apple
),
[1] => Array (
objectid => 2,
name => colour,
value => red
),
[2] => Array (
objectid => 3,
name => title,
value => pear
),
[3] => Array (
objectid => 3,
name => colour,
value => green
)
)
What I would like to do is group all the items in the array by their objectid, and convert the 'name' values into keys and 'value' values into values of an associative array....like below:
Array (
[0] => Array (
objectid => 2,
title => apple,
colour => red
),
[1] => Array (
objectid => 3,
title => pear,
colour => green
)
)
I've tried a few things but haven't really got anywhere.. Any ideas?
Thanks in advance
This should work with your current setup and should be able to handle as many key-value pairs as available:
<?php
$results = array(
array('objectid' => 2, 'name' => 'title', 'value' => 'apple'),
array('objectid' => 2, 'name' => 'color', 'value' => 'red'),
array('objectid' => 3, 'name' => 'title', 'value' => 'pear'),
array('objectid' => 3, 'name' => 'color', 'value' => 'green'));
$final = array();
foreach ($results as $result) {
$final[$result['objectid']]['objectid'] = $result['objectid'];
$final[$result['objectid']][$result['name']] = $result['value'];
}
print_r($final);
It would be easier to have the array keys correspond with your object id, that way you can iterate through your existing array, and add the key-value pairs for each object, like so:
$newArray = array();
foreach ($results as $result) {
if (!array_key_exists($result['objectid'], $newArray)) {
$newArray[$result['objectid'] = array();
}
foreach ($result as $key => $value) {
$newArray[$result['objectid'][$key] = $value;
}
}
Peter's method is perfectly valid, I just thought I would show a shorter version of the same thing (couldn't do in a comment)
foreach( $array as $obj ) {
if( !isset( $objects[$obj['objectid']] ) )
$objects[$obj['objectid']]['objectid'] = $obj['objectid'];
$objects[$obj['objectid']][$obj['name']] = $obj['value'];
}

Categories