doudu4282 2011-04-04 13:17
浏览 12
已采纳

数组合PHP中的问题

HI,

I got two array.

var_dump($tbDateArr);
var_dump($tbTitleArr);

output:

array
  0 => 
    object(stdClass)[16]
      public 'eventDate' => string '5' (length=1)
  1 => 
    object(stdClass)[17]
      public 'eventDate' => string '16' (length=2)
array
  0 => 
    object(stdClass)[18]
      public 'eventTitle' => string 'mar' (length=10)
  1 => 
    object(stdClass)[19]
      public 'eventTitle' => string 'tri' (length=10)

BTW,I print them as this,

print_r($tbDateArr);
echo '<br>';
print_r($tbTitleArr);
Array ( [0] => stdClass Object ( [eventDate] => 5 ) [1] => stdClass Object ( [eventDate] => 16 ) ) 
Array ( [0] => stdClass Object ( [eventTitle] => mar ) [1] => stdClass Object ( [eventTitle] => tri ) )

I tried to combin them,

$dataArr = array_combine($tbDateArr, $tbTitleArr);

I JUST WANT THE SIMPLY RESULT as this,

Array
(
    [5]  => mar
    [16]    => tri

)

Is there anything wrong? Appreciated for your help.

[updated with array_merge]

array
  0 => 
    object(stdClass)[16]
      public 'eventDate' => string '5' (length=1)
  1 => 
    object(stdClass)[17]
      public 'eventDate' => string '16' (length=2)
  2 => 
    object(stdClass)[18]
      public 'eventTitle' => string 'fuzhou mar' (length=10)
  3 => 
    object(stdClass)[19]
      public 'eventTitle' => string 'weihai tri' (length=10)
  • 写回答

4条回答 默认 最新

  • dopr25398 2011-04-04 13:28
    关注

    Assuming that $tbDateArr and $tbTitleArr entries only have a single property (eventDate and eventTitle respectively), you can do this:

    $array = array_combine(
        array_map('current', $tbDateArr),
        array_map('current', $tbTitleArr)
    );
    

    If they have (or can have) more than a single property, you're better off using a good old foreach, assuming they have matching keys (if they don't, just array_values them beforehand):

    $array = array();
    foreach ($tbDateArr as $key => $value) {
        $array[$value] = $tbTableArr[$key];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令