Skip to main content
Functional syntax
Source Link
mickmackusa
  • 4.8k
  • 4
  • 18
  • 48

Since your logos and urls exist in pairs, you can use a single loop over the logos and access the urls via the shared index.

Code: (Demo)

foreach ($jsonPartenaires['logo'] as $i => $logo) {
    $html = sprintf('<img src="%s">', $logo);
    if ($jsonPartenaires['url_partenaire'][$i]) {
        $html = sprintf(
            '<a href="%s">%s</a>',
            $jsonPartenaires['url_partenaire'][$i],
            $html
        );
    }
    echo $html . "<br>";
}

Output: (tabbed and spaced for readability)

<img src="images/website_logo_1.png">
<br>
<a href="https://www.url_website_2.fr">
    <img src="images/website_logo_2.png">
</a>
<br>
<img src="images/website_logo_3.png">
<br>

Using printf() or sprintf() (the silent version), enables you to bind variables to a string without the noise of string concatenation or interpolation. If you ever want to call a string function on a variable (to prepare the strings, you can just wrap the parameters containing variables in the desired function(s).


I'll admit that in my own project, I'd probably use functional syntax. This allows you to cleanly join the html elements with <br> with no trailing <br>.

Code: (Demo)

echo implode(
         "<br>",
         array_map(
             function($logo, $url) {
                 $html = sprintf('<img src="%s">', $logo);
                 if ($url) {
                     $html = sprintf('<a href="%s">%s</a>', $url, $html);
                 }
                 return $html;
             },
             $jsonPartenaires['logo'],
             $jsonPartenaires['url_partenaire']
         )
     );

Since your logos and urls exist in pairs, you can use a single loop over the logos and access the urls via the shared index.

Code: (Demo)

foreach ($jsonPartenaires['logo'] as $i => $logo) {
    $html = sprintf('<img src="%s">', $logo);
    if ($jsonPartenaires['url_partenaire'][$i]) {
        $html = sprintf(
            '<a href="%s">%s</a>',
            $jsonPartenaires['url_partenaire'][$i],
            $html
        );
    }
    echo $html . "<br>";
}

Output: (tabbed and spaced for readability)

<img src="images/website_logo_1.png">
<br>
<a href="https://www.url_website_2.fr">
    <img src="images/website_logo_2.png">
</a>
<br>
<img src="images/website_logo_3.png">
<br>

Using printf() or sprintf() (the silent version), enables you to bind variables to a string without the noise of string concatenation or interpolation. If you ever want to call a string function on a variable (to prepare the strings, you can just wrap the parameters containing variables in the desired function(s).

Since your logos and urls exist in pairs, you can use a single loop over the logos and access the urls via the shared index.

Code: (Demo)

foreach ($jsonPartenaires['logo'] as $i => $logo) {
    $html = sprintf('<img src="%s">', $logo);
    if ($jsonPartenaires['url_partenaire'][$i]) {
        $html = sprintf(
            '<a href="%s">%s</a>',
            $jsonPartenaires['url_partenaire'][$i],
            $html
        );
    }
    echo $html . "<br>";
}

Output: (tabbed and spaced for readability)

<img src="images/website_logo_1.png">
<br>
<a href="https://www.url_website_2.fr">
    <img src="images/website_logo_2.png">
</a>
<br>
<img src="images/website_logo_3.png">
<br>

Using printf() or sprintf() (the silent version), enables you to bind variables to a string without the noise of string concatenation or interpolation. If you ever want to call a string function on a variable (to prepare the strings, you can just wrap the parameters containing variables in the desired function(s).


I'll admit that in my own project, I'd probably use functional syntax. This allows you to cleanly join the html elements with <br> with no trailing <br>.

Code: (Demo)

echo implode(
         "<br>",
         array_map(
             function($logo, $url) {
                 $html = sprintf('<img src="%s">', $logo);
                 if ($url) {
                     $html = sprintf('<a href="%s">%s</a>', $url, $html);
                 }
                 return $html;
             },
             $jsonPartenaires['logo'],
             $jsonPartenaires['url_partenaire']
         )
     );
deleted 852 characters in body
Source Link
mickmackusa
  • 4.8k
  • 4
  • 18
  • 48

Since your logos and urls exist in pairs, you can use a single loop over the logos and access the urls via the shared index.

Code: (DemoDemo)

foreach ($jsonPartenaires['logo'] as $i => $logo) {
    printf$html = sprintf('<img src="%s">', $logo);
    if ($jsonPartenaires['url_partenaire'][$i]) {
  '<a href="%s"><img src="%s"><    $html = sprintf(
            '<a href="%s">%s</a><br>'a>',
        $logo    $jsonPartenaires['url_partenaire'][$i],
        $jsonPartenaires['url_partenaire'][$i]    $html
        );
    }
    echo $html . "<br>";
}

Output: (tabbed and spaced for readability)

<a<img href="imagessrc="images/website_logo_1.png"> 
    <img<br>
<a src="httpshref="https://www.url_website_1url_website_2.fr">
</a>
<br>
<a href="images/website_logo_2.png">
    <img src="https:/src="images/www.url_website_2website_logo_2.fr">png">
</a>
<br>
<a<img href="imagessrc="images/website_logo_3.png">
    <img src="https://www.url_website_3.fr">
</a>
<br>

Using printf() or sprintf() (the silent version), enables you to bind variables to a string without the noise of string concatenation or interpolation. If you ever want to call a string function on eithera variable (to prepare the strings, you can just wrap the 2nd or 3rd parameterparameters containing variables in the desired function(s).


Functional code styling might be too intimidating for developers who are just starting to learn php, but the benefit is being able to join the generated html strings with <br> -- avoiding the trailing <br>.

Code: (Demo) (or without any ...)

echo implode(
         '<br>',
         array_map(
             function(...$v) {
                 return sprintf(
                     '<a href="%s"><img src="%s"></a>',
                     ...$v
                 );
             },
             ...array_values($jsonPartenaires)
         )
    );

(The "spread/splat operator" is used to unpack multiple subarrays when passing data into the custom function, receiving the data in the custom function, and again unpacking the data into sprintf().)

Since your logos and urls exist in pairs, you can use a single loop over the logos and access the urls via the shared index.

Code: (Demo)

foreach ($jsonPartenaires['logo'] as $i => $logo) {
    printf(
        '<a href="%s"><img src="%s"></a><br>',
        $logo,
        $jsonPartenaires['url_partenaire'][$i]
    );
}

Output: (tabbed and spaced for readability)

<a href="images/website_logo_1.png"> 
    <img src="https://www.url_website_1.fr">
</a>
<br>
<a href="images/website_logo_2.png">
    <img src="https://www.url_website_2.fr">
</a>
<br>
<a href="images/website_logo_3.png">
    <img src="https://www.url_website_3.fr">
</a>
<br>

Using printf() or sprintf() (the silent version), enables you to bind variables to a string without the noise of string concatenation or interpolation. If you ever want to call a string function on either variable (to prepare the strings, you can just wrap the 2nd or 3rd parameter in the desired function(s).


Functional code styling might be too intimidating for developers who are just starting to learn php, but the benefit is being able to join the generated html strings with <br> -- avoiding the trailing <br>.

Code: (Demo) (or without any ...)

echo implode(
         '<br>',
         array_map(
             function(...$v) {
                 return sprintf(
                     '<a href="%s"><img src="%s"></a>',
                     ...$v
                 );
             },
             ...array_values($jsonPartenaires)
         )
    );

(The "spread/splat operator" is used to unpack multiple subarrays when passing data into the custom function, receiving the data in the custom function, and again unpacking the data into sprintf().)

Since your logos and urls exist in pairs, you can use a single loop over the logos and access the urls via the shared index.

Code: (Demo)

foreach ($jsonPartenaires['logo'] as $i => $logo) {
    $html = sprintf('<img src="%s">', $logo);
    if ($jsonPartenaires['url_partenaire'][$i]) {
        $html = sprintf(
            '<a href="%s">%s</a>',
            $jsonPartenaires['url_partenaire'][$i],
            $html
        );
    }
    echo $html . "<br>";
}

Output: (tabbed and spaced for readability)

<img src="images/website_logo_1.png">
<br>
<a href="https://www.url_website_2.fr">
    <img src="images/website_logo_2.png">
</a>
<br>
<img src="images/website_logo_3.png">
<br>

Using printf() or sprintf() (the silent version), enables you to bind variables to a string without the noise of string concatenation or interpolation. If you ever want to call a string function on a variable (to prepare the strings, you can just wrap the parameters containing variables in the desired function(s).

added 840 characters in body
Source Link
mickmackusa
  • 4.8k
  • 4
  • 18
  • 48

Since your logos and urls exist in pairs, you can use a single loop over the logos and access the urls via the shared index.

Code: (Demo)

foreach ($jsonPartenaires['logo'] as $i => $logo) {
    printf(
        '<a href="%s"><img src="%s"></a><br>',
        $logo,
        $jsonPartenaires['url_partenaire'][$i]
    );
}

Output: (tabbed and spaced for readability)

<a href="images/website_logo_1.png"> 
    <img src="https://www.url_website_1.fr">
</a>
<br>
<a href="images/website_logo_2.png">
    <img src="https://www.url_website_2.fr">
</a>
<br>
<a href="images/website_logo_3.png">
    <img src="https://www.url_website_3.fr">
</a>
<br>

Using printf() or sprintf() (the silent version), enables you to bind variables to a string without the noise of string concatenation or interpolation. If you ever want to call a string function on either variable (to prepare the strings, you can just wrap the 2nd or 3rd parameter in the desired function(s).


Functional code styling might be too intimidating for developers who are just starting to learn php, but the benefit is being able to join the generated html strings with <br> -- avoiding the trailing <br>.

Code: (Demo) (or without any ...)

echo implode(
         '<br>',
         array_map(
             function(...$v) {
                 return sprintf(
                     '<a href="%s"><img src="%s"></a>',
                     ...$v
                 );
             },
             ...array_values($jsonPartenaires)
         )
    );

(The "spread/splat operator" is used to unpack multiple subarrays when passing data into the custom function, receiving the data in the custom function, and again unpacking the data into sprintf().)

Since your logos and urls exist in pairs, you can use a single loop over the logos and access the urls via the shared index.

Code: (Demo)

foreach ($jsonPartenaires['logo'] as $i => $logo) {
    printf(
        '<a href="%s"><img src="%s"></a><br>',
        $logo,
        $jsonPartenaires['url_partenaire'][$i]
    );
}

Output: (tabbed and spaced for readability)

<a href="images/website_logo_1.png"> 
    <img src="https://www.url_website_1.fr">
</a>
<br>
<a href="images/website_logo_2.png">
    <img src="https://www.url_website_2.fr">
</a>
<br>
<a href="images/website_logo_3.png">
    <img src="https://www.url_website_3.fr">
</a>
<br>

Using printf() or sprintf() (the silent version), enables you to bind variables to a string without the noise of string concatenation or interpolation. If you ever want to call a string function on either variable (to prepare the strings, you can just wrap the 2nd or 3rd parameter in the desired function(s).

Since your logos and urls exist in pairs, you can use a single loop over the logos and access the urls via the shared index.

Code: (Demo)

foreach ($jsonPartenaires['logo'] as $i => $logo) {
    printf(
        '<a href="%s"><img src="%s"></a><br>',
        $logo,
        $jsonPartenaires['url_partenaire'][$i]
    );
}

Output: (tabbed and spaced for readability)

<a href="images/website_logo_1.png"> 
    <img src="https://www.url_website_1.fr">
</a>
<br>
<a href="images/website_logo_2.png">
    <img src="https://www.url_website_2.fr">
</a>
<br>
<a href="images/website_logo_3.png">
    <img src="https://www.url_website_3.fr">
</a>
<br>

Using printf() or sprintf() (the silent version), enables you to bind variables to a string without the noise of string concatenation or interpolation. If you ever want to call a string function on either variable (to prepare the strings, you can just wrap the 2nd or 3rd parameter in the desired function(s).


Functional code styling might be too intimidating for developers who are just starting to learn php, but the benefit is being able to join the generated html strings with <br> -- avoiding the trailing <br>.

Code: (Demo) (or without any ...)

echo implode(
         '<br>',
         array_map(
             function(...$v) {
                 return sprintf(
                     '<a href="%s"><img src="%s"></a>',
                     ...$v
                 );
             },
             ...array_values($jsonPartenaires)
         )
    );

(The "spread/splat operator" is used to unpack multiple subarrays when passing data into the custom function, receiving the data in the custom function, and again unpacking the data into sprintf().)

Source Link
mickmackusa
  • 4.8k
  • 4
  • 18
  • 48
Loading