PHP foreach döngüsü anahtar değeri

3 Cevap php

I am running this DB call to get me multi-dimensional array I am trying to get the keys of each but when I try it comes up blank or as array.

 $root_array = array();
        	$sites = $this->sites($member_id);
        	foreach ($sites as $site){
        		$records = $this->db->select('p.name as place_name, p.id as place_id,p.active as place_status')
        		->from('places p')
        		->join('members_permissions pm','pm.sites_id = p.sites_id and pm.members_id ='.$member_id)
        		->where('p.active', 0)
        		->get();

        		$places = $records->result_array();
        		$places['name'] = $site['name'];
        		foreach($places as $place){

        			$root_array[$site['name']][] = $place;
        		}

        	}
    	return $root_array;

Buraya döngüler benim php:

<?php foreach($places as $site): ?>
    <h5><?=key($site)?></h5>
        <?php foreach($site as $place): ?>
            <h6><?=$place['place_name']?></h6>
        <?php endforeach?>

<?php endforeach ?>

Also when I run a test which just spits out the array this is the result, What I am trying to render is [Philadelphia]

[Philadelphia] => Array
        (
            [0] => Array
                (
                    [place_name] => XYX
                    [place_id] => 103200
                    [place_status] => 0
                )

            [1] => Array
                (
                [place_name] => YYYY
                [place_id] => 232323
                [place_status] => 0
            )

3 Cevap

Bunu gibi dizi tuşları erişebilirsiniz:

foreach ($array as $key => $value)

Yukarıda belirtildiği gibi, Pekka

foreach ($array as $key => $value)

Ayrıca bir özyinelemeli işlev denemek isteyebilirsiniz

displayRecursiveResults($site);

function displayRecursiveResults($arrayObject) {
    foreach($arrayObject as $key=>$data) {
        if(is_array($data)) {
            displayRecursiveResults($data);
        } elseif(is_object($data)) {
            displayRecursiveResults($data);
        } else {
            echo "Key: ".$key." Data: ".$data."<br />";
        }
    }
}

Ayrıca kullanabileceğiniz array_keys(). Dostu acemi:

$keys = array_keys($values);
for($i=0;$i<count($values);$i++){
    echo "<option value=\"".$keys[$i]."\">".$values[$keys[$i]]."</option>";
}