need help with php-templates array loops

Discussion in 'PHP' started by whofarted, May 28, 2020.

  1. #1
    Can anybody help me out with this?

    I'm trying to loop through arrays to put into a template and show multiple posts on a page using the template. But I cannot get this to work and I don't know why. I'm very new to OOP and don't fully understand it. This seems generic and it should be easy to do but I keep having problems.

    I can get it to work if I type the arrays in manually, but that doesn't help me if I pull data from a database and loop through it.

    here's what i'm talking about: from this class: https://github.com/isRuslan/php-template

    I can't get this part to work in the template
    
    $template->assign('items', array(
    array('name' => 'First'),
    array('name' => 'Second')
    ));
    
    PHP:
    I'm trying to use this to display arrays pulled from the database with now luck.
    
    $names = array('test1', 'test2', 'test3');
    $names2 = array('test2 1', 'test2 2', 'test2 3');
    
    
    $template->assign('people', array(
          array('name' => $names),
          array('name2' => $names2)
    ));
    
    
    PHP:
    any help....please. :)
     
    whofarted, May 28, 2020 IP
  2. whofarted

    whofarted Greenhorn

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    nevermind, someone helped me get there.

    anyone interested:
    $names = array('Bob', 'Ralph', 'Mike');
    $names2 = array('Col.', 'Gen.', 'Adm.');
    $i = 0;
    
    $template_array = array();
    
    while($i < 3)
    {
        $template_array[] = array('NAME' => $names[$i], 'RANK' => $names2[$i]);
        $i++;
    }
    
    
    
    $template->assign('people', $template_array);
    PHP:
    :cool:
     
    whofarted, May 29, 2020 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Overthinking it. One thing to remember with PHP is "yeah, we've got a function for that". In this case, "array_map" is likely what you should be doing.

    Also this is 2020 not 2005, you can stop saying "array" all the blasted time.

    
    $names = [ 'Bob', 'Ralph', 'Mike' ];
    $ranks = [ 'Col.', 'Gen.', 'Adm.' ];
    
    function nameRankPair($a, $b) {
    	return [ 
    		'NAME' => $a,
    		'RANK' => $b
    	];
    }
    
    $template_array = array_map('nameRankPair', $names, $ranks);
    $template->assign('people', $template_array);
    
    Code (markup):
    You might even consider ditching the "variable for nothing" of the last two lines and simply:

    
    $template->assign('people', array_map('nameRankPair', $names, $ranks));
    
    Code (markup):
    If you have no plans to re-use the array again later in your page.

    It may even be worth using an anonymous function.

    
    <?php
    
    $names = [ 'Bob', 'Ralph', 'Mike' ];
    $ranks = [ 'Col.', 'Gen.', 'Adm.' ];
    
    $template->assign('people', array_map(function($a, $b) {
    	return [ 
    		'NAME' => $a,
    		'RANK' => $b
    	];
    }, $names, $ranks));
    
    Code (markup):
    Though I dislike that due to a lack of code clarity, this too is a valid approach.

    The only advantage the "while" approach brings is that it MIGHT run faster since the overhead of function calls isn't involved, but really you'd be better off using a FOREACH if that were the case and speed optimization was important.

    
    $names = ['Bob', 'Ralph', 'Mike'];
    $ranks = ['Col.', 'Gen.', 'Adm.'];
    
    $template_array = [];
    
    foreach ($names as $index => $name) $template_array[] = [
    	'NAME' => $name,
    	'RANK' => $ranks[$index]
    ];
    
    $template->assign('people', $template_array);
    Code (markup):
    Would probably be the fastest running of the various approaches one could take.
     
    deathshadow, Jun 8, 2020 IP