在没有键的情况下将值从数组转移到一个数组

时间:2019-04-13 14:28:00

标签: php arrays

我的数组在数组操作上遇到麻烦

#include <memory>
#include <cassert>

template <typename T>
class unique_ref {
public:
    // a number of problems here, but that is a discussuion for another day
    template <class... Types>
    unique_ref(Types&&... Args) 
    : mPtr(std::make_unique<T>(std::forward<Types>(Args)...))
    { }

    // unique_ref is implicitly move-only

    // see check below
    bool has_value() const {
        return bool(mPtr);
    }

    // here I am implicitly propagating the container's constness to the 
    // inner reference yielded. You may not want to do that.
    // note that all these accessors are marshalled through one static function
    // template. This gives me control of behaviour in exactly one place. 
    // (DRY principles)
    auto operator*() -> decltype(auto) {
        return *get_ptr(this);
    }

    auto operator*() const -> decltype(auto) {
        return *get_ptr(this);
    }

    auto operator->() -> decltype(auto) {
        return get_ptr(this);
    }

    auto operator->() const -> decltype(auto) {
        return get_ptr(this);
    }

private:
    using implementation_type = std::unique_ptr<T>;
    implementation_type release() { return std::move(mPtr); }

    // this function is deducing constness of the container and propagating it
    // that may not be what you want.
    template<class MaybeConst>
    static auto get_ptr(MaybeConst* self) -> decltype(auto)
    {
        auto ptr = self->mPtr.get();
        assert(ptr);
        using self_type = std::remove_pointer_t<decltype(self)>;
        if constexpr (std::is_const<self_type>())
            return static_cast<T const*>(ptr);
        else
            return ptr;
    }

private:
    implementation_type mPtr;
};

struct foo
{
};

auto generate()->unique_ref<foo> {
    return unique_ref<foo>();
}

void test()
{
    auto rfoo1 = generate();
    auto rfoo2 = generate();
//    auto rfoo3 = rfoo1; not copyable

    // we have to assume that a user knows what he's doing here
    auto rfoo3 = std::move(rfoo1);

    // but we can add a check
    assert(!rfoo1.has_value());

    auto& a = *rfoo3;
    static_assert(!std::is_const<std::remove_reference_t<decltype(a)>>());

    const auto rfoo4 = std::move(rfoo3);
    auto& b = *rfoo4;
    static_assert(std::is_const<std::remove_reference_t<decltype(b)>>());
}

我希望得到

[time] => Array
     (
       [0] => Array
          (
           [total_time] => 02:10:00
          )

       [1] => Array
          (
           [total_time] => 01:05:00
          )

       [2] => Array
          (
           [total_time] => 00:50:00
          )
  )

我尝试过foreach

$total_time = ('02:10:00', '01:05:00', '00:50:00');

但是我的输出是:

 foreach ($data['time'] as $key) {
    array_push($total_time, $key['total_time']);
 }

有没有办法得到这个

print_r($total_time) = 3

1 个答案:

答案 0 :(得分:1)

您可以像使用foreach或使用array_column一样简单地完成操作:

// you could use a foreach loop like you did
$total_time = [];
foreach ($data['time'] as $key) {
    $total_time[] = $key['total_time'];
}

// or array_column
$total_time = array_column($data['time'], 'total_time');