I am getting an error while sending an array to array_map function. Because that array contains an array inside of that.
$arr = array();
$value=array(
"result"=>$str,
"rightAnswer"=>$arr,
"tid"=>$topicId,
"view"=>$view,
);
$value = array_map('utf8_encode', $value);
This shows an error like
Message: utf8_encode() expects parameter 1 to be string, array given
parameter passed to utf8_encode should be a string. Hope the below callback function helps you to get it work.
function encode_data($val){
if(is_array($val)){
return $val = array_map('encode_data', $val);
}else{
return utf8_encode($val);
}
}
$value = array_map('encode_data', $value);
print_r($value);
Utf_encode() accept string parameter only and you are sending an array parameter
"rightAnswer"=>$arr
So thats why it is showing a warning.
$arr = '';
$value=array(
"result"=>$str,
"rightAnswer"=>$arr,
"tid"=>$topicId,
"view"=>$view,
);
$value = array_map('utf8_encode', $value);
it will work fine. I just made $arr = '' to string
I am getting error while using substr:
Warning: substr() expects parameter 3 to be long
I am new to php and could not locate the problem. I would appreciate any help.
Here is the code:
function prepare_string($passed_string,$length)
{
$matches = array("`","!","#","®","©","~","#","$","%","^","&","*","-","=","+","|","\\","[","{","]","}","(",")",";",":","\"","'",",","<",">",".","?","/","\'","\\","'","’");
$passed_string =substr($passed_string,0,$length);
for($i=0;$i<count($matches);$i++)
{
$passed_string = str_replace($matches[$i],"_",$passed_string);
}
$passed_string = str_replace(" ","_",$passed_string);
return $passed_string;
}
var_dump($length) and see what's in there.
remove foreach loop and instead put just this line $passed_string = str_replace($matches,"_",$passed_string);
add " " in $matches array too and then you can get rid of this line $passed_string = str_replace(" ","_",$passed_string);
Sounds like substr() is not receiving a well-formed value for some reason... One option would be to add a line inside the function that casts $length as an integer. Example:
function prepare_string($passed_string,$length)
{
// Add this line
$length = (int) $length;
$matches = array("`","!","#","®","©","~","#","$","%","^","&","*","-","=","+","|","\\","[","{","]","}","(",")",";",":","\"","'",",","<",">",".","?","/","\'","\\","'","’");
$passed_string =substr($passed_string,0,$length);
for($i=0;$i<count($matches);$i++)
{
$passed_string = str_replace($matches[$i],"_",$passed_string);
}
$passed_string = str_replace(" ","_",$passed_string);
return $passed_string;
}
i am newbie with Cake PHP. I am trying to run application that I have downloaded recently but it doesn't work.
My debug.log file says:
2012-07-31 12:31:47 Debug: Notice (8): Undefined offset: 0 in [C:\wamp\www\app\models\vwbrowse.php, line 78]
And my error.log file:
2012-07-31 12:31:47 Warning: Warning (2): array_keys() expects parameter 1 to be array, null given in [C:\wamp\www\app\models\vwbrowse.php, line 78]
And finally this is function that causes problem:
function afterFind($results, $primary)
{
if(!$primary) return $results;
$ret = array();
//we are primary and not part of an associated find
if(!is_array($results)) //find first
{
$tables = array_keys($results);
$record = array();
foreach($tables as $table){
$record = array_merge($record,$result[$table]);
}
$ret['Vwbrowse'] = $record;
}else{ //merge all arrays if separated
$tables = array_keys($results[0]);
foreach($results as $result){
$record['Vwbrowse'] = array();
foreach($tables as $table){
$record['Vwbrowse'] = array_merge($record['Vwbrowse'],$result[$table]);
}
$ret[] = $record;
}
}
return $ret;
}
**This is line 78: $tables = array_keys($results[0]);
**
What is wrong? Thanks in advance for solution.
You're checking to see if $results IS NOT an array, then telling your script to perform an array_keys() on it. Does not compute.
Also, you're trying to access $results[0] without confirmation that the 0th index exists.
Try this first:
if (array_key_exists(0, $results)) { ... }
The undefined index error is stating that the index of the array, in your case [0] is undefined.
You are checking if $results is an array with if (!is_array($results)), but not if it has any data. Try changing it to if (!is_array($results) || (count($results) == 0)).
I've did some research. I've found that you can serialize then unserialize to get a string... but I want a better solution.
I got an object array returned by IMAP pear module (function imap_getmailboxes).
public function GetMailBoxes(){
$List = imap_getmailboxes($this->Link, '{'.$this->Server.':'.$this->Port.'}', '*');
$Data = array();
if(is_array($List)){
foreach($List as $Key => $Value){
$Value = unserialize(serialize($Value));
$In = strpos($Value->name, '{');
$Out = strpos($Value->name, '}');
$Part = substr($Value, $Out);
$Value->real_name = explode($Value->delimiter, imap_utf7_decode($Part));
$Value->real_name = (isset($Value->real_name[1]) ? $Value->real_name[1] : null);
$Data[$Key] = $Value;
}
}
return $Data;
}
The problem here is strpos tell me this Warning: substr() expects parameter 1 to be string, object given in /home/david/domains/davidbelanger.net/public_html/panel/drivers/mail.php on line 178.
How Can I transform the object into a string ? Any idea, never done this before.
Thanks.
I think instead of
$Part = substr($Value, $Out);
you want
$Part = substr($Value->name, $Out);
You can override the magic method __toString()
As noted here: http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
I'am executing code :
<?php
$input="ABC123";
$splits = chunk_split($input,2,"");
foreach($splits as $split)
{
$split = strrev($split);
$input = $input . $split;
}
?>
And output that i want is :
BA1C32
But it gaves me Warning.
Warning: Invalid argument supplied for foreach() in /home/WOOOOOOOOHOOOOOOOO/domains/badhamburgers.com/public_html/index.php on line 4
chunk_split doesn't return an array but rather a part of the string.
You should use str_split instead:
$input="ABC123";
$splits = str_split($input, 2);
And don't forget to reset your $input before the loop, else it will contain the old data aswell.
It looks like http://php.net/manual/en/function.chunk-split.php returns a string, not an array. You could use str_split instead:
$input = "ABC123";
$splits = str_split( $input, 2 );
$output = "";
foreach( $splits as $split ){
$split = strrev($split);
$output .= $split;
}
As the documentation for chunk_split mentions clearly, http://php.net/manual/en/function.chunk-split.php chunk split returns a string.
foreach expects an array as first argument.