We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11316
    • 81 Posts
    Hopefully someone can whip this newbie php’er into shape.

    I am trying to echo a foreach on an array inside an array but I am hitting a wall.

    The code
    <?php
    $parentid = 315;
    $tvidnames=gmapLatitude;
    $myArray = $modx->getDocumentChildrenTVarOutput($parentid, $tvidnames);
    
    print_r($myArray);
    
    foreach ( $myArray as $key => $value){
    	echo "<br /><br />Docid: $key $value <br />";
    }
    ?>


    Which prints out
    Array ( [316] => Array ( [gmapLatitude] => 0.0 ) [317] => Array ( [gmapLatitude] => 0.0 ) [318] => Array ( [gmapLatitude] => 0.0 ) )
    
    Docid: 316 (I want 0.0 instead and no Array at the end) Array
    Would look like: Docid: 0.0
    
    Docid: 317 Array
    
    Docid: 318 Array 

      • 34084 ☆ A M B ☆
      • 756 Posts
      change this:
      foreach ( $myArray as $key => $value){
      	echo "<br /><br />Docid: $key $value <br />";
      }
      


      to this:
      foreach ( $myArray as $doc => $m_value){
      	foreach ($m_value as $x_key => $x_value)
      	echo '<br /><br />Docid ('.$doc.'): '.$x_key.' '.$x_value.' <br />';
      }
      


      good luck

      cheers
        Evo Revo // Ubuntu, CentOS, Win // Apache 2x, Lighttp (Lighty)
        Visit CharlesMx.com for latest news and status updates.
        • 11316
        • 81 Posts
        worked like a charm!

        I have sort of switched gears at this point. Up for another one?? smiley
          • 34084 ☆ A M B ☆
          • 756 Posts
          update my previous code post to add in the missing ")"... smiley

          what the new ?
            Evo Revo // Ubuntu, CentOS, Win // Apache 2x, Lighttp (Lighty)
            Visit CharlesMx.com for latest news and status updates.
            • 11316
            • 81 Posts
            How could I use this on going down the tree?

            This snippet works great on individual pages, I didn’t create it myself that goes to the wonderful modx developers reply in a forum.

            Current Page
            I can use this [[CustomSnippet]] which is great or any other place directly related to the specific page

            <?php
            $output = '';
            $myArray = $modx->getTemplateVarOutput(array('tv1','tv2'));
            	if(empty($myArray['tv1'])){
            	} else {
            	$output .= '<h3>'.$myArray['tv1'].'</h3>';
            	}
            	if(empty($myArray['tv2'])){
            	} else {
            	$output .= '<h3>'.$myArray['tv2'].'</h3>';
            	}
            return $output;
            ?>
            

            The challenge.

            Trying to use it one above or two above.
            Parent Page using Ditto doesn’t appropriately associate the tv variable with the correct document ID.

            Resource container holding documents using Ditto

            Ditto $tpl=`ChunkName` &parents=`32`

            ChunkName = {{ [+pagetitle+] [[SnippetBelow]] }}
            <?php
            $output = '';
            $parent = $modx->documentObject[id];
            $tvidnames=(array('tv1','tv2'));
            
            $myArray = $modx->getDocumentChildrenTVarOutput($parent, $tvidnames);
            
            $new=array_merge(array(),$myArray); // Used this because I couldnt find my around it show the Document ID = [id] of specific child id.
            
            	if(empty($new[0][tv1])){
            	} else {
            	$output .= '<h3>'.$myArray['tv1'].'</h3>';
            	}
            	if(empty($new[0][tv2])){
            	} else {
            	$output .= '<h3>'.$myArray['tv2'].'</h3>';
            	}
            return $output;
            ?>
            


            If I have totally lost you, then you are already to kind to indulge me! SO thank you in advance