douran7929
douran7929
2016-10-27 13:39

在php中制作其他数组的一部分数组

  • php
  • wordpress
  • arrays
已采纳

So I'm working on collecting a lot of data from a Gravity Forms form, which stores the values in array called $entry[].

I need to gather all the values from $entry[14] through $entry[30] or something around that. Is there a way to do a foreach loop or something for only those selected parts of the array?

my current solution is rather repetitive.

$valuesArray = array($entry["14"], $entry["15"], $entry["16"], $entry["17"], $entry["18"], $entry["19"]);

Var dump of $entry gives the following

array(30) { 
["id"]=> string(2) "48" 
["form_id"]=> string(1) "1" 
["date_created"]=> string(19) "2016-10-27 13:30:24" 
["is_starred"]=> int(0) 
["is_read"]=> int(0) 
["ip"]=> string(13) "..." 
["source_url"]=> string(26) "http://examplesite.com/" 
["post_id"]=> NULL 
["currency"]=> string(3) "DKK" 
["payment_status"]=> NULL 
["payment_date"]=> NULL 
["transaction_id"]=> NULL 
["payment_amount"]=> NULL 
["payment_method"]=> NULL 
["is_fulfilled"]=> NULL 
["created_by"]=> string(1) "1" 
["transaction_type"]=> NULL 
["user_agent"]=> string(82) "" 
["status"]=> string(6) "active" 
[9]=> string(14) "Jeg er en mand" 
[10]=> string(9) "50-54 år" 
[11]=> string(9) "Kommune 3" 
[16]=> string(1) "3" 
[15]=> string(1) "0" 
[6]=> string(0) "" 
[1]=> string(0) "" 
[8]=> string(0) "" 
[12]=> string(0) "" 
[2]=> string(0) "" 
[5]=> string(0) "" } 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • dongzhong5182163 dongzhong5182163 5年前

    You could use a mix of functions, avoiding loops:

    $keys = array_flip(range(14,30));
    $result = array_intersect_key($entry, $keys);
    

    range() generates an array with the indices you want to compare against.
    array_flip() turns the so generated array values into array keys, since you want to intersect array keys.
    array_intersect_key does what you need basically.

    The above example could be written also as oneliner, but... you know readability and that ;)

    点赞 评论 复制链接分享
  • dongzhanlu8890 dongzhanlu8890 5年前

    I have seen answer above, you also can use array_filter()

    <?php
    for($i = 0; $i <30; $i++)
    {
      $array[$i] = $i;
    }
    $filtered = array_filter(
        $array,
        function ($key){
            return $key > 13;
        },
        ARRAY_FILTER_USE_KEY
    );
    var_dump($filtered);
    

    here is the demo

    点赞 评论 复制链接分享
  • dsa4214 dsa4214 5年前

    Yeah sure, using Array_Slice is probs the easiest solution here:

    $originalArray = $entry; //Example just to show what original array is
    $startOffset = 14;
    $newArray = array_slice($originalArray, $startOffset);
    

    You can also give it an ending offset (aka length)

    $newArray = array_slice($originalArray, $startOffset, 16);
    
    点赞 评论 复制链接分享
  • donglengyuan6826 donglengyuan6826 5年前

    You want to get $entry[14] through $entry[30]?

    A for loop in PHP can run between a range of numbers and could work:

    $newarray = array();
    for ($x = 14; $x <= 30; $x++) {
        //The number is $x;
        $newarray[] = $entry[$x];
    }
    
    点赞 评论 复制链接分享

为你推荐