简体   繁体   English

我在使用 php foreach 循环遍历数组时遇到困难

[英]i am having difficulty with php foreach loop through array

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我收到错误 500 请指导我我迷路了

stdClass Object is telling us you have an object inside your array. stdClass Object告诉我们您的阵列中有一个 object。 so your $response structure looks like this in code:所以你的$response结构在代码中看起来像这样:

[
    {
        "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的第二个引用:

$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.我怀疑这就是你写出来的目的,但本质上你是在声明 $values,然后不使用它......然后调用与 $values 相同的集合。

Next time enable error_reporting and include any errors and warnings you get.下次启用 error_reporting并包含您收到的任何错误和警告。

In this instance the issue is fairly obvious.在这种情况下,问题是相当明显的。 Your array only contains a single item - and that item is an object.您的数组仅包含一个项目 - 该项目是 object。 The echo construct expects a string (but can handle an implicit cast from other scalar types). echo构造需要一个字符串(但可以处理来自其他标量类型的隐式转换)。 It doesn't know how to output the object.它不知道如何 output object。 If the object implements the __tostring() method then PHP will call that to get a string to output.如果 object 实现了__tostring()方法,则 PHP 将调用该方法以获取到 output 的字符串。 But stdClass (the type of object you have here) does not have a _tostring() method.但是 stdClass (您在此处拥有的 object 的类型)没有 _tostring() 方法。

You also don't state in your question if you are looking for a specific value in the data - ie if you know its name - which is rather critical for how you approach the solution.如果您正在寻找数据中的特定值(即如果您知道它的名称),那么您的问题中也不会出现 state,这对于您如何处理解决方案至关重要。

That you have a stdclass object suggests that you have loaded it from serialized representation of data such as JSON.你有一个标准类 object 表明你已经从 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:如果您已将此输入转换为数组而不是 object(PHP 中的大部分内容将返回 stdclass 对象也可以返回关联数组),您可以简单地这样做:

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()您可以使用get_object_vars()将 object 转换为关联数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM