I have gotten the tree-array like that:
Now I want to convert it to an array like this:
array(
1 => array('id'=>'1','parentid'=>0),
2 => array('id'=>'2','parentid'=>0),
3 => array('id'=>'3','parentid'=>1),
4 => array('id'=>'4','parentid'=>1),
5 => array('id'=>'5','parentid'=>2),
6 => array('id'=>'6','parentid'=>3),
7 => array('id'=>'7','parentid'=>3)
);
I had already coded something like this:
private function _getOrderData($datas)
{
$_data = [];
static $i = 0;
foreach ($datas as $data) {
$i++;
$rows = ['id' => $data['id'], 'pid' => isset($data['children']) ? $data['id'] : 0, 'menu_order' => $i];
if(isset($data['children'])) {
$this->_getOrderData($data['children']);
}
$_data[] = $rows;
}
return $_data;
}
But it didn't work
Cry~~
How can I fix my code to get the array? Thanks~
BTW, my English is pool.
So much as I don't know you can read my description of the problem or not.
I had already solved this problem, Thx~
private function _getOrderData($datas, $parentid = 0)
{
$array = [];
foreach ($datas as $val) {
$indata = array("id" => $val["id"], "parentid" => $parentid);
$array[] = $indata;
if (isset($val["children"])) {
$children = $this->_getOrderData($val["children"], $val["id"]);
if ($children) {
$array = array_merge($array, $children);
}
}
}
return $array;
}
The array look like:
array
Related
I am trying to get my XML format be like this:
but my code now does not loop for the 'order_line' array and will return like this:
below are sample of my code I did:
$result = $this->db->query("SELECT * FROM `grn_order` a");
foreach($result->result() as $row )
{
$result_line = $this->db->query("SELECT * FROM `grn_order_line` a where a.order_no = '$row->order_no'");
foreach($result_line->result() as $row_line);
{
$line = array(
'guid' => $row_line->guid,
'itemcode' => $row_line->itemcode,
);
}
$my_array[] = array(
'order_no' => $row->order_no,
'loc_code' => $row->loc_code,
'trans_code' => $row->trans_code,
'po_no' => $row->po_no,
'order_line' => $line
);
} $xml = new SimpleXMLElement('<orders/>');
// function callback
$data = $this->array2XML($xml, $my_array);
print $xml->asXML();
function array2XML($obj, $array)
{
foreach ($array as $key => $value)
{
if(is_numeric($key))
$key = 'order';
if (is_array($value))
{
$node = $obj->addChild($key);
$this->array2XML($node, $value);
}
else
{
$obj->addChild($key, htmlspecialchars($value));
}
}
}
You keep on overwriting the last line in your load loop. Change it to...
$line = [];
foreach($result_line->result() as $row_line);
{
$line[] = array(
'guid' => $row_line->guid,
'itemcode' => $row_line->itemcode,
);
}
So each line is added using $line[].
I came up with this ugly verbose array (and I have to use it):
$puppy_mother_father_arr = array(
array('46' => array('30','29')),
array('17' => array('30','29')),
array('16' => array('24','29'))
);
How do I simplify it to something like this :
$puppy_mother_father_arr = array(
'46' => array('30','29'),
'17' => array('30','29'),
'16' => array('24','29')
);
I've been stuck a day in here. thanks in advance
$tmp = array();
foreach ($puppy_mother_father_arr as $parent) {
foreach($parent as $key => $nodes) {
$tmp[$key] = $nodes;
}
}
$puppy_mother_father_arr = $tmp;
Would this work?
See the result online
<?php
$puppy_mother_father_arr = array( array('46' => array('30','29')),array('17' => array('30','29')),array('16' => array(24,'29')) );
$list = array();
foreach ($puppy_mother_father_arr as $info)
{
foreach ($info as $key => $value)
{
$list[$key] = $value;
break;
}
}
var_export($list);
$newarray = array();
foreach ($puppy_mother_father_arr as $array) {
foreach ($array as $puppy => $parents) {
$newarray[$puppy] = $parents;
}
}
$puppy_mother_father_arr = $newarray;
key and current could be useful if each array has only one interested key and value:
$result = array();
foreach($puppy_mother_father_arr as $arr) {
$result[key($arr)] = current($arr);
}
var_dump($result);
im having a little problem i need some help with.
Im trying to convert a multidimensional array into a flatten array with nested set values right and left like so:
$array = {
'id' => 1
'name' => 'john'
'childs' => array(
array(
'id' => 1
'name' => 'jane'
)
)
}
to
$array = {
array(
'id' => 1,
'name' => 'john'
'left' => '1'
'right' => '4'
),
array(
'id' => 1,
'name' => 'jane'
'left' => '2'
'right' => '3'
)
}
Any help is appreciated!
I asked a very similar question and got a result, so thought I'd send it on for you. I realise this is quite an old topic, but still worth getting an answer. I've included my data, but would be easily adapted for yours.
$JSON = '[{"id":1,"children":[{"id":2,"children":[{"id":3},{"id":4}]},{"id":5}]}]';
$cleanJSON = json_decode($JSON,true);
$a_newTree = array();
function recurseTree($structure,$previousLeft)
{
global $a_newTree; // Get global Variable to store results in.
$indexed = array(); // Bucket of results.
$indexed['id'] = $structure['id']; // Set ID
$indexed['left'] = $previousLeft + 1; // Set Left
$lastRight = $indexed['left'];
$i_count = 0;
if ($structure['children'])
{
foreach ($structure['children'] as $a_child)
{
$lastRight = recurseTree($structure['children'][$i_count],$lastRight);
$i_count++;
}
}
$indexed['right'] = $lastRight + 1; // Set Right
array_push($a_newTree,$indexed); // Push onto stack
return $indexed['right'];
}
recurseTree($cleanJSON[0],0);
print_r($a_newTree);
function restructRecursive($array, $left = 1) {
if (isset($array['childs'])) {
$result = array();
foreach ($array['childs'] as $child) {
$result = array_merge($result, restructRecursive($child, $left+1));
}
unset($array['childs']);
}
$array['left'] = $left;
return array_merge(array($array), $result);
}
$newStruct = restructRecursive($oldStruct);
For anyone coming here and looking for a solution, loneTraceur's solution works fine. However, I needed one in OOP, so here is my version of it.
<?php
class NestedSet
{
protected $tree = [];
public function deconstruct($tree, $left = 0)
{
$this->flattenTree($tree, $left);
return $this->tree;
}
protected function flattenTree($tree, $left)
{
$indexed = [];
$indexed['id'] = $tree['id'];
$indexed['_lft'] = $left + 1;
$right = $indexed['_lft'];
if (isset($tree['children']) && count($tree['children'])) {
foreach ($tree['children'] as $child) {
$right = $this->flattenTree($child, $right);
}
}
$indexed['_rgt'] = $right + 1;
$this->tree[] = $indexed;
return $indexed['_rgt'];
}
}
You would run it like this:
$NestedSet = new NestedSet;
$flat = $NestedSet->deconstruct($tree):
This code is based on the other answer.
The following function is from a PEAR script called File_CSV_DataSource, and is a CSV parser.
CSV file is this:
name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
The current output of this function is like this:
array (0 => array ('name' => 'john','age' => '13','skill' => 'knows magic',),1 =>array('name' => 'tanaka','age' => '8','skill' => 'makes sushi',),2 =>array ('name' => 'jose','age' => '5','skill' => 'dances salsa',),)
what I'm trying to achieve is to make the function to output like this:
array (array ('john','13','knows magic'), array('tanaka','8','makes sushi'), array ( 'jose','5','dances salsa'),)
so without the 0 => and without the column header => , only the value of the cells
Is there any way for me to modify the function bellow to make it output as in my example above?
public function connect($columns = array())
{
if (!$this->isSymmetric()) {
return array();
}
if (!is_array($columns)) {
return array();
}
if ($columns === array()) {
$columns = $this->headers;
}
$ret_arr = array();
foreach ($this->rows as $record) {
$item_array = array();
foreach ($record as $column => $value) {
$header = $this->headers[$column];
if (in_array($header, $columns)) {
$item_array[$header] = $value;
}
}
// do not append empty results
if ($item_array !== array()) {
array_push($ret_arr, $item_array);
}
}
return $ret_arr;
}
Thanks in advance
Just use the built in filegetcsv. It will do that for you.
I'm trying to get this working:
I have an array that gets "deeper" every loop. I need to add a new array to the deepest "children" key there is.
while($row = mysql_fetch_assoc($res)) {
array_push($json["children"],
array(
"id" => "$x",
"name" => "Start",
"children" => array()
)
);
}
So, in a loop it would be:
array_push($json["children"] ...
array_push($json["children"][0]["children"] ...
array_push($json["children"][0]["children"][0]["children"] ...
... and so on. Any idea on how to get the key-selector dynamic like this?
$selector = "[children][0][children][0][children]";
array_push($json$selector);
$json = array();
$x = $json['children'];
while($row = mysql_fetch_assoc($res)) {
array_push($x,
array(
"id" => "$x",
"name" => "Start",
"children" => array()
)
);
$x = $x[0]['children'];
}
print_r( $json );
Hmmm - maybe better to assign by reference:
$children =& $json["children"];
while($row = mysql_fetch_assoc($res)) {
array_push($children,
array(
"id" => "$x",
"name" => "Start",
"children" => array()
)
);
$children =& $children[0]['children'];
}
$json = array();
$rows = range('a', 'c');
foreach (array_reverse($rows) as $x) {
$json = array('id' => $x, 'name' => 'start', 'children' => array($json));
}
print_r($json);
If you want to read an array via a string path, split the string in indices, and then you can do something like this to get the value
function f($arr, $indices) {
foreach ($indices as $key) {
if (!isset($arr[$key])) {
return null;
}
$arr = $arr[$key];
}
return $arr;
}