illegal string offset '#id' in test.php - php

Not sure if data issue or program issue, sometimes below program
show error message :
PHP Warning : "illegal string offset '#id' in test.php"
PHP Warning : "illegal string offset 'Order' in test.php"...
....
$ret1=curlDest($transaction_url);
$retval = $ret1["Response"]["TransactionList"]["#TotalCount"];
foreach ($ret1["Response"]["TransactionList"]["Transaction"] as $v)
{
$transaction_id = $v["#Id"];
$order_id = array();
$orderid_query_string = '';
foreach ($v["Order"] as $order)
{...
}
the data of #ret1 is below
Array
<
[#Id] => 120852760
[Order] = > Array
<
[#Id] => YM152222
>
>

The error said
illegal string offset '#id'
You should use a capitalized #Id instead.

Related

Illegal string offset After PHP Upgrade

Warning [2] Illegal string offset 'title' - Line: 66
if(!isset($params['title'])) {
$params['title'] = "Hidden Content";
}
and Warning [2] Illegal string offset 'title' - Line: 3
eval("\$return = \"".$templates->get("lock_wrapper")."\";");
Please help!

Undefined property within foreach unless print_r'd

I feel I must be missing something here. PHP displays: "Notice: Undefined property: DateTime::$date" when trying to display a property from an outer loop but if I print_r() the line before, then it prints the value with no error. Comment it out again, and the error comes back?
I suspect perhaps $iteration['EventTime']->date is getting confused with the date() function in PHP but am not sure how to overcome this via an escape. I cannot change the format of the object.
Object:
array (size=6)
'LINE' => string '2' (length=1)
'EventTime' =>
object(DateTime)[2]
public 'date' => string '2019-09-11 01:25:10.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/London' (length=13)
'Tag' => string 'PLC32.J.A.10.NOREAD' (length=19)
'Area' => string 'Material\PLC32' (length=14)
'Message' => string '32JA10 Consecutive No-read Fault' (length=32)
'FAULT_STATE' => int 1
Loop:
foreach ($sqlsrv_fetch_all as $index => $row) {
if ($row['FAULT_STATE'] == 1) {
foreach ($sqlsrv_fetch_all as $iteration) {
if ($iteration['LINE'] < $row['LINE'] || $iteration['Tag'] != $row['Tag'] || $iteration['FAULT_STATE'] == 1) {
continue;
} else {
//print_r($row);
echo "row: ".$row['EventTime']->date;
}
}
}
}
Should output "row: 2019-09-11 01:25:10.000000";
But unless I call print_r() first, it errors and outputs nothing.
This is actually a bug in PHP (I'm yet to find its ID in the bugtracker) that has been fixed with PHP 7.4 - see here.
print_r is one of a few ways to display internals of an object, including its private properties. Apparently, before PHP 7.4, calling print_r on an object caused its properties to become public (presumably using reflections, so that their contents could be displayed), but never turned back after that.
The documentation does not name \DateTime::$date as a public field. If you want to display date, use \DateTime::format() method instead.

warning illegal string offset php

Hi I am currently having an error that says illegal string offset and i already searh here I just know that you get that warning if you are treating a string as if it is an array but I am certain that I am using it as an array can anybody help me thanks
$data2 = array('EquipmentName' => $this->input->post('txt_equipb'),
'EquipmentType' => $this->input->post('txt_equiptype'),
'RequirementID' => $id2);
foreach($data2 as $d) {
$data2s = array('EquipmentName' => $d['EquipmentName'],
'EquipmentType' => $d['EquipmentType'],
'RequirementID' => $d['RequirementID']);
}
You've misunderstand the meaning of foreach.(sigh)
Suggestions Provided:
just var_dump($d); before assignment of $data2s, and you'll know the result.
In the foreach, as you could see, each $d is only the value part of $data2, which means in every assignment of $data2s, there's no key as 'EquipmentName', only a simple string.
If you're just gonna loop once to set array values to a variable, you may just simply do it this way without using a foreach loop.
$data2 = array('EquipmentName' => $this->input->post('txt_equipb'),
'EquipmentType' => $this->input->post('txt_equiptype'),
'RequirementID' => $id2);
$data2s = $data2;
print_r( $data2s ); // check if has values

Array to string conversion with array_walk in PHP

I am tried to add the same string to every array value.
I tried to use array_walk() like I read it on this answer.
But I get: Notice: Array to string conversion
I have also tried to use array_map(), but I get same error notice.
working code
if ($voice->getValue() === Voice::Passive) {
array_walk($aller_form, function(&$value, $key) { $value .= ' être'; });
$aller_form = [
Mood::Indicatif => [
Tense::Futur_compose => [
Person::FirstPersonSingular => 'vais être',
Person::SecondPersonSingular => 'vas être',
Person::ThirdPersonSingular => 'va être',
Person::FirstPersonPlural => 'allons être',
Person::SecondPersonPlural => 'allez être',
Person::ThirdPersonPlural => 'vont être'
]
]
];
}
return $aller_form[$mood->getValue()][$tense->getValue()][$person->getValue()];
not working code
if ($voice->getValue() === Voice::Passive) {
array_walk($aller_form, function(&$value, $key) { $value .= ' être'; });
}
return $aller_form[$mood->getValue()][$tense->getValue()][$person->getValue()];
EDIT:
The complete error log:
Notice: Array to string conversion in on line 2
Warning: Illegal string offset 'futur_compose' on line 4
Warning: Illegal string offset 'firstPersonSingular' on line 4
(I see 6 times this three error lines for every Person once)

php - Warning: Illegal string offset but vardump shows correct string offset [duplicate]

This question already has answers here:
Illegal String Offset within PDO For Each loop
(2 answers)
Closed 8 years ago.
After completing a SELECT query on a MySQL database and applying the fetchAssoc() method to the result, I am receiving warnings when attempting to access the results in a foreach loop. Here is the code:
$query=$subquery->execute()->fetchAssoc();
foreach($query as $result) {
if ($result['active'] == 'Y'){ // (this is line 703)
$page_id = $result['page_id']; // (this is line 704)
if (!$package_pages[$page_id] || $package_pages[$page_id] != $page_id) { // line 705
$pages[] = $page_id;
}
}
I inserted var_dump($query) to inspect the results of the query. Here is an example of the output:
array (size=3)
'page_name' => string 'Apts & Rentals' (length=14)
'page_id' => string '49' (length=2)
'active' => string 'Y' (length=1)
array (size=3)
'page_name' => string 'Homepage' (length=8)
'page_id' => string '1' (length=1)
'active' => string 'Y' (length=1)
There are 25 arrays output from var_dump($query)
And here is a sample of the warnings:
Warning: Illegal string offset 'active' in ads_load() (line 703 of ...
Warning: Illegal string offset 'page_id' in ads_load() (line 704 of ...
Notice: Undefined index: Y in ads_load() (line 705 of ...
Why are the offsets 'active', and 'page_id' being flagged as illegal?
Where is the "Notice: Undefined index: Y" coming from since it is not being used as an index on line 705?
I don't believe this is a duplicate of Illegal String Offset within PDO For Each loop. The issue was resolved when I realized the code given above was placed within another loop which gave the impression I was dealing with an array of arrays rather than a single array each time.
You're accessing them incorrectly - you've got an associative array and you're trying to loop through it as if it were an array of associative arrays. This is your dumped data structure:
$query = array(
'page_name' => 'Homepage',
'page_id' => '1',
'active' => 'Y' );
You can get the results directly from $query, i.e.:
$query=$subquery->execute()->fetchAssoc();
if ($query['active'] == 'Y')
$page_id = $query['page_id'];
// ... the rest of your code ...
}
You could iterate through the keys and values of $query, but since you are interested in particular values from the $query array, there isn't much point.

Categories