dtdb99743 2018-10-22 13:57
浏览 10

PHP阵列组织

I'm trying to get a well-better formated list of information.

Basically what I have right now is a multi dimensional array that has multiple entries in it with different data.

If I would do foreach $bigarray, I would get something like this:

 array(
    "id" => 1,
    "company" => "bar",
    "advertisment" => "Selling X",
    "user_id" => "200",
    "uri" => "bbbbbbxa"
);

 array(
        "id" => 2,
        "company" => "bar",
        "advertisment" => "Selling ABC",
        "user_id" => "200",
        "uri" => "xxxaaaa"
    );

 array(
        "id" => 3,
        "company" => "CROMUS",
        "advertisment" => "Selling BBB",
        "user_id" => "222",
       "uri" => "bsaxxaa"
    );

** notice the same user_id and company name **

** I want a unique list of companies with ads corresponding to it **

From the above I would get 3 'squares' of data like this:

  • Company Name
  • Advertisment Title

================

  • Same Company Name
  • Same advertisment title

================

  • Different Company Name
  • Different advertisment title

=========================

but I would want to get:

=========================

  • Company Name
  • first advertisment title
  • second advertisment title
  • ** so on if any other entries with same user_id are found **

=========================

  • Different Company Name
    • Different Advertisment

=========================

So basically I would want array 1 to add somehow to array 0 so when I echo in the foreach($bigarray as $post), I go something like

echo $post['advertisment'].' '.$post['uri']
echo $post['advertisment_2'].' '.$post['uri2']

Note: I might have more than 2 values , I might have 6 ads on the same company and I basically want to organise them by the id (the top one would be the newest)

Until now I did something like this just to realise i took the wrong approach:

foreach($latest->posts as $key => $post) {
    if(in_array($post->user_id,$iduri)){
        array_push($iduri, $post->user_id);
    }
    if(in_array($post->user_id,$iduri)) {
        array_push($cuie,$post->title);
        array_push($urix,$post->uri);
    } else {
        for ($i = 0; $i <= count($cuie); $i++) { 
            echo $cuie[i];
            echo $urix[i];
        }  
    } // end if in array
} //end foreach
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。