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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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',
    ),
  ),
)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.