Jump to content

PHP Array Question: Fatal Error - Cannot use string offset as an array in


RIRedinPA

Recommended Posts

I searched around the internet for an explanation of this but I just can't seem to understand what I am doing wrong. I'm making a SOAP call to an API and getting these results returned:

 

Array ( [searchResult] => Array ( [id] => Array ( [0] => 077BE86E-372F-4390-8F0A-EC163706D8FC [1] => 318A39E4-EC95-4DA9-B92E-4C081E10A974 [2] => D4489F50-760C-403D-AB79-3C962356F784 ) ) )

Array

 

If I use this code I don't get an error, I get a count of 1:

 

print_r($SearchCventResults);
echo "<br>" . count($SearchCventResults['SearchResult']);
echo "<hr>";

 

if I change the code to this, I get the string offset error

 

print_r($SearchCventResults);
echo "<br>" . count($SearchCventResults['SearchResult']['Id']);
echo "<hr>";

 

I read this about the string offset error:

 

PHP5 Error message that is caused by attempting to assign a value to an array element of a variable that is declared as a string.

 

It's kind of Greek to me but it sounds like the error is when you try to assign something as a string first and then give it an array value? I guess I am confused as I am not assigning $SearchCventResults as anything but an array - it passes variables as XML to a function in the API and returns the results as an array. --> $SearchCventResults = $client->call("Search", $searchXML);

 

Any help would be greatly appreciated.

 

 

I think it's just saying that you are trying to count something that is a string, and count is meant to count array elements, so your out of luck. [id] does appear to be an array though, so I may be wrong.

 

What is strange is that the Id array seems to be an associative array, but lacks a key for the first element. I can't even reproduce the array, but if I copy and paste your array, the key is an asterisk. Very odd.

I think it's just saying that you are trying to count something that is a string, and count is meant to count array elements, so your out of luck. [id] does appear to be an array though, so I may be wrong.

 

What is strange is that the Id array seems to be an associative array, but lacks a key for the first element. I can't even reproduce the array, but if I copy and paste your array, the key is an asterisk. Very odd.

 

Thanks sKunkbad, that's where I was getting confused...['Id'] is an array so I'm not sure why I can't produce a count on it or loop through it.

Sounds weird to me, but the count man has a way to count recursive elements. Not sure if this is what you are looking for

 

Please see the Array section of the manual for a detailed explanation of how arrays are implemented and used in PHP.

Parameters

var

    The array.

mode

    If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array. The default value for mode is 0. count() does not detect infinite recursion.

 

So you "could" do this:

 

<?php
$count = (count($SearchCventResults['SearchResult'], 1) - count($SearchCventResults['SearchResult']));
?>

 

But that is really a hodged podged way of doing it. This would probably be preferred.

<?php
$temp = $SearchCventResults['SearchResult']['Id'];
$cntIds = count($temp);
$temp = null;

echo $cntIds . " is how many elements are in SearchResult.";
?>

 

But I am interested in why it does not work. I will probably do some more testing on my machine and let you know what I find out if anything.

 

EDIT:

I decided to do this:

 

<?php
$array = array("SearchResult" => array("Id" => array("bob", "you", "me")));

$cnt = count($array['SearchResult']['Id']);

echo "\$cnt should be 3, it is: {$cnt} for array \$array.";
die();
?>

 

And it returns three. There is something going weird in your code, not sure what it is but yea.

 

Edit:

In reply to:

I can't even reproduce the array, but if I copy and paste your array, the key is an asterisk. Very odd.

 

That is because he did not post the array results in a code tag. The Forum takes [0] as a list element, thus does not display it.

Please put the following code in to dump the array in a better format:

<?php
echo '<pre>' . var_export($SearchCventResults,true) . '</pre>';
?>

and post the results here between


tags.

 

Ken

Hi Ken

 

Thanks for the help. Here's the output:

 

array (
  'SearchResult' => 
  array (
    'Id' => 
    array (
      0 => '077BE86E-372F-4390-8F0A-EC163706D8FC',
      1 => '318A39E4-EC95-4DA9-B92E-4C081E10A974',
    ),
  ),
)

I just tried:

<?php
$SearchCventResults = array (
  'SearchResult' =>
  array (
    'Id' =>
    array (
      '077BE86E-372F-4390-8F0A-EC163706D8FC',
      '318A39E4-EC95-4DA9-B92E-4C081E10A974',
    ),
  ),
);
print_r($SearchCventResults);
echo count($SearchCventResults['SearchResult']). "<br>";
echo count($SearchCventResults['SearchResult']['Id'])."<br>";
?>

 

And I get:

Array
(
    [searchResult] => Array
        (
            [id] => Array
                (
                    [0] => 077BE86E-372F-4390-8F0A-EC163706D8FC
                    [1] => 318A39E4-EC95-4DA9-B92E-4C081E10A974
                )

        )

)
1
2

Which is correct.

 

I think we need to see more of your code and the exact error message.

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.