0

In Laravel I can return data from Google analytics to get most visited page with this command:

$FilterData =$this->parseResults($data)->pluck('url');

It will be return this URLs:

 [
    "/products/r4-04",
    "/products/r6-01",
    "/products/cu3-20",
    "/products/r4-51",
    "/products/zp-1",
    "/products/r5-31",
    "/products/cu3-64",
    "/products/cu6-01-1",
    "/products/cu6-01-2",
    "/products/r4-14",
    "/products/t4-74",
    "/products/cu-001",
    "/products/cu5-18",
    "/products/zp-8",
    "/products/td6-01",
    "/products/t4-14",
    "/products/c6-01"
]

Now I want to remove all /products/ word from this and find the products by slug.

3
  • Does this answer your question? How to remove duplicate values from an array in PHP Commented Feb 26, 2020 at 9:36
  • 1
    no, cause i want to remove part of this, not duplicate records Commented Feb 26, 2020 at 9:42
  • I just noticed, but you used a misleading title, I updated your question. Commented Feb 26, 2020 at 9:43

3 Answers 3

1

If you need just remove /products/ from every array value, you can use str_replace for this:

$FilterData =$this->parseResults($data)->pluck('url');
$FilterDataNew = str_replace("/products/","",$FilterData);
var_dump($FilterDataNew);
Sign up to request clarification or add additional context in comments.

Comments

0
<?php
$products =  [
    "/products/r4-04",
    "/products/r6-01",
    "/products/cu3-20",
    "/products/r4-51",
    "/products/zp-1",
    "/products/r5-31",
    "/products/cu3-64",
    "/products/cu6-01-1",
    "/products/cu6-01-2",
    "/products/r4-14",
    "/products/t4-74",
    "/products/cu-001",
    "/products/cu5-18",
    "/products/zp-8",
    "/products/td6-01",
    "/products/t4-14",
    "/products/c6-01"
];
function replace($product) {
   return str_replace('/products/', '', $product);  
}
$products = array_map('replace', $products);

Comments

0

You can simple use str_replace to achieve the same.

Assuming your variable as $products

 $array =  str_replace('/products/', '', $products);
 dd($array);

1 Comment

No need array_map, only str_replace will work in this case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.