I am using the following array:
$words = $xpath->query('//div[#id="relatedSearches"]/a');
$related_words=array();
foreach ($words as $word) {
$related_words[]=trim($word->nodeValue);
}
var_dump($related_words); // to debug
and the output is:
array
0 => string 'internet books' (length=14)
1 => string 'internet business' (length=17)
2 => string 'internet marketing' (length=18)
and in my code, at a later stage, I use the following code segment to assign values to another array:
$values = array_values($related_words);
Everything is okay, if the output of $related_words does not contain spaces e.g
<li>internet <li>book <li>business
Whenever a space is encountered in input array $related_words I get the error:
Catchable fatal error: Object of class DOMElement could not be converted to string in line 22
Line 22 is $values = array_values($input);
Anyone can please give me a solution to this? Or help?
I searched the same error on stackoverflow but failed to get help..
Related
This question already has answers here:
How create an array from the output of an array printed with print_r?
(11 answers)
Closed 1 year ago.
Array ( [status] => 1 [message] => Logged In Successfully. )
I Want to access status from this array like string.
I fetch this Response from API.
It's look not good.not like array or not like json.
I am not able to access key,so any one can help me, now.
You could achieve this using preg_match perhaps? See it working over at 3v4l.org but it is not a very dynamic solution and I'm assuming the status will always be a single integer.
preg_match('/(\Sstatus\S => \d)/',
'Array ( [status] => 1 [message] => Logged In Successfully. )',
$matches
);
if(!empty($matches))
{
$status = (int) $matches[0][strlen($matches[0]) -1]; // 1
}
To improve #Jaquarh's answer, you could write this function that helps you extract the values using any desired string, key and expected type.
I have added a few features to the function like not minding how many spaces come between the => separator in the string, any value-type matching, so that it can retrieve both numeric and string values after the => separator and trimming of the final string value. Finally, you have the option of casting the final value to an integer if you want - just supply an argument to the $expected_val_type argument when you call the function.
$my_str is your API response string, and $key_str is the key whose value you want to extract from the string.
function key_extractor($my_str, $key_str, $expected_val_type=null) {
// Find match of supplied $key_str regardless of number of spaces
// between key and value.
preg_match("/(\[" . $key_str . "\]\s*=>\s*[\w\s]+)/", $my_str, $matches);
if (!empty($matches)) {
// Retrieve the value that comes after `=>` in the matched
// string and trim it.
$value = trim(substr($matches[0], strpos($matches[0], "=>") + 2));
// Cast to the desired type if supplied.
if ($expected_val_type === 'int') {
return ((int) $value);
}
return $value;
}
// Nothing was found so return null.
return NULL;
}
You could then use it like this:
key_extractor($res, 'status', 'int');
$res is your API response string.
I have a function that loops through an array and runs database operations within each loop.
function myfunc($array) {
foreach($array as $a) {
// db operations here
echo $a["one"].'<br />';
echo $a["two"].'<br />';
}
}
but then sometimes, I have a single-dimensional array such as
$x = array(
'one' => '1',
'two' => '2'
);
myfunc($x);
however, it's falling to loop because there is nothing to loop over.
I get errors saying:
Warning: Illegal string offset 'one'
Warning: Illegal string offset 'two'
i know I could make the single dimensional array $x[] but that would mean I have to reset it to empty each time ($x = array();) - which isn't a problem but if it;s possible to loop over a single dimensional array, i'd rather do that
If you know you have only one entry, you'll want to wrap it into a array of one, like this:
myfunc([$array]);
We're running PHP 5.3.10-1ubuntu3.15 with Suhosin-Patch, and I just ran across the weirdest thing. I keep getting an Array to string conversion error.
Here is some code with line numbers:
115 $report['report'][$key]['report'] = array();
116 watchdog('ranking_report_field', 'key is a: ' . gettype($key), array(), WATCHDOG_NOTICE);
117 $report['report'][$key]['report'] = array(
'#markup' => "<p>No information available.</p><p>For questions, <a href='mailto:$emailAddr'>email</a> your account executive ($emailAddr).</p>",
);
Here are Drupal's (sequential) logs for those line numbers:
Notice: Array to string conversion in foo() (line 115 of /var/www/...
key is a: string
Notice: Array to string conversion in foo() (line 117 of /var/www/...
So far as I can tell there's no array to string conversion that should be taking place. Someone help me out with a second pair of eyes, please - or is this some kind of bug that just hit PHP?
One of the array keys is mapped to a string not an array. Here is a program for how such an error could occur.
<?php
$key = 0;
$report = array();
$report['report'] = array();
$report['report'][$key] = 'report';
// Array to string conversion error
$report['report'][$key]['report'] = array();
// what I assume you are expecting is
$report['report'][$key] = array();
$report['report'][$key]['report'] = array(); // no more notices
NOTE: at his time the OP has not included info for how the array is created
I have a php code. I am just trying out to define and get array. The below is the code.
<?php
$query = 'summer';
$query['jink'] = array( 1,4,5,6 );
var_dump($query);
var_dump($query['jink']);
?>
var_dump returns:
string 'Aummer' (length=6)
string 'A' (length=6)
The output is not as expected. it should give something from (1,4,5,6)
I fixed your errors in order to show the issue:
$query = 'summer';
$query['jink'] = array( 1,4,5,6 );
$query is a string "summer" so ['jink'], not being a valid string offset is converted to 0 and it accesses the first character of "summer". Also, array( 1,4,5,6 ) is not a string it is Array and the "A" from Array is assigned to offset 0 of "summer" yielding "Aummer":
var_dump($query);
Now again you are getting string offset 0 which is "A" from "Aummer":
var_dump($query['jink']);
If you use error reporting:
error_reporting(E_ALL);
ini_set('display_errors', '1');
You will see:
PHP Warning: Illegal string offset 'jink' in file line
PHP Notice: Array to string conversion in file line
PHP Warning: Illegal string offset 'jink' in file line
What you maybe want is:
$query = ['summer'];
$query['jink'] = [1,4,5,6 ];
var_dump($query);
var_dump($query['jink']);
PHP Sandbox: http://sandbox.onlinephpfunctions.com/code/50f22fb5de571baf5978ab37e8cd645ec174125e.
Well the error is that you set the $query as a string then turn it into an array.You should simply edit $query = 'summer' to $query[] = 'summer' as this link shows.
I am using Codeigniter framework of PHP and trying to extract keywords from the page. The complete code for reference can be seen here. It is not ready-made though.
The issue is due to the array function in the following line:
$keywordCounts = array_count_values( $words );
The error message being displayed is as follows:
A PHP Error was encountered
Severity: Warning
Message: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values!
EDITED: The array $words for reference can be found here.
There are no special symbols or invalid characters to my knowledge in the $words array. Hyphens and periods are not read by the function or is there some other issue ?
you have null values in your array. you have to replace them before working with array_count_values like this:
$x = array('s'=>'ss', 'a',4 , 'sss' => null);
$ar = array_replace($x,array_fill_keys(array_keys($x, null),''));
$v = array_count_values($ar);
var_dump($v);
which will result:
array (size=4)
'ss' => int 1
'a' => int 1
4 => int 1
'' => int 1