Display an array in a readable/hierarchical format
Try this:
foreach($data[0] as $child) {
echo $child . "\n";
}
in place of print_r($data)
How can I display an array in a readable format?
There isn't much to say, just loop through the array and show the values. This works for variable number of items
foreach($result as $r){
echo $r."<br>";
}
Since you had difficulties doing such thing, I suggest you to study about the basics of the language (IF, lopps, variables, etc) - maybe that's what you are doing, IDK. Foreach and More.
How to output (to a log) a multi-level array in a format that is human-readable?
If you need to log an error to Apache error log you can try this:
error_log( print_r($multidimensionalarray, TRUE) );
Is there a pretty print for PHP?
Both print_r()
and var_dump()
will output visual representations of objects within PHP.
$arr = array('one' => 1);
print_r($arr);
var_dump($arr);
Flat PHP Array to Hierarchy Tree
You should use recursion.
Here an exemple of code:
$datas = array(
array('id' => 1, 'parent' => 0, 'name' => 'Page 1'),
array('id' => 2, 'parent' => 1, 'name' => 'Page 1.1'),
array('id' => 3, 'parent' => 2, 'name' => 'Page 1.1.1'),
array('id' => 4, 'parent' => 3, 'name' => 'Page 1.1.1.1'),
array('id' => 5, 'parent' => 3, 'name' => 'Page 1.1.1.2'),
array('id' => 6, 'parent' => 1, 'name' => 'Page 1.2'),
array('id' => 7, 'parent' => 6, 'name' => 'Page 1.2.1'),
array('id' => 8, 'parent' => 0, 'name' => 'Page 2'),
array('id' => 9, 'parent' => 0, 'name' => 'Page 3'),
array('id' => 10, 'parent' => 9, 'name' => 'Page 3.1'),
array('id' => 11, 'parent' => 9, 'name' => 'Page 3.2'),
array('id' => 12, 'parent' => 11, 'name' => 'Page 3.2.1'),
);
function generatePageTree($datas, $parent = 0, $depth=0){
$ni=count($datas);
if($ni === 0 || $depth > 1000) return ''; // Make sure not to have an endless recursion
$tree = '<ul>';
for($i=0; $i < $ni; $i++){
if($datas[$i]['parent'] == $parent){
$tree .= '<li>';
$tree .= $datas[$i]['name'];
$tree .= generatePageTree($datas, $datas[$i]['id'], $depth+1);
$tree .= '</li>';
}
}
$tree .= '</ul>';
return $tree;
}
echo(generatePageTree($datas));
You can test it at: http://phpfiddle.org/main/code/1qy-5fj
Or if you want the exact format:
function generatePageTree($datas, $parent = 0, $depth = 0){
$ni=count($datas);
if($ni === 0 || $depth > 1000) return ''; // Make sure not to have an endless recursion
$tree = '';
for($i=0; $i < $ni; $i++){
if($datas[$i]['parent'] == $parent){
$tree .= str_repeat('-', $depth);
$tree .= $datas[$i]['name'] . '<br/>';
$tree .= generatePageTree($datas, $datas[$i]['id'], $depth+1);
}
}
return $tree;
}
The test: http://phpfiddle.org/main/code/jw3-s1j
How to refer to data in an array (php/json)
array(2) {
["ok"]=> bool(true)
["result"]=> array(1)
{
[0]=> array(2)
{
["update_id"]=> int(44893465)
["message"]=> array(5)
{
["message_id"]=> int(16)
["from"]=> array(3)
{
["id"]=> int(29595794)
["first_name"]=> string(3) "Bob"
["username"]=> string(14) "Bobo"
}
["chat"]=> array(3)
{
["id"]=> int(29595794)
["first_name"]=> string(3) "Bob"
["username"]=> string(14) "Bobo"
}
["date"]=> int(1435354253)
["text"]=> string(7) "/q 3.33"
}
}
}
}
You are using wrong array index. $data[1][0]["username"];
not exists.
$data["result"][0]["message"]["from"]["username"];
$data["result"][0]["message"]["chat"]["username"];
This will give you the proper username
PHP - search for a key value and then find the value of another key in the same object subarray
There is a lot of ways to skin this cat... this is probably the most straight forward
function getFirstValueByFieldId($fieldId, $Arraytosearch){
foreach($Arraytosearch["fields"] as $textitem){
if ($textitem->__attributes["field_id"] == $fieldId){
return $textitems->__attributes["values"][0]["value"];
}
}
}
to Use in your case would be
echo getFirstValueByFieldId("37325091", $Arraytosearch);
Basically it walks the elements in the fields array and returns the value in the first associated value in the values array where field_id is equal to the parameter of the function.
Related Topics
Random Float Between 0 and 1 in PHP
Default as First Option in Switch Statement
Detect If Cookies Are Enabled in PHP
PHP Remember File Field Contents
Php, Merging Arrays with Common Keys
Using Gcm to Send Notifications on App, Returns Invalidregistration Error
PHP Script to Execute at Certain Times
Forbidden Error When Submitting Simple PHP Form
How to Send an HTML Email with an Inline Attached Image with PHP
File Download Script Doesn't Work When Called from Ajax
How to I Send Data from JavaScript to PHP and Vice Versa
How to Extract HTML Comments and All HTML Contained by Node
Replace Values in a Uri Query String
Azure Active Directory Sso with Laravel