I'm using var_export to dump output to logs when errors occur. However since the result is in pure text, I don't get a chance to push it through some sort of library like krumo so I can interactively explores the output.
What methods do people have to deal with making var_export text more readable?
Here is my function, it works well for multidimensional arrays:
function VE($varname, $varval, $short_syntax=true, $tag = ' ', $comma='', $end_line="\r\n") {
$res = '';
if($short_syntax){
$begin_array = '[';
$end_array = ']';
} else {
$begin_array = 'array(';
$end_array = ')';
}
$arr = explode('/',$varname);
$dim =count($arr)-1;
$lastKey = end($arr);
if (! is_array($varval)){
if( is_string($varval)) $varval = "'$varval'";
$res .= str_repeat($tag,$dim) . $lastKey . ' => ' . $varval . $comma . $end_line;
}else{
$res .= str_repeat($tag,$dim) . $lastKey . ' => ' . $begin_array . $end_line;
$count_varval = 0;
$dim_varval = count($varval);
foreach ($varval as $key => $val){
$count_varval++;
if($count_varval<$dim_varval) $commma=','; else $commma='';
if( is_string($key)) $key = "'$key'";
$res .= VE ($varname . "/" . $key , $val, $short_syntax, $tag, $commma);
}
$res .= str_repeat($tag,$dim) . $end_array . $comma . $end_line;
}
return $res;
}
$bigarray = array(); // your array
$bb = VE ('$bigarray', $bigarray);
echo "<pre>$bb</pre>";
I hope it helps ;)
Related
can I ask for your help? I have values in the array and I want to combine them to become one value. I want a result that 'gendesc' 'dmdnost' 'stredesc' 'formdesc' 'rtedesc' will be in one column. Thanks.
foreach($data as $item){
$tmp = array();
$tmp_item = (array) $item;
$tmp['description'] = $tmp_item['gendesc'];
$tmp['m1'] = $tmp_item['dmdnost'];
$tmp['m2'] = $tmp_item['stredesc'];
$tmp['m3'] = $tmp_item['formdesc'];
$tmp['m4'] = $tmp_item['rtedesc'];
$final_data [] = $tmp;
}
print_r($final_data);
Can you try
foreach($data as $item){
$tmp_item = (array) $item;
$tmp = $tmp_item['gendesc']
. ' ' . $tmp_item['dmdnost']
. ' ' . $tmp_item['stredesc']
. ' ' . $tmp_item['formdesc']
. ' ' . $tmp_item['rtedesc'];
$final_data[] = array('description' => $tmp);
}
print_r($final_data);
You want this?
foreach($data as $item){
$tmp = array();
// ver.1
$tmp['description'] = $item['gendesc']
. $item['dmdnost']
. $item['stredesc']
. $item['formdesc']
. $item['rtedesc'];
// ver.2 also you can use one-line shortcut with implode instead of ver.1
$tmp['description'] = trim(implode(" , ", $item));
$final_data[] = $tmp;
}
print_r($final_data);
Try this,
$i =0;
foreach($data as $item)
{
$tmp = [];
$tmp[$i]['description'] = $item['gendesc'];
$tmp[$i]['m1'] = $item['dmdnost'];
$tmp[$i]['m2'] = $item['stredesc'];
$tmp[$i]['m3'] = $item['formdesc'];
$tmp[$i]['m4'] = $item['rtedesc'];
$i++;
}
print_r($tmp);
I have created a form,I read from a INI file and show its values in the form.
Now I want to edit that file.
So I show values in text box and let the user edit the file.
Now I am using INI Library to write my files, but my code changes the structure of INI file, also it remove the comments.
Is it possible that when I write to the file only those value is changed and other things like comments and Section remain as it is ?
This is my code:
$CFG_file = $this->data['file_data'] = parse_ini_file("CFG/fms.cfg" ,true);
if (isset($_POST['submit']))
{
//var_dump($_POST);
$data_file = $this->parameter_m->array_from_post(array('VAL1','VAL2','InMedia','VAL','VAL','VAL5','VAL3'));
$this->load->helper('file');
$file = "CFG/fms.cfg";
$this->load->library('ini');
$ini = new INI($file);
$ini->write($file, $data_file );
}
And the library code that writes file :
function write($file = NULL, $file_content = array(), $sections = FALSE) {
$this->file_content = (!empty($file_content)) ? $file_content : $this->file_content;
$this->file = ($file) ? $file : $this->file;
$this->sections = $sections;
$content = NULL;
if ($this->sections) {
foreach ($this->file_content as $section => $file_content) {
$content .= '[' . $section . ']' . PHP_EOL;
foreach ($file_content as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[]=' . (is_numeric($v) ? $v : $v ) . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . '=' . PHP_EOL;
} else {
$content .= $key . '=' . (is_numeric($val) ? $val : $val ) . PHP_EOL;
}
}
$content .= PHP_EOL;
}
} else {
foreach ($this->file_content as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[] = ' . (is_numeric($v) ? $v : '"' . $v . '"') . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . ' = ' . PHP_EOL;
} else {
$content .= $key . ' = ' . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL;
}
}
}
return (($handle = fopen($this->file, 'w+')) && fwrite($handle, trim($content)) && fclose($handle)) ? TRUE : FALSE;
}
And this is my file :
[]
VAL1= 0.0.0.0
VAL2= 5070
;media settings
;ON/OFF/AUTO
VAL3= OFF
VAL4= 8000
VAL5= 4000
VAL6= 360
;Transport settings
;ON/OFF
VAL7= OFF
When i execute my code this becomes :
VAL1= "0.0.0.0"
VAL2 = 5071
VAL3 =
VAL4 = 8000
VAL5 = 4000
VAL6 = 360
VAL7 = "ON"
I need to add data in my database and one way or the other the format must be like,
0 - 0 or 1 - 5
i have tryed =>
mysqli_real_escape_string();
but did not work.
i have also tryed changing the sign to / , * , +, exp.
PHP
$dataBase->_insertDataBase('tableNaam', $input);
function _insertDataBase($tabel,$input){
$velden='';
$waarden='';
$i=0;
foreach($input AS $key=>$value){
$i=$i+1;
if($i !== count($input)){
$a=', ';
}else{
$a='';
}
$velden.=$key.$a;
$waarden.=$value.$a;
unset($a);
}
$sql = "INSERT INTO `$tabel`($velden) VALUES ($waarden)";
$this->_conn()->query($sql);
}
The problem is your SQL statement is giving the equation 5-1 as a value, so the server is doing the math and inserting 4 just like you told it to. You need tell it that it's a literal string by surrounding the values with apostrophe's (i.e. '5-1').
Replace:
foreach ($input AS $key => $value) {
$i = $i + 1;
if ($i !== count($input)) {
$a = ', ';
} else {
$a = '';
}
$velden .= $key . $a;
$waarden .= $value . $a;
unset($a);
}
With:
$velden = array();
$waarden = array();
foreach ($input AS $key => $value) {
$velden[] = $key;
$waarden[] = $value;
}
$velden = implode(',', $velden);
$waarden = "'" . implode("','", $waarden) . "'";
Or possibly even:
$velden = array_keys($input);
$waarden = array_values($input);
$velden = implode(',', $velden);
$waarden = "'" . implode("','", $waarden) . "'";
I looking for a way (regex, snippet ,plugin etc) to convert the old arrays with the new php syntax with sublimeText.
// Old synthax
$var = array(
'foo' => 'bar'
);
// New synthax
$var = [
'foo' => 'bar'
];
someone has an idea ?
I found a script that does the job perfectly !
https://github.com/thomasbachem/php-short-array-syntax-converter
I found out that it is also possible to do this with the php codesniffer: https://github.com/squizlabs/PHP_CodeSniffer
phpcbf src/ --standard=Generic --sniffs=Generic.Arrays.DisallowLongArraySyntax
In this example you have to replace src/ with the folder containing the scripts. Alternatively you can provide a file name.
Maybe a bit late, but I created my own. Maybe not pretty, but it does do the job I want. If you do not like tab, change \t in indent function to 2 or 4 spaces.
function loopArray(array $array, $loopcount = 0) {
$returnString = ($loopcount == 0) ? "[\n" : "";
$tabKey = indent($loopcount + 2);
$tabValue = indent($loopcount + 3);
$lastKey = array_key_last($array);
foreach ($array as $key => $value) {
$totalChildren = count($array[$key]);
$returnString .= $tabKey . '"' . $key . '" => ';
if ($totalChildren == 0) $returnString .= '[]';
if ($totalChildren > 0 && is_array($array[$key])) $returnString .= '[' . "\n";
if (is_array($value)) {
$returnString .= loopArray($value, $loopcount + 1);
} else {
if ($totalChildren == 1) $returnString .= '"' . $value . '"';
if ($totalChildren > 1) $returnString .= $tabValue . '"' . $value . '"' . ",\n";
}
$returnString .= ($lastKey == $key) ? "\n" . indent($loopcount+1) . "]" : ",\n";
}
return $returnString;
}
function indent($amount) {
return str_repeat("\t", $amount);
}
/** use function below only prior to php 7.3 */
function array_key_last(array $array) {
$key = NULL;
if ( is_array( $array ) ) {
end( $array );
$key = key( $array );
}
return $key;
}
I got this code from someone and it works very well, I just want to remove the link from the last element of the array:
//get rid of empty parts
$crumbs = array_filter($crumbs);
$result = array();
$path = '';
foreach($crumbs as $crumb){
$path .= '/' . $crumb;
$name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
$result[] = "$name";
}
print implode(' > ', $result);
This will output for example:
Content > Common > File
I just want a to remove the link from the last item - "File" to be just plain text..
I tried myself to count the array items and then if the array item is the last one then to print as plain text the last item.. but I'm still noob, I haven't managed to get a proper result..
Thank you!
This should work:
$crumbs = array_filter($crumbs);
$result = array(); $path = '';
//might need to subtract one from the count...
$count = count($crumbs);
foreach($crumbs as $k=>$crumb){
$path .= '/' . $crumb;
$name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
if($k != $count){
$result[] = "$name";
} else {
$result[] = $name;
}
}
print implode(' > ', $result);
You could simply tweak your existing code to use a 'normal' loop (rather than a foreach iterator) to achieve this.
For example:
//get rid of empty parts
$crumbs = array_filter($crumbs);
$result = array();
$path = '';
$crumbCount = count($crumbs);
for($crumbLoop=0; $crumbLoop<$crumbCount; $crumbLoop++) {
$path .= '/' . $crumbs[$crumbLoop];
$name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumbs[$crumbLoop]));
$result[] = ($crumbLoop != $crumbCount -1) ? "$name" : $name;
}
print implode(' > ', $result);
N.B: I don't have access to PHP at the moment, so the above might not be error free, but you should get the idea.
for($i=0;$i< sizeof($crumbs);$i++) {
$path .= '/' . $crumbs[$i];
$name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumbs[$i]));
if ($i != sizeof($crumbs)-1) {
$result[] = "$name";
}else {
$result[] = $name;
}
}