Loop on a variable delimited with gamma using Laravel 8

I have in my database values with this form :

["<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1a4aca0a8adf081a6aca0a8adefa2aeac">[email protected]</a>","<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b5e565a5257097b5c565a525715585456">[email protected]</a>"]

I’m using laravel and I want to loop on this variable to get each element for example get the element : [email protected].

I tried the following code :

I have the following array :

$emails :
    array:2 [▼
      0 => "["<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="06636b676f6a3746616b676f6a2865696b">[email protected]</a>","<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b5e565a5257097b5c565a525715585456">[email protected]</a>"]"
      1 => "["<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f59098949c99c6b59298949c99db969a98">[email protected]</a>","<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b6e666a62673f4b6c666a626725686466">[email protected]</a>"]"
    ]

So I’m using the following code to get each element :

$var = array();
          foreach ($emails as $key => $value) {
            $var[] = $value;
          }

I get the following result :

array:2 [▼
  0 => "["<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bdd8d0dcd4d18cfddad0dcd4d193ded2d0">[email protected]</a>","<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a4f474b4346186a4d474b434604494547">[email protected]</a>"]"
  1 => "["<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cda8a0aca4a1fe8daaa0aca4a1e3aea2a0">[email protected]</a>","<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef8a828e8683dbaf88828e8683c18c8082">[email protected]</a>"]"
]

If you have any idea , please help

UPDATE

I have the following array :

array:2 [▼
  0 => "["<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1a4aca0a8adf081a6aca0a8adefa2aeac">[email protected]</a>","<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3b6beb2babfe193b4beb2babffdb0bcbe">[email protected]</a>"]"
  1 => "["<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b434a414a590549445e4a474a46424a18186b4c464a424705484446">[email protected]</a>","<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="65021000170b0c0b004b0e0d04010c0f04250208040c094b060a08">[email protected]</a>"]"
]

And I did the following method :

$emailss = collect($emails)->flatten()->all();
 dd($emailss);

I get the following result :

array:2 [▼
  0 => "["<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef8a828e8683deaf88828e8683c18c8082">[email protected]</a>","<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="50353d31393c6210373d31393c7e333f3d">[email protected]</a>"]"
  1 => "["<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5ada4afa4b7eba7aab0a4a9a4a8aca4f6f685a2a8a4aca9eba6aaa8">[email protected]</a>","<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d0a18081f030403084306050c0904070c2d0a000c0401430e0200">[email protected]</a>"]"
]

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Update

Ha, tricky one. It seems that you have a PHP expression (an array) stored. So in order to extract the arrays, we need to evaluate them first.

Try this instead:

$elements = [
  "['[email protected]','[email protected]']",
  "['[email protected]','[email protected]']",
];

$emails = collect($elements)
  ->map(function ($element) {
    return (eval("return $element = " . $element . ';'));
  })
  ->flatten()
  ->all();

Try this:


$elements = [
  ['[email protected]','[email protected]'],
  ['[email protected]','[email protected]'],
];

$emails = collect($elements)->flatten()->all();

This will get you:

=> [
     "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53363e323a3f6213343e323a3f7d303c3e">[email protected]</a>",
     "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e3868e828a8fd1a3848e828a8fcd808c8e">[email protected]</a>",
     "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="791c141810154a391e14181015571a1614">[email protected]</a>",
     "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53363e323a3f6713343e323a3f7d303c3e">[email protected]</a>",
   ]

Check this method on the docs.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x