1

I have the following array $myarr and the $url value as such :

$url = "http://www.example.com/jane-doe/testingideas.html";


stdClass Object
(
    [items] => Array
        (
            [0] => stdClass Object
                (
                    [updated] => 2015-01-08 17:22:23.279210
                    [url] => http://www.example.com/jane-doe/testingideas.html
                    [score] => 21.053322

                )

            [1] => stdClass Object
                (
                    [updated] => 2015-01-08 17:22:23.279226
                    [url] => http://www.example.com/john-doe/ideas.html
                    [score] => 18.889984
                )
 )

)

The array has over 2000 values. I copied 2 to simplify things. I need to retrieve the score based on the URL. This is the code I wrote:

  $myarr = $output->items;
    foreach ($myarr as $val){
       if ($val->url == $url ) {
       $score = round($val->score); 
    } else $score = 'N/A';
    }

This does not work because it doesn't find any scores, even when the URLs are matching.

I also trimmed both URLs to remove any whitespaces but I am having the same issue.

4 Answers 4

1

You are unsetting $score in each loop, you'd better do:

$myarr = $output->items;
$score = 'N/A';
foreach ($myarr as $val){
    if ($val->url == $url ) {
       $score = round($val->score); 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

This should work for you:

foreach ($myarr as $items) {
    foreach($items as $item) {

        if ($item->url == $url ) {
            $score = round($val->score); 
        } else {
            $score = 'N/A';
        }

    }
}

2 Comments

your answer doesn't work its the same thing I did. it doesn't match the urls. sorry I forgot to add a line of code sorry. But that wasn't wrong. the actual urls are both outputted and they are matching to the eye. but the code doesn't resolve the score in those cases.
@stacknoob If you have a array that contains this(What you have shown in the question), but you assign it like this: $myarr = $output->items; what's then the output of print_r($myarr); ?! (Also try trim() with both values so there are no hidden whitespaces)
0
$url = "http://www.example.com/jane-doe/testingideas.html";
$myarr = $output->items;
foreach ($myarr as $val){
   if (strcmp($val->url,$url) == 0) {
        $score = round($val->score); 
   } else {
        $score = 'N/A';
   }
}

1 Comment

I used this then added after N/A case echo strcmp($val->url,$thisurl); and it returns numbers from -512 to 1280
0

Try using trim() to make sure there is no excesive whitespace:

if (trim($item->url) == trim($url))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.