PHP expert nisaidie

[PHP]Array
(
[0] => Array
(
[id] => 1
[arraykey] => good
)

[1] => Array
    (
        [id] => 2
        [arraykey] => great
    )

[2] => Array
    (
        [id] => 3
        [arraykey] => best
    )

)[/PHP]

[PHP]Array
(
[0] => Array
(
[value] => 1
[name] => good
)

[1] => Array
    (
        [value] => 2
        [name] => great
    )

[2] => Array
    (
        [id] => 3
        [name] => best
    )

)[/PHP]

nimegoogle sielewi sana…nataka when [name] from 2nd array matches [arraykey] from 1st array niweze kuecho stuff fulani

nisaidiwe io code ya kumatch multidimensional:(

Asubuhi mapema. Depends on moods na hangz.

does … y=mx + c, help?
;):wink:

Loops?
[PHP]$arr_1 =
[
[’ id’ => 1, ‘arraykey’ => ‘good’ ],
[ ‘id’ => 2, ‘arraykey’ => ‘great’],
[ ‘id’ => 3, ‘arraykey’ => ‘best’]
];

$arr_2 =
[
[ ‘value’ => 1, ‘name’ => ‘good’],
[ ‘value’ => 2, ‘name’ => ‘great’],
[ ‘value’ => 3, ‘name’ => ‘best’]
];

// Loopy
foreach($arr_2 as $set_2) {
foreach ($arr_1 as $set_1) {
if ( $set_2[‘name’] == $set_1[‘arraykey’] ) {
echo sprintf(‘STUFF: %s == %s
’, $set_2[‘name’], $set_1[‘arraykey’] ); // ?
}
}
}[/PHP]

Provide some context. I have a feeling there’s a much better way like creating a dictionary of some sort.

:eek::smiley:

@Wakanyama usaidizi wako unahitajika apa

nifanye nini,nikukamue?

@uwesmake hajakukamua vyakutosha:rolleyes:

Nice stream line code bro…

this is what am trying to do… i have images in a file with a code looping to display them.but pia nataka text overlay on the images

[PHP]$images =
[
[’ id’ => 1, ‘arraykey’ => ‘lake.jpg’ ],
[ ‘id’ => 2, ‘arraykey’ => ‘boat.jpg’],
[ ‘id’ => 3, ‘arraykey’ => ‘beach.jpg’]
];

$resultarray =
[
[ ‘value’ => 1, ‘title’ => ‘Lake’ ‘name’ => ‘lake.jpg’],
[ ‘value’ => 2, ‘title’ => ‘Boat’ ‘name’ => ‘boat.jpg’],
[ ‘value’ => 3, ‘title’ => ‘Beach’ ‘name’ => ‘beach.jpg’]
];
[/PHP]

so what i wanted is when the arraykey in $images matches name in $resultarray, the title in $resultarray is displayed as a text overlay …io code umenipea is working good but each image iko na 3 text overlays Lake,Boat,Beach is it possible kuifanya zimatch such that kuna one overlay.

the current code am using inafanana ivi …is is possible ama ?:D:D will appreciate sana
[PHP]<?php foreach ($images as $image): ?>



  •                         <img  alt="image" class="background-image" src="<?php echo $image['full']; ?>" />
                            
                              
    
                            
    
                                <?php
    
                                foreach($images as $set_2) {
                                     foreach ($resultarray as $set_1) {
                                          if ( $set_2['name'] == $set_1['arraykey'] ) {
                                        
                                     $match = $set_1['name'];
    
                                   }
                                }
                             ?>                            
                    
    
                             </div>
    
                            </a>
                        </li>
                                    <?php endforeach; ?>[/PHP]
    
  • Ok. Will check in the evening.

    Looking at your code. You put the loop in the wrong place. You should have replaced the entire loop. Anyways…
    [PHP]/**

    • I presume this is the array that holds your images.
      */
      $images =
      [
      [’ id’ => 1, ‘arraykey’ => ‘lake.jpg’ ],
      [ ‘id’ => 2, ‘arraykey’ => ‘boat.jpg’ ],
      [ ‘id’ => 3, ‘arraykey’ => ‘beach.jpg’ ]
      ];

    $resultarray =
    [
    [ ‘value’ => 1, ‘title’ => ‘Lake’, ‘name’ => ‘lake.jpg’ ],
    [ ‘value’ => 2, ‘title’ => ‘Boat’, ‘name’ => ‘boat.jpg’ ],
    [ ‘value’ => 3, ‘title’ => ‘Beach’, ‘name’ => ‘beach.jpg’ ]
    ];

    $images_dictionary = [];
    foreach ($resultarray as $image_info) {
    $images_dictionary[ $image_info[‘name’] ] = $image_info[‘title’];
    }

    foreach ($images as $image): ?>

  • <?php /* <-- HTML error: Wrapping a block element with an inline element. */ ?>
    image <?php echo isset( $images_dictionary[ $image['arraykey'] ] ) ? $images_dictionary[ $image['arraykey'] ] : ''; ?>
  • <?php endforeach;[/PHP]

    [PHP]

    <?php // Indexed array of all images $images = array('lake.jpg', 'boat.jpg', 'beach.jpg'); // Associative array for image titles $result_array = array( 'Lake' => 'lake.jpg', 'Beach' => 'beach.jpg', 'Boat' => 'boat.jpg', ); foreach ($images as $url) { if (in_array($url, $result_array)) { // If image URL exists in result_array $title = array_search($url, $result_array); // Get the title of the matching element $src = "< img src = '/images/" . $url . "' alt = '" . $title . "' / >"; echo $src . "
    "; } }[/PHP]

    Yap Yap. Looks good… @Nattydread kuja hapa… 1…2…3tokambio

    It can’t hurt to see another slightly different approach. Post the code below in phptester.net.

    [php]
    $images =
    [
    [’ id’ => 1, ‘arraykey’ => ‘lake.jpg’ ],
    [ ‘id’ => 2, ‘arraykey’ => ‘boat.jpg’],
    [ ‘id’ => 3, ‘arraykey’ => ‘beach.jpg’]
    ];

    $resultarray =
    [
    [ ‘value’ => 1, ‘title’ => ‘Lake’, ‘name’ => ‘lake.jpg’],
    [ ‘value’ => 2, ‘title’ => ‘Boat’, ‘name’ => ‘boat.jpg’],
    [ ‘value’ => 3, ‘title’ => ‘Beach’, ‘name’ => ‘beach.jpg’]
    ];

    foreach($images as $img) {

    // $res is the value you want from $resultarray. 
    // It uses a filter relying on a closure to simplify
    // access to the current $images value
    $res = array_filter($resultarray, function($k) use ($img) {
      
        return $k['name'] == $img['arraykey'];      
    });
    
    // You now have your $resultarray mapped to $images
    // through name and arraykey.  Your markup will fit somewhere
    // in here.
    $out = "title=" . array_values($res)[0]['title'] . " image=" . $img['arraykey'] . "<p>";
    echo $out;
    

    }
    [/php]

    Pos€.Don,

    NB. Not to seem like a knock on your approach. But in the future you want to avoid(or minimize) mixing your markup and server code as much as possible for cleaner more maintainable code.

    Ideally, you get your result(in this case matched $images and $resultarrays in one final array), push it as JSON to the web-page. Use a web UI component to generate the display