Trying to create tree in multidimensional array with the following code
$source = array(
(array('id'=>406,'parent'=>0,'title'=>'level_0_406')),
(array('id'=>270,'parent'=>268,'title'=>'level_0_406_268_270')),
(array('id'=>271,'parent'=>268,'title'=>'level_0_406_268_271')),
(array('id'=>272,'parent'=>268,'title'=>'level_0_406_268_272')),
(array('id'=>273,'parent'=>268,'title'=>'level_0_406_268_273)')),
(array('id'=>269,'parent'=>268,'title'=>'level_0_406_268_269')),
(array('id'=>268,'parent'=>406,'title'=>'level_0_406_268')),
(array('id'=>407,'parent'=>406,'title'=>'level_0_406_407')),
(array('id'=>274,'parent'=>406,'title'=>'level_0_406_274')),
(array('id'=>500,'parent'=>407,'title'=>'level_0_406_407_500')),
);
$result = array();
$links = array(0=>&$result);
foreach ($source as &$element){
$links[$element['id']] = &$element;
$links[$element['parent']]['childs'][$element['id']] = &$element;
}
But result array does not include several nodes of source array, viz. nodes with id=269,270,271,272,273.
Array
(
[childs] => Array
(
[406] => Array
(
[id] => 406
[parent] => 0
[title] => level_0_406
[childs] => Array
(
[268] => Array
(
[id] => 268
[parent] => 406
[title] => level_0_406_268
)
[407] => Array
(
[id] => 407
[parent] => 406
[title] => level_0_406_407
[childs] => Array
(
[500] => Array
(
[id] => 500
[parent] => 407
[title] => level_0_406_407_500
)
)
)
[274] => Array
(
[id] => 274
[parent] => 406
[title] => level_0_406_274
)
)
)
)
)
I tried different code examples of tree generation but all of them have the same issue with source array like $source. Please help me understand such behavior.
Update
Now i understand what is wrong with array. But what if i have such data in DB, how to make selection properly? $source array should be specially sorted before using tree generation function.
The original $source array values are not properly created. It should be:-
$source = array(
(array('id'=>406,'parent'=>0,'title'=>'level_0_406')),
(array('id'=>268,'parent'=>406,'title'=>'level_0_406_268')),
(array('id'=>407,'parent'=>406,'title'=>'level_0_406_407')),
(array('id'=>274,'parent'=>406,'title'=>'level_0_406_274')),
(array('id'=>270,'parent'=>268,'title'=>'level_0_406_268_270')),
(array('id'=>271,'parent'=>268,'title'=>'level_0_406_268_271')),
(array('id'=>272,'parent'=>268,'title'=>'level_0_406_268_272')),
(array('id'=>273,'parent'=>268,'title'=>'level_0_406_268_273)')),
(array('id'=>269,'parent'=>268,'title'=>'level_0_406_268_269')),
(array('id'=>500,'parent'=>407,'title'=>'level_0_406_407_500')),
);
If you look carefully, you will see that previously, only the child element of the parent element ID 407 was available, since the element ID 407 has been defined before the occurrence of the child element.
It is the de-facto of your coding logic to have the parent elements defined first followed by the definitions of the child elements. Also the general practice & standard has always been the same.
In my answer, I have changed the occurrence of the elements properly. This should work.
Hope it helps.
Related
I've looked but can't find an answer for my specific use case. I want to add an element to a multidimensional array while looping through it. What I have before the loop:
Array
(
[fname] => Monty
[lname] => Python
[phone] => 555 555 1212
[email] => a#b.com
[modelList] => Array
(
[0] => Array
(
[id] => 1
[modelName] => X-Wing
[remarks] =>
[htmlRemarks] =>
[category] => Vehicles
[catID] => 178
[attachedToBase] => 1
[oversized] => 0
)
)
)
In code, I'm looping through the [modelList] array and after doing some database operations what I want to do is append new elements to each model array - in the case below, the [dbID]:
Array
(
[fname] => Monty
[lname] => Python
[phone] => 555 555 1212
[email] => a#b.com
[modelList] => Array
(
[0] => Array
(
[id] => 1
[modelName] => X-Wing
[remarks] =>
[htmlRemarks] =>
[category] => Vehicles
[catID] => 178
[attachedToBase] => 1
[oversized] => 0
[dbID] => 907
)
)
)
All inputs are from a form POST, and in my php handler:
// Loop thru model entries
$modelList = json_decode($_POST["modelList"], TRUE);
foreach($modelList as $model) {
(do some work)
// Add the new element
array_push($model['dbID'], $newID);
}
But this throws an error:
PHP Warning: array_push() expects parameter 1 to be array, null given
How can I add the new element to the sub-array?
array_push
Push one or more elements onto the end of array
You can't add a key value item to an array using array_push.
Use this instead :
foreach ($modelList as $key => $model){
$modelList[$key]['dbID'] = $newID;
}
Based on your snippet with the loop are you trying to actually update the dbID?
If so, you should change the logic to:
foreach ($modelList as $key => $model) {
(do some work)
$modelList[$key]['dbID'] = $newID;
}
Why:
you cannot change contents of the array you are looping over (unless accessed by reference)
in your example code $model['dbID'] is number 907 which is not an array, hence the error message
Found it - I needed to use a reference to $model in order to update it
// Loop thru model entries
$modelList = json_decode($_POST["modelList"], TRUE);
foreach($modelList as &$model) {
(do some work)
// Add the new element
$model['dbID'] = $newID;
}
unset($model);
i want display the backdrop_path of the movie but i get this error
Notice: Undefined index: backdrop_path in /..../public_html/movie.php on line 167
I have added
print_r($tmdbResult);
to see the content of $tmdbResult. I want to display just backdrop_path.
Array
(
[movie_results] => Array
(
[0] => Array
(
[adult] =>
[backdrop_path] => /mbA7SCtJoFTactP1lDHA055qCf.jpg
[genre_ids] => Array
(
[0] => 35
[1] => 28
)
[id] => 261392
[original_language] => en
[original_title] => American Ultra
[overview] => A stoner and his girlfriend's sleepy, small-town existence is disrupted when his past comes back to haunt him in the form of a government operation set to wipe him out.
[release_date] => 2015-08-21
[poster_path] => /6oGHH27nqaLGfpcgYRIZYSJs7AD.jpg
[popularity] => 3.509263
[title] => American Ultra
[video] =>
[vote_average] => 5.6
[vote_count] => 134
)
)
[person_results] => Array
(
)
[tv_results] => Array
(
)
[tv_episode_results] => Array
(
)
[tv_season_results] => Array
(
)
)
The code i use is
ini_set("display_errors",1);
$tmdbResponse = curl_exec($ch);
curl_close($ch);
$tmdbResult = json_decode($tmdbResponse, true );
$backdrop_path = $tmdbResult['movie_results']['backdrop_path'];
$smarty->assign("backdrop_path",$backdrop_path);
print_r($tmdbResult);
The reason is you're getting a numeric index, because the result can contain multiple movies. In this case you're getting just one.
All you need to do is access the array like this #u_mulder said:
$bp = $tmdbResult['movie_results'][0]['backdrop_path'];
The [0] meaning that in case you get multiple results, just need to change that index to access the others. May be you shoulds think of a foreach loop, unless for some reason of yours, you KNOW you'll always get ONE single movie, in that case, hardcode your $bp = $tmdbResult['movie_results'][0]['backdrop_path']; no problem.
I've come across a weird scenario I do not know how to code around. I'm creating a JSON API for a wordpress site. I'm using the Connections plugin and trying to pull out the "original" image filename. The output of my sql command is this:
{
["options"]=>
string(396) "a:4:{s:5:"entry";a:1:{s:4:"type";s:12:"organization";}s:5:"group";a:1:{s:6:"family";a:0:{}}s:4:"logo";a:2:{s:6:"linked";b:0;s:7:"display";b:0;}s:5:"image";a:3:{s:6:"linked";b:1;s:7:"display";b:1;s:4:"name";a:4:{s:9:"thumbnail";s:25:"invoicelogo_thumbnail.jpg";s:5:"entry";s:21:"invoicelogo_entry.jpg";s:7:"profile";s:23:"invoicelogo_profile.jpg";s:8:"original";s:24:"invoicelogo_original.jpg";}}}"
}
}
I'm using the following command to acquire that:
querystr = "SELECT options FROM {$wpdb->prefix}connections WHERE id= '{$_GET['companyID']}'";
$options = $wpdb->get_results($querystr);
I'm not sure how to pull out the "original" part of this code though as it's not all that organized. Any help would be appreciated.
What you are seeing is the results of a php serialize call
To get at the original name just do this.
$decodedOptions = unserialize($options);
$original = $decodedOptions["image"]["name"]["original"];
Hope that helps
As a side note the deserialized data looks like
Array
(
[entry] => Array
(
[type] => organization
)
[group] => Array
(
[family] => Array
(
)
)
[logo] => Array
(
[linked] =>
[display] =>
)
[image] => Array
(
[linked] => 1
[display] => 1
[name] => Array
(
[thumbnail] => invoicelogo_thumbnail.jpg
[entry] => invoicelogo_entry.jpg
[profile] => invoicelogo_profile.jpg
[original] => invoicelogo_original.jpg
)
)
)
I've seen other questions about this but not quite like my situation. I have the following table in MySQL:
term_id name slug taxonomy parent
1 Entry Form entry-form format 0
2 Page page format 3
3 Facebook facebook format 0
4 Entry Form facebook-entry-form format 3
5 Twitter twitter format 0
6 Single single format 2
I have the following OBJECT query:
$formats = $wpdb->get_results($wpdb->prepare("
SELECT * FROM table t
WHERE t.taxonomy = 'format'
"));
I wind up with the following array:
Array ( [0] => stdClass Object ( [term_id] => 1 [name] => Entry Form [slug] => entry-form [taxonomy] => format [parent] => 0 ) [2] => stdClass Object ( [term_id] => 2 [name] => Page [slug] => page [taxonomy] => format [parent] => 3 ) [3] => stdClass Object ( [term_id] => 3 [name] => Facebook [slug] => facebook [taxonomy] => format [parent] => 0 ) [4] => stdClass Object ( [term_id] => 4 [name] => Entry Form [slug] => entry-form-facebook [taxonomy] => format [parent] => 3 ) [5] => stdClass Object ( [term_id] => 5 [name] => Twitter [slug] => twitter [taxonomy] => format [parent] => 0 ) [6] => stdClass Object ( [term_id] => 6 [name] => Single [slug] => single [taxonomy] => format [parent] => 2 ) ) 1
All of the above needs to be turned into a hierarchical list on output that looks like this:
Entry Form
Twitter
Facebook
- Entry Form
- Page
-- Single
As such, I need to turn array $formats into a hierarchical array based on the parent field. A parent of 0 means it is a top level item. As such, since Single has parent of 2 it is the child of Page which in turn has parent of 3 and is a child of Facebook.
Can anyone help me turn my array into a hierarchical array and then show me how I can loop through it for output?
If performance due to volume of queries is not going to be a problem, the simplest solution is that, instead of doing a single query that populates an array, you do one query per node in your hierarchical tree, adding a "AND parent = $id", where $id is the term_id of the current node. Something like:
Do a SELECT WHERE .... AND parent = 0;
for each result in 1, $id = term_id, do a select WHERE ... AND
parent = $id
Repeat recursively until no more results
If performance is a problem you can still dump the query to your array and apply the same algorithm to the array, but you most likely will have memory issues doing it that way if you really have that much volume.
You need to put the data into assoc array while fetching it from the database:
//$groups - result array
$groups = array();
//$record contains the assoc array of the current record
while($record = $result->fetchAssoc()) {
if (!isset($groups[$record["parent"]]))
{
$groups[$record["parent"]] = array();
}
array_push($groups[$record["parent"]], $record);
}
In the end you will get an assoc array of hierarchy with parent as a key. Then traverse through it recursively, and you'll get the result:
function print_recursively(&$groups, $parent_id, $dashes = '')
{
if(isset($groups[$parent_id]))
{
foreach($groups[$parent_id] as $key => $value)
{
print $dashes . ' ' . $value["name"];
print_recursively(&$groups, $value["term_id"], $dashes . '-');
}
}
}
I didn't test this code, but the algorithm is correct.
I've seen similar questions on here but I can't seem to apply the solutions to my problem. I have a variable called $results which I got from an API. I'll change the proper nouns so as to protect my work's customers:
stdClass Object
(
[out] => stdClass Object
(
[count] => 2
[transactions] => stdClass Object
(
[RealTimeCommissionDataV2] => Array
(
[0] => stdClass Object
(
[adId] => 12345678
[advertiserId] => 123456789
[advertiserName] => Chuck E. Cheese, inc.
[commissionAmount] => 50
[country] => US
[details] => stdClass Object
(
)
[eventDate] => 2009-11-16T09:44:25-08:00
[orderId] => X-XXXXXXXXXX
[saleAmount] => 0
[sid] => 123456789
[websiteId] => 2211944
)
[1] => stdClass Object
(
[adId] => 987654321
[advertiserId] => 12345
[advertiserName] => Chorizon Wireless.
[commissionAmount] => 50
[country] => US
[details] => stdClass Object
(
)
[eventDate] => 2009-11-16T09:58:40-08:00
[orderId] => X-CXXXXXX
[saleAmount] => 0
[sid] => 61-122112
[websiteId] => 1111922
)
)
)
)
)
I shortened it to two entries here but the number of entries will vary, it's the result of a check for transactions in the past hour, there may sometimes be only one and sometimes as many as a dozen.
I want to assign these entries to variables like websiteId1 websiteId2 etc. I know I need to do a foreach loop but can't seem to figure it out. How can I write it so that I get the "[details]" as well?
foreach ($results->out->transactions->RealTimeCommissionDataV2 AS $commissionData) {
// you can access the commissionData objects now, i.e.:
$commissionData->adId;
$commissionData->details;
}
<?
foreach ($result->out->transactions->RealTimeCommissionDataV2 as $item)
{
// do somthing with each item.
print_r($item);
// or the details array
$num_details = sizeof($item->details)
}
I think this is what you want.
EDIT
Updated based on some notes in the documentation. Specifically, these two
a numerically indexed array will not
produce results unless you use
EXTR_PREFIX_ALL or
EXTR_PREFIX_INVALID.
Prefixes are automatically separated
from the array key by an underscore
character.
echo extract( $results->out->transactions->RealTimeCommissionDataV2, EXTR_PREFIX_ALL, 'websiteId' );
// test the extract
print_r( $websiteId_0 );