Archive for the ‘PHP’ Category
How to display array as Tree
-
<?php
-
$customers
-
= array(
-
array('first' => 'Bill', 'last' => 'Jones',
-
'age' => 24, 'state' => 'CA'),
-
array('first' => 'Mary', 'last' => 'Smith',
-
'age' => 32, 'state' => 'OH'),
-
array('first' => 'Joyce', 'last' => 'Johnson',
-
'age' => 21, 'state' => 'TX'),
-
);
-
printf("print_r():<pre>%s</ pre>", print_r($customers, TRUE));
-
printf("var_export():<pre>%s</ pre>", var_export($customers, TRUE));
-
print 'var_dump():<pre>';
-
var_dump($customers);
-
print '</ pre>';
-
?>
How to Remove Item in Array in PHP
Use unset() to remove an item/element in Arrays, see example below
-
<?PHP
-
-
$array = array('name'=>'Bill', 'age'=>32, 1=>'25 Main St.',
-
2=>'Apt. 24', 'City'=>'San Francisco', 'state'=>'CA');
-
-
printf("<pre>;%s</ pre>\n", var_export($array, TRUE));
-
-
unset($array['age']);
-
-
printf("<pre>%s</ pre>\n", var_export($array, TRUE));
-
-
?>
Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0
Error: Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0
This means the current user or user the service has no read/write permission on the folder.
Type the following command to change permission
chmod a+rwx /path/to/folder
Convert Array data to String
To Convert array data to string, this can be done automatically if it is one dimension array only, use implode function.
<php>
$array = array(‘firstname’, ‘lastname’, ‘middlename’);
$tostring = implode(“,”, $array);
echo $tostring ; // firstname,lastname, middlename
</php>