【问题标题】:can not get an element from an array无法从数组中获取元素
【发布时间】:2017-09-22 04:08:17
【问题描述】:

我正在尝试获取制造商字段的值列表,但除了白屏,没有错误,一切似乎都正确,但没有任何效果

<?php

function removeBomUtf8($s){
  if(substr($s,0,3)==chr(hexdec('EF')).chr(hexdec('BB')).chr(hexdec('BF'))){
        return substr($s,3);
    }else{
        return $s;
    }
}

$url = "http://www.pureexample.com/backend/data/car-sale.json";
$content = file_get_contents($url);
$clean_content = removeBomUtf8($content);
$decoded = json_decode($clean_content);

while ($el_name = current($decoded)) {
    if ($el_name == 'Manufacturer') {
        echo key($decoded).'<br />';
    }
    next($decoded);
}
?>

【问题讨论】:

  • 提示:"\xEF\xBB\xBF"明显比你那里的短。
  • $el_name 不会是具有ManufacturerSoldMonth 属性的对象吗?它肯定不会是字符串
  • 也不清楚为什么你会使用真正晦涩难懂的current/next 方法进行迭代,而不是直接的foreach($decoded as $el_name)
  • 它用于从 BOM 中清除 json。你可以通过 echo $decoded 看到 json;
  • 你的预期输出是什么?

标签: php arrays json list


【解决方案1】:

$decoded 是一个对象数组(StdClass 类型):当您调用current 时,您将返回具有三个属性的对象:Manufacturer、Sold 和 Month。如果您只想打印出制造商名称,请按如下方式编辑您的代码:

while ($el_name = current($decoded)) {
  echo $el_name->Manufacturer . '<br>';
  next($decoded);
}

但是,正如其中一位评论者所提到的,当前/下一个语法非常晦涩难懂,而且不容易理解。你最好写:

foreach($decoded as $el_name) {
  echo $el_name->Manufacturer . '<br>';
}

【讨论】:

    【解决方案2】:

    $decoded 数组中的每个条目都应该是类似的 stdclass 对象表示

    {
        "Manufacturer": "Toyota",
        "Sold": 1200,
        "Month": "2012-11"
    }
    

    从您的评论开始...

    我希望看到所有带有制造商部分的行,例如 1“制造商”:“丰田”、2“制造商”:“福特”3“制造商”:“宝马”等

    你可能追求的是

    $manufacturers = array_map(function($el) {
      return sprintf('"Manufacturer": "%s"', $el->Manufacturer);
    }, $decoded);
    echo '<ol><li>', implode('</li><li>', $manufacturers), '</li></ol>';
    

    演示~https://eval.in/866131


    或者,只需使用 foreach 循环遍历 $decoded...

    ?>
    <ol>
      <?php foreach ($decoded as $el) : ?>
      <li>"Manufacturer": "<?= htmlspecialchars($el->Manufacturer) ?>"</li>
      <?php endforeach ?>
    </ol>
    

    【讨论】:

      【解决方案3】:

      您正在处理一个对象,为了使其更简单,请尝试运行它以获得您所说的结果:

      <?php
      
      function removeBomUtf8($s){
        if(substr($s,0,3)==chr(hexdec('EF')).chr(hexdec('BB')).chr(hexdec('BF'))){
              return substr($s,3);
          }else{
              return $s;
          }
      }
      
      $url = "http://www.pureexample.com/backend/data/car-sale.json";
      $content = file_get_contents($url);
      $clean_content = removeBomUtf8($content);
      $decoded = json_decode($clean_content);
      
      foreach ($decoded as $car) {
      echo "Manufacturer is: $car->Manufacturer" . "<BR>";
      echo "Sold is: $car->Sold" . "<BR>";
      echo "Month is: $car->Month" . "<BR>";
      echo "<P>";
      }
      ?>
      

      这是上面 PHP 代码的输出:

      Manufacturer is: Toyota
      Sold is: 1200
      Month is: 2012-11
      
      Manufacturer is: Ford
      Sold is: 1100
      Month is: 2012-11
      
      Manufacturer is: BMW
      Sold is: 900
      Month is: 2012-11
      
      Manufacturer is: Benz
      Sold is: 600
      Month is: 2012-11
      
      Manufacturer is: GMC
      Sold is: 500
      Month is: 2012-11
      
      Manufacturer is: HUMMER
      Sold is: 120
      Month is: 2012-11
      

      【讨论】:

        猜你喜欢
        • 2017-08-15
        • 1970-01-01
        • 2014-12-19
        • 2020-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-17
        • 1970-01-01
        相关资源
        最近更新 更多