Home>

Thank you very much. I obtained the following associative array with the explode function.

// Array ([0] => ソ [1] =>1 [2] =>manbyo1 [3] =>hello [4] =>2018/08/24 13:02 : 14 [5] =>2 [6] =>manbyo1 [7] =>hello [8] =>2018/08/24 13:02:18 [9] =>3 [10] =>manbyo1 [11] =>hello [12] =>2018/08/24 13:02:20 [13] =>4 [14] =>manbyo1 [15] =>hello [16] =>24/08/2018 13:02:23 [17] =>5 [18] =>manbyo1 [19] =>hello [20] =>2018/08/24 13:02:24 [21] =>6 [22] =>manbyo1 [23] =>hello [24] =>2018/08/24 13:02:26)

From here
[1] = 1
[5] = 2
[9] = 3
[13] = 4

Please tell me how to assign only the part of

to a variable.

php
  • Answer # 1

    You can filter by judging only numbers

    <? php
    $list = array (
        "mojibake",
        1, "manbyo1", "hello", "2018/08/24 13:02:14",
        2, "manbyo1", "hello", "2018/08/24 13:02:18",
        3, "manbyo1", "hello", "24/08/2018 13:02:20",
        4, "manbyo1", "hello", "2018/08/24 13:02:23",
        5, "manbyo1", "hello", "2018/08/24 13:02:24",
        6, "manbyo1", "hello", "2018/08/24 13:02:26",
    );
    $list = array_filter (
        $list,
        function ($item) {
            return is_numeric ($item);
        }
    );
    print_r ($list);

    Result

    Array
    (
        [1] =>1
        [5] =>2
        [9] =>3
        [13] =>4
        [17] =>5
        [21] =>6
    )

  • Answer # 2

    Does this mean?

    $a = ["A", "B", "C", "D", "E"];
    $b = array_filter ($a, function ($x) {
      return in_array ($x, [1,3])! == false;
    }, ARRAY_FILTER_USE_KEY);
    print_r ($b);


    Take $1 and # 3 from $a