skip to Main Content

Supposing I have this php array:


$booklist =

Array
(
    [0] => Peppa / Season 2 / 43 The Quarrel / Page 1
    [1] => Nursery Rhymes / Wheels On The Bus / Page 1
    [2] => Wonderskills / Starter Book 2 / Unit 1 At The Firehouse / Part 3 / Page 3
    [3] => Oxford Phonics World / Level 2 Short Vowels / Unit 1 Short A / Page 7
    [4] => Peppa / Season 2 / 43 The Quarrel / Page 1
    [5] => Nursery Rhymes / Twinkle Twinkle / Page 1
    [6] => Wonderskills / Starter Book 2 / Unit 1 At The Firehouse / Part 2 / Page 17
    [7] => Oxford Phonics World / Level 1 The Alphabet / Unit 8 / Review W X Y Z / Page 2
    [8] => Peppa / Season 2 / 42 Granny And Grandpas Attic / Page 1
    [9] => Nursery Rhymes / The Phonics Song / Page 1
    [10] => Wonderskills / Starter Book 2 / Unit 1 At The Firehouse / Part 2 / Page 4
    [11] => Peter Pan / Level 1 / Page 1
    [12] => Peppa / Season 2 / 42 Granny And Grandpas Attic / Page 1
    [13] => Nursery Rhymes / The Phonics Song / Page 1
    [14] => Donald Duck / Page 19
    [15] => Donald Duck / Page 18
    [16] => Oxford Phonics World / Level 1 The Alphabet / Unit 8 / Letter Z / Page 2
)

I would like to through each item and add it to a new array IF the new array does not already contain an item with the initial prefix (the first ‘/’ being the delimiter).

The final array would therefore only contain these values:


$**finalArray **=

Array
(
    [0] => Peppa / Season 2 / 43 The Quarrel / Page 1
    [1] => Nursery Rhymes / Wheels On The Bus / Page 1
    [2] => Wonderskills / Starter Book 2 / Unit 1 At The Firehouse / Part 3 / Page 3
    [3] => Oxford Phonics World / Level 2 Short Vowels / Unit 1 Short A / Page 7
    [4] => Peter Pan / Level 1 / Page 1
    [5] => Donald Duck / Page 19

)

Many thanks in advance.

I have tried many combinations of things such as strpos() and str_starts_with() with array_push(), but I just can’t seem to get it working. I either end up with the same array, an empty array, or an array with tens of thousands of items.

$finalArray = [];

foreach ($booklist as $book){
    
            foreach($finalArray as $finalItem){
                
                $array = explode(" / ", $book);
                
                $mainBranch = $array[0];

                   if(0 === strpos($finalItem, $mainBranch){ /// problems in here
                     
                       array_push($finalArray, $book);

          }
    }
}

2

Answers


  1. You can directly use array keys for filtering:

    if(!isset($finalArray[$mainBranch])) {
        $finalArray[$mainBranch] = $book;
    }
    

    If you really need the final array to be an indexed array, use array_values to convert it:

    $finalArray = array_values($finalArray);
    

    BTW, if you only need to take the first word before the slash, you can use strstr to avoid creating arrays:

    $mainBranch = strstr($book, '/', true);
    
    Login or Signup to reply.
  2. Store only the rows which include the first of a unique prefix. Determine "duplicateness" by using temporary keys in the result array. After looping, remove the temporarykeys with array_values().

    Code: (Demo)

    $result = [];
    foreach ($booklist as $book) {
        $result[strtok($book, '/')] ??= $book;
    }
    var_export(array_values($result));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search