-2

i need help fetching values from an array,

Array ( [0] => stdClass Object ( 
[service] => text 
[reference] => 12345678 
[status] => approved 
[sender] => webmaster 
[mobile] => 123456789 
[message] => I need hekp. 
[data] => 
[price] => 3.2500 
[units] => 1 
[length] => 86c/1p 
[send_date] => 2021-05-20 15:42:41 
[date] => 2021-05-20 15:42:41 )  )

what i have done

$response = json_decode($result);
foreach($response as $value){
echo $value['units'];
}

i get an error 500 please kindly guide me i am lost

Nuel Young
  • 13
  • 2
  • 2
    See where it says `stdClass Object` in the dump? Your array contains an object, so you need to use property access (`$var->prop`), not array access (`$var['index']`). – El_Vanja May 20 '21 at 20:48
  • For future reference, an error 500 is **always** accompanied by an error message somewhere. You can adjust PHP's error_reporting settings, or you can take a look in the webserver's logs where the message will often wind up. – ceejayoz May 20 '21 at 20:49
  • And for future reference, error 500 is just a generic error meant for the end user, not the developer, as it's intentionally stripped of any useful information. To see the actual error you have two options: 1) check your error log and 2) [display errors](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) during development. – El_Vanja May 20 '21 at 20:49
  • 1
    Yes, but your code will work if you do `$response = json_decode($result, true);` to get an array. https://www.php.net/manual/en/function.json-decode.php – AbraCadaver May 20 '21 at 20:57
  • @El_Vanja i used the property like you said $response->units but i got a null value – Nuel Young May 20 '21 at 21:00
  • Because you called it on `$response`. That's an array. `$value` is the one that holds the object. – El_Vanja May 20 '21 at 21:01

2 Answers2

0

stdClass Object is telling us you have an object inside your array. so your $response structure looks like this in code:

[
    {
        "service": "text",
        "reference": 12345678
    }
]

try this:

$response = json_decode($result);
foreach($response as $value){
    echo $value->units;
}

in response yo your comment on this answer:

if you wanted to neaten this up you could replace the second reference to $response->data with $values:

$values = $response->data;
foreach($values as $value){
    echo "<table>
    <td>$value->reference</td>
    <td>$value->message</td>
    <td>$value->sender</td>
    <td>$value->mobile</td>
     <td>$value->status</td>
     <td>$value->units</td>
    </table>";
}

I suspect this is what you were going for when you wrote it out, but essentially you were declaring $values and then not using it.... then making a call to the same collection as $values.

Vulps
  • 180
  • 3
  • 16
  • Thanks for your help, i also followed what @El_Vanja said and i arrived here ```$values = $response->data; foreach($response->data as $value){ echo "
    $value->reference $value->message $value->sender $value->mobile $value->status $value->units
    "; }```
    – Nuel Young May 20 '21 at 21:33
  • @NuelYoung I've updated my answer with a response to your comment, take a look and see if it helps you any further :) – Vulps May 21 '21 at 10:23
-1

Next time enable error_reporting and include any errors and warnings you get.

In this instance the issue is fairly obvious. Your array only contains a single item - and that item is an object. The echo construct expects a string (but can handle an implicit cast from other scalar types). It doesn't know how to output the object. If the object implements the __tostring() method then PHP will call that to get a string to output. But stdClass (the type of object you have here) does not have a _tostring() method.

You also don't state in your question if you are looking for a specific value in the data - i.e. if you know its name - which is rather critical for how you approach the solution.

That you have a stdclass object suggests that you have loaded it from serialized representation of data such as JSON. If you had converted this input to an array instead of an object (most of the things in PHP which will return stdclass objects can also return associative arrays) you could simply have done this:

function walk_array($in, $depth) 
{
    if ($depth><MAXDEPTH) {
       // because you should never use unbounded recursion
       print "Too deep!\n";
       return;
    }
    foreach ($in as $k=>$item) {
       print str_pad("", $depth, "-") . $k . ":";
       if (is_array($item)) {
           print "\n";
           walk_array($item, $depth+1);
       } else {
           print $item;
       }
    }
}

You can convert an object into an associative array with get_object_vars()

symcbean
  • 47,736
  • 6
  • 59
  • 94