I am trying to implement a decision tree based on this jQuery plugin.
It expects data in this format:
$tree = array(
"href" => "/",
"label" => "Title",
"nodes" => array(
array(
"href" => "/q1",
"label" => "Question 1?",
"nodes" => array(
array(
"href" => "/q1/a1",
"label" => "Answer 1",
"nodes" => array(
"href" => "/q1/a1/q11",
"label" => "Question 1.1?",
"nodes" => array(
array(
"href" => "/q1/a1/q11/a11",
"label" => "Answer 1.1",
"nodes" => null
),
array(
"href" => "/q1/a1/q11/a12",
"label" => "Answer 1.1",
"nodes" => null
)
)
)
),
array(
"href" => "/q1/a2",
"label" => "Answer 2",
"nodes" => null
)
)
)
)
);
I fetch the results as a flat list from a database and then am able to generate the tree structure using a recursive function:
function buildTree(array $elements, $rootId = 0, $current_path = '', $new_path = '') {
$ctr = 0;
$branch = array();
foreach($elements as $element) {
if ($element['root_id'] == $rootId) {
$array = array(
'root_id' => $element['root_id'],
'label' => $element['label'],
'text' => $element['text'],
);
if(!empty($current_path)){
$path = $current_path . ++$ctr;
$array['href'] = $path;
}
$new_path = $new_path == '/q' ? '/a' : '/q';
$children = buildTree($elements, $element['id'], $path . $new_path, $new_path);
if ($children) {
$array['nodes'] = $children;
}
$branch[] = $array;
}
}
return $branch;
}
$tree = buildTree($results);
which yields:
$tree = array(
"href" => "/",
"label" => "Title",
"nodes" => array(
array(
"href" => "/q1",
"label" => "Question 1?",
"nodes" => array(
array(
"href" => "/q1/a1",
"label" => "Answer 1",
"nodes" => array(
"href" => "/q1/a1/q1",
"label" => "Question 1.1?",
"nodes" => array(
array(
"href" => "/q1/a1/q1/a1",
"label" => "Answer 1.1",
"nodes" => null
),
array(
"href" => "/q1/a1/q1/a1",
"label" => "Answer 1.1",
"nodes" => null
)
)
)
),
array(
"href" => "/q1/a2",
"label" => "Answer 2",
"nodes" => null
)
)
)
)
);
which gives the correct 'href's up to a depth of 2 but then deviates from the expected format.
I can't figure out how to get the 'href's correct.
How can i generate the correct 'href' using my recursive function as expected by the jQuery plugin?
$path = $current_path . ++$ctr;
You're counting yet this value is localized for that particular function even on recursion. It will never and seems to never move above 1 because after it iterates once, it calls buildTree again.
Is q11 not stored in the database? You need a way of incrementing/parsing/gathering (i am not sure exactly what the value should be) that value to what it should be as it recurses through. Should this value just remove the . from 1.1 ?
Try dropping in this function in place of yours and see if it changes anything:
function buildTree(array $elements, $rootId = 0, $current_path = '', $new_path = '', $parent_id = 0, $ctr = 0) {
$branch = array();
foreach($elements as $element) {
if ($element['root_id'] == $rootId) {
$array = array(
'root_id' => $element['root_id'],
'label' => $element['label'],
'text' => $element['text'],
);
if(!empty($current_path)){
if($parent_id > 0 ) {
$path = $current_path . $parent_id . $ctr;
}else{
$path = $current_path . ++$ctr;
}
$array['href'] = $path;
}
$new_path = $new_path == '/q' ? '/a' : '/q';
$parent_id++;
$children = buildTree($elements, $element['id'], $path . $new_path, $new_path, $parent_id, $ctr);
$parent_id--;
if ($children) {
$array['nodes'] = $children;
}
$branch[] = $array;
}
}
return $branch;
}
Related
I am trying to turn a string into a nested array
Here is my string:
a/b/d.docx
and I wanted to be like this:
array(
"name" => "a",
"type" => "folder",
"sub" => array(
"name" => "b",
"type" => "folder",
"sub" => array(
"name" => "c.docx",
"type" => "file",
"size" => "20"
)
)
)
This is the code that I have so far
$items = explode('/', $strings);
$num = count($items);
$num = --$num;
$temp = array();
foreach($items as $keys => $value) {
$temp[$keys] = array(
"name" => $value,
"type" => "folder",
"items" => $temp[++$keys]
);
if($keys == $num){
$temp[$keys] = array(
"name" => $value,
"type" => "file",
"size" => "20"
);
}
}
var_dump($temp);
I am trying this functions but this only turn string into a single array and it also can't do the 'items' line.
Any help would be appreciated.Thanks.
Note that the path is virtual and doesn't exist.
UPDATE: How can I add path to each array??for example,"path"=>"a/b"
You can do that:
$path = 'a/b/d.docx';
$parts = explode('/', $path);
$result = [ 'name' => array_pop($parts), 'type' => 'file', 'size' => 20 ];
while ($parts) {
$result = [ 'name' => array_pop($parts), 'type' => 'folder', 'sub' => $result ];
}
print_r($result);
<?php
$strings='a/b/d.docx';
$items = explode('/', $strings);
$num = count($items)-1;
$root= array();
$cur = &$root;
$v='';
foreach($items as $keys => $value) {
$v = $v.$value;
$temp = array( "name" => $value, "path"=>$v, "type" => "folder", "items" => "");
if($keys == $num){
$temp = array( "name" => $value, "path"=>$v, "type" => "file", "size" => "20");
}
$v= $v.'/';
if($keys==0) {
$cur = $temp;
}
else
{
$cur['items'] = $temp;
$cur = &$cur['items'];
}
}
var_dump($root);
Try recursion:
public function testAction(){
$sString = 'a/b/c/d.exe';
$aExploded = explode('/', $sString);
var_dump($this->_parse_folder_rec($aExploded));
}
private function _parse_folder_rec($aExploded){
$aResult = [];
$aResult['name'] = array_shift($aExploded);
if($aExploded){
$aResult['type'] = 'folder';
$aResult['sub'] = $this->_parse_folder_rec($aExploded);
}else{
$aResult['type'] = 'file';
$aResult['size'] = 20;
}
return $aResult;
}
I am creating bar chart , i am unable to get output and generate chart.
here is my code
if ($sql) {
$arrData = array(
"chart" => array(
"caption" => "Status stastics",
"showValues" => "0",
"theme" => "zune"
)
);
$arrData["data"] = array();
foreach($sql as $row) {
array_push($arrData["data"], array(
"label" => $row["name"],
"value" => $row["value"]
)
var_dump($row["value"]);
);
}
}
I am not getting any output for ( var_dump($row["value"]);)
am i going in a right way?
Can any one help me in this.
Got my answer
if ($sql) {
$arrData = array(
"chart" => array(
"caption" => "Status stastics",
"showValues" => "0",
"theme" => "zune"
)
);
$arrData["data"] = array();
foreach($sql as $row)
{
$object_array =(array)$row;
array_push($arrData["data"], array(
"label" => $object_array["name"],
"value" => $object_array["value"]
)
);
}
}
?>
I need my for loop to return an array that looks like this
$nullArray =array(
0 => array("id" => 1, "label" => "test 1", "type" => "folder"),
array("id" => 2, "label" => "test 2", "type" => "folder"),
array("id" => 3, "label" => "test 3", "type" => "folder"),
etc...
etc...
etc...
);
what I have right now
$nullArray = array();
$numOfVer = mysql_num_rows($result);
$startArray= array();
//SETS FIRST NODE
for($i =0;$i < $numOfVer;$i++)
{
$label = mysql_result($result, $i);
$id = $i+1;
$startArray = array(array('id' => $id,'label' => $label, "type" => "folder"));
//$startArray[]['id'] = $id;
//$startArray[]['label'] = $label;
//$startArray[]['type'] = "folder";
//array_push($startArray,array(array('id' => $id,'label' => $label, "type" => "folder")));
//$nullArray[0]= array(array('id' => $id,'label' => $label, "type" => "folder"));
//array_push($nullArray[0],array('id' => $id,'label' => $label, "type" => "folder"));
}
$nullArray[0] = $startArray;
echo json_encode($nullArray[0]);
Everything that I have commented out is something that I have tried and it has failed. I'v been at it for too long for something so simple so I decided to get some help! Thank you in advance! :)
In for loop you are redclaring your $startArray thats why the previous value removed. Try this.
$nullArray = array();
$numOfVer = mysql_num_rows($result);
$startArray= array();
for($i =0;$i < $numOfVer;$i++)
{
$label = mysql_result($result, $i);
$id = $i+1;
$startArray[] = array('id' => $id,'label' => $label, "type" => "folder");
}
Suppose i have a array like this :
Array(
'1' => Array(
"ID" => 1,
"Name" => "name 1"
),
'2' => Array (
Array(
"ID" => 2,
"Name" => "name 2"
)
),
'3' => Array(
Array(
Array(
Array(
"ID" => 3,
"Name" => "name3"
)
)
),
'4' => Array (
Array {
"ID" => 4,
"Name" => "name 4"
),
Array(
"ID" => 5,
"Name" => "name 5"
),
Array(
"ID" => 6,
"Name" => "name 6"
)
);
number of sub-arrays is not ordered it may be 3, 4 or 5 etc...
and i wanted to get :
Array(
Array( "ID" => 1, "Name" => "name 1"),
Array( "ID" => 2, "Name" => "name 2"),
Array( "ID" => 3, "Name" => "name 3"),
Array( "ID" => 4, "Name" => "name 4"),
Array( "ID" => 5, "Name" => "name 5"),
Array( "ID" => 6, "Name" => "name 6"));
Is there an easy way to do this ?
EDIT :
Edited the above array to add :
'4' => Array (
Array {
"ID" => 4,
"Name" => "name 4"
),
Array(
"ID" => 5,
"Name" => "name 5"
),
Array(
"ID" => 6,
"Name" => "name 6"
)
);
which aren't nested but i still want them to be in my final array.
Thanks.
//Assuming your data is in $in
$out=array();
foreach($in as $k=>$v) {
while ((is_array($v)) && (isset($v[0]))) $v=$v[0];
//See below for next line
$out[]=$v;
}
print_r($out);
With the marked line being either $out[$k]=$v; or $out[]=$v; depending on wether you want to keep the 1st-level keys. In your desired output you do not ,so use the shown version
Edit
With you changed input array, you need to do something like
function addtoarray($inarray, &$outarray) {
foreach ($inarray as $i) {
if (!is_array($i)) continue;
if (isset($i['ID'])) $outarray[]=$i;
else addtoarray($i,$outarray);
}
}
$out=array();
addtoarray($in,$out);
print_r($out);
This was tested against your new input data
You could recurse through the array and check for the ID field.
function combine_array(array $src, array &$dest)
{
if( isset( $src['ID'] ) ) {
$dest[] = $src;
return;
}
foreach( $sub in $src ) {
combine_array( $sub, $dest );
}
}
Just wrote this off the top of my head, no testing, so it might have a few problems. But that's the basic idea.
This may work for you, assuming $array is the variable and php 5.3
//process
$array = array_map(function($block) {
$return = '';
foreach ($block as $sub) {
if (true === isset($sub['ID']) {
$return = $block;
break;
}
}
return $block;
}, $array);
array_filter($array); //remove empty elements
Wrote up a quick recursive function. You'll need to write any appropriate error handling and check the logic, since a discrepancy in your data could easily turn this into an infinite loop:
$output = array();
foreach ( $data as $d ) {
$output[] = loop_data( $d );
}
function loop_data( $data ) {
// Check if this is the right array
if( ! array_key_exists( "ID", $data ) ) {
// Go deeper
$data = loop_data( $data[0] );
}
return $data;
}
I have a file contain text
ABBCDE1990-12-10JOBALPHABETabbcde1990-12-10jobalphabet
$field = array(
"fullname" => array("length"=5,"mandat"=>True),
"bithday" => array("length"=>10,"mandat"=>True)
"job" => array("length"=>3,"mandat"=>True),
"desc" => array("length"=>8,"mandat"=>false)
);
How can I get the array some thing like this:
$output = array(
//ABBCDE1990-12-10JOBALPHABET
0=>array(
"fullname" => "ABBCDE"
"bithday" => 1990-12-10
"job" => "JOB"
"desc"=> "ALPHABET"
)
//abbcde1990-12-10jobalphabet
1=>array(
"fullname" => "abbcde"
"bithday" => 1990-12-10
"job" => "job"
"desc"=> "alphabet"
)
);
I am tryin to buld a function
function toOutput($str,$filed){
$per_line = 27;//len of abbcde1990-12-10jobalphabet
$pos = 0;
while ($pos<strlen($str)){
$pos += 27;
//
}
}
$field = array(
"fullname" => array("length"=>6,"mandat"=>True),
"bithday" => array("length"=>10,"mandat"=>True),
"job" => array("length"=>3,"mandat"=>True),
"desc" => array("length"=>8,"mandat"=>false)
);
$string = 'ABBCDE1990-12-10JOBALPHABETabbcde1990-12-10jobalphabet';
$result = array();
$countString = strlen($string)/27;
$oldPos = 0;
for($i=0;$i<$countString;$i++) {
foreach($field as $k=>$v) {
$result[$i][$k] = substr($string,$oldPos,$v['length']);
$oldPos += $v['length'];
}
}
print_r($result);
You can see it up and running here: http://codepad.org/CEQNIPCg (version 0.3)
Based on that you can create a function so you can pass to it all $string that you have