【问题标题】:PHP Array_Push not workingPHP Array_Push 不工作
【发布时间】:2014-01-11 08:02:17
【问题描述】:

我正在尝试组织一组通过数据库查询获得的结果。这是我用来执行此操作的代码:

$time_slots = $wpdb->get_results($query);
print_r($time_slots); 
echo ("<br/><br/><br/><br/>");
/*organize slots into array*/


$openings = array(); 
foreach($time_slots as $ts)
{
    if(empty($openings))
    {
        echo("Empty Array: ");
        echo ("<br/>");
        echo ("<br/>");
        echo("Inserting: ");
        print_r($ts); 
        echo ("<br/>");
        echo ("<br/>");
        $openings[$ts->route_date] = $ts; 
        echo("contents of Opening: ");
        echo ("<br/>");
        echo ("<br/>");
        print_r($openings); 
        echo ("<br/><br/><br/><br/>");

    }
    elseif (array_key_exists($ts->route_date, $openings)) 
    {
        echo("Same Day");
        echo ("<br/>");
        echo ("<br/>");
        echo("Inserting: ");
        print_r($ts); 
        echo ("<br/>");
        echo ("<br/>");
        array_push($openings[$ts->route_date][$ts->name], $ts); 
        echo("contents of Opening: ");
        echo ("<br/>");
        echo ("<br/>");
        print_r($openings); 
        echo ("<br/><br/><br/><br/>");
    }
    else
    {
        echo("New Day : ");
        echo ("<br/>");
        echo ("<br/>");
        echo("Inserting: ");
        print_r($ts); 
        echo ("<br/>");
        echo ("<br/>");
        $openings[$ts->route_date] = $ts; 
        echo("contents of Opening: ");
        echo ("<br/>");
        echo ("<br/>");
        print_r($openings); 
        echo ("<br/><br/><br/><br/>"); 
    }
}

/*return results*/
$result['openings'] = $openings; 
$result['time'] = $time_slots;  
$result['begin'] = $begin; 
$result['end'] = $end; 
$result['query'] = $query; 
$result['type'] = "success"; 
$result = json_encode($result);
print_r($openings); 

这是我print_r$ts 时单个结果的样子:

 stdClass Object ( [route_date] => 2014-01-10 [name] => 2 [openings] => 1 [appointments] => 0 ) 

这是一个循环的样子。你会注意到逻辑是有效的,并且一切都在它应该去的地方,但是添加 t:

> Empty Array: 

Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 



Same Day

Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 1 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 



Same Day

Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 2 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 



Same Day

Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 3 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 



Same Day

Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 4 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 



New Day : 

Inserting: stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-11] => stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 



Same Day

Inserting: stdClass Object ( [route_date] => 2014-01-11 [name] => 1 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-11] => stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 

当一切都说完了,我得到了这个结果:

Array ([2014-01-10] => stdClass Object ([route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01 -11] => stdClass 对象 ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [约会] => 0 ) [2014-01-12] => stdClass 对象 ( [ route_date] => 2014-01-12 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01-13] => stdClass Object ([route_date] => 2014-01-13 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-14] => stdClass Object ([route_date] => 2014-01-14 [name] => 0 [openings] => 1 [约会] => 0 ) [2014-01-15] => stdClass 对象 ( [route_date] => 2014-01-15 [名称] => 0 [职位] => 1 [约会] => 0 ) [2014-01-16] => stdClass Object ([route_date] => 2014-01-16 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01-17] = > stdClass Object ([route_date] => 2014-01-17 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01-18] => stdClass Object ([route_date] => 2014-01-18 [姓名] => 0 [职位] => 1 [约会s] => 0 ) [2014-01-19] => stdClass Object ( [route_date] => 2014-01-19 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014- 01-20] => stdClass 对象 ( [route_date] => 2014-01-20 [name] => 0 [openings] => 1 [约会] => 0 ) [2014-01-21] => stdClass 对象 ( [route_date] => 2014-01-21 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01-22] => stdClass Object ([route_date] => 2014-01- 22 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-23] => stdClass Object ([route_date] => 2014-01-23 [name] => 0 [openings ] => 1 [约会] => 0) [2014-01-24] => stdClass 对象 ([route_date] => 2014-01-24 [名称] => 0 [职位] => 1 [约会] => 0 ) [2014-01-25] => stdClass Object ( [route_date] => 2014-01-25 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-26] => stdClass Object ( [route_date] => 2014-01-26 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-27] => stdClass Object ( [route_date] = > 2014-01-27 [姓名] => 0 [职位空缺] => 1 [约会] => 0 ) [2014-01-28] => stdClass Object ([route_date] => 2014-01-28 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01 -29] => stdClass Object ( [route_date] => 2014-01-29 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-30] => stdClass Object ( [ route_date] => 2014-01-30 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01-31] => stdClass Object ([route_date] => 2014-01-31 [名称] => 0 [职位] => 1 [约会] => 0 ) [2014-02-01] => stdClass 对象 ([route_date] => 2014-02-01 [名称] => 0 [职位] => 1 [约会] => 0 ) [2014-02-02] => stdClass 对象 ( [route_date] => 2014-02-02 [名称] => 0 [职位空缺] => 1 [约会] => 0 ) [2014-02-03] => stdClass Object ([route_date] => 2014-02-03 [name] => 0 [openings] => 1 [appointments] => 0) [2014-02-04] = > stdClass Object ([route_date] => 2014-02-04 [name] => 0 [openings] => 1 [appointments] => 0) [2014-02-05] => stdClass Object ( [route_date] => 2014-02-05 [姓名] => 0 [职位空缺] => 1 [约会] => 0 ) [2014-02-06] => stdClass 对象 ( [route_date] => 2014-02-06 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02- 07] => stdClass Object ( [route_date] => 2014-02-07 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-08] => stdClass Object ( [route_date ] => 2014-02-08 [name] => 0 [openings] => 1 [appointments] => 0) [2014-02-09] => stdClass Object ([route_date] => 2014-02-09 [ name] => 0 [openings] => 1 [appointments] => 0) [2014-02-10] => stdClass Object ([route_date] => 2014-02-10 [name] => 0 [openings] = > 1 [约会] => 0) [2014-02-11] => stdClass 对象 ([route_date] => 2014-02-11 [名称] => 0 [职位] => 1 [约会] => 0) [2014-02-12] => stdClass 对象([route_date] => 2014-02-12 [name] => 0 [openings] => 1 [appointments] => 0)[2014-02-13] => stdClass 对象([route_date] => 2014-02-13 [name] => 0 [openings] => 1 [appointments] => 0)[2014-02-14] => stdClass 对象([route_date] => 2014 -02-14 [姓名] => 0 [职位空缺] => 1 [约会] => 0 ) [2014-02-15] => stdClass Object ( [route_date] => 2014-02-15 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02- 16] => stdClass 对象 ( [route_date] => 2014-02-16 [name] => 0 [openings] => 1 [约会] => 0 ) [2014-02-17] => stdClass 对象 ( [route_date ] => 2014-02-17 [name] => 0 [openings] => 1 [appointments] => 0) [2014-02-18] => stdClass Object ([route_date] => 2014-02-18 [ name] => 0 [openings] => 1 [appointments] => 0) [2014-02-19] => stdClass Object ([route_date] => 2014-02-19 [name] => 0 [openings] = > 1 [约会] => 0) [2014-02-20] => stdClass 对象 ([route_date] => 2014-02-20 [名称] => 0 [职位] => 1 [约会] => 0) [2014-02-21] => stdClass 对象([route_date] => 2014-02-21 [name] => 0 [openings] => 1 [appointments] => 0)[2014-02-22] => stdClass 对象([route_date] => 2014-02-22 [name] => 0 [openings] => 1 [appointments] => 0)[2014-02-23] => stdClass 对象([route_date] => 2014 -02-23 [姓名] => 0 [职位空缺] => 1 [约会] = > 0 ) [2014-02-24] => stdClass Object ([route_date] => 2014-02-24 [name] => 0 [openings] => 1 [appointments] => 0) [2014-02-25 ] => stdClass Object ( [route_date] => 2014-02-25 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-26] => stdClass Object ( [route_date] => 2014-02-26 [name] => 0 [openings] => 1 [appointments] => 0) [2014-02-27] => stdClass Object ([route_date] => 2014-02-27 [name ] => 0 [职位空缺] => 1 [约会] => 0 ) )

您会注意到只有第一个对象的第一个实例被添加到新数组$openings。

更新:

我刚刚意识到我需要一个多维数组,但是当我尝试添加第二维时,我收到以下错误:

 Cannot use object of type stdClass as array

我在这里做错了什么?我想要一个数组$openings[route_date][number],但我无法让它工作。任何帮助都会很棒。

【问题讨论】:

  • 顺便说一句,请在输出前添加echo "&lt;pre&gt;" 并在其后添加echo "&lt;/pre&gt;" 来打印此类数组。
  • 我不知道你能做到这一点!太棒了!!!!谢谢!!!!
  • 不客气,只是您应该注意的提示.. :)

标签: php mysql multidimensional-array


【解决方案1】:

这是因为array_push() 适用于数组,而不是对象。您应该将 stdClass 转换为数组。尝试使用:

$ts = (array) $ts;

【讨论】:

    【解决方案2】:

    在您的array_push($openings[$ts-&gt;route_date][$ts-&gt;name], $ts); 中,第一个参数似乎不是一个数组。如果第一个参数不是数组,array_push() 将引发警告。

    This should be array_push($openings, $ts);
    

    我做了一个很好的例子。

    //I'm using mysql and I will switch to  MySQLi or PDO_MySQL later) 
    
    $query = "SELECT * FROM events";
    $result = mysql_query($query);
    $openings = array();
    while ($row = mysql_fetch_object($result)) {
        array_push($openings, $row); 
    }
    print_r($openings);
    
    //output
    
    Array
    (
        [0] => stdClass Object
            (
                [idevent] => 1
                [event] => Event1
                [event_date] => 2014-01-06
            )
    
        [1] => stdClass Object
            (
                [idevent] => 2
                [event] => Event2
                [event_date] => 2014-01-07
            )
    
    )
    

    【讨论】:

      【解决方案3】:

      事实证明我需要做几件事。这是我需要的代码:

          $time_slots = $wpdb->get_results($query);
      echo("<pre>"); 
      //print_r($time_slots); 
      
      echo ("<br/><br/><br/><br/>");
      /*organize slots into array*/
      
      
      $openings = array(); 
      foreach($time_slots as $ts)
      {
          if(empty($openings))
          {
              echo("Empty Array: ");
              echo ("<br/>");
      
              echo("Inserting: ");
              echo ("<pre>");
              print_r($ts); 
              echo("</pre>");
              echo ("<pre>");
              $openings[$ts->route_date][$ts->name] = $ts; <--- Needed to add the [$ts->name]
              echo("</pre>");
              echo("contents of Opening: ");
              echo ("<pre>");
              print_r($openings); 
              echo ("</pre>");
      
          }
          elseif (array_key_exists($ts->route_date, $openings)) 
          {
              echo("Same Day");
              echo("Inserting: ");
              echo ("<pre>");
              print_r($ts); 
              echo("</pre>");
              $openings[$ts->route_date][$ts->name]=$ts; 
              echo("contents of Opening: ");
              echo ("<br/>");
              echo ("<br/>");
              print_r($openings); 
              echo ("<br/><br/><br/><br/>");
          }
          else
          {
              echo("New Day : ");
              echo("Inserting: ");
              echo ("<pre>");
              print_r($ts); 
              echo("</pre>");
              echo ("<pre>");
              $openings[$ts->route_date][$ts->name] = $ts; 
              echo("</pre>");
              echo("contents of Opening: ");
              echo ("<pre>");
              print_r($openings); 
              echo ("</pre>");
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-19
        • 1970-01-01
        • 1970-01-01
        • 2017-04-21
        • 2013-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多