1
function do_get_array() {

    $posts[] = array(
        'name'          => 'Page',
        'icon'          => 'fa fa-users',
        'fields'        => array(
            array(
                'field_name'       => 'Publisher Logo',
                'id'               => 'pub_logo',
                'type'             => 'text',
            ),
        ),
    );

    $posts[] = array(
        'name'          => 'Slideshow',
        'icon'          => 'fa fa-users',
        'fields'        => array(
            array(
                'field_name'       => 'Publisher Logo2',
                'id'               => 'pub_logob',
                'type'             => 'text',
            ),
        ),
    );
return $posts;

} 

getting arrays from above function.

$posts = do_get_array();
$return = array();
foreach ($posts as $post) {
    $return['name'] = $post['name'];
    $return['icon'] = $post['icon'];

}
print_r($return);

but it only returns second array. I want it to return all the arrays value with key that i provided. I am actually new to php that's why i think i have some confusion.

4
  • You are overwriting your values in the loop: $return['name'] = .... Why do you need a new array? Commented Mar 16, 2015 at 16:46
  • foreach ($posts as $post) { $return[] = ['name' => $post['name'], 'icon' => $post['icon']]; } Commented Mar 16, 2015 at 16:48
  • but I want it to return with the key. like this. Array ( [name] => Page [icon] => fa fa-users ) Array ( [name] => Slideshow [icon] => fa fa-users ) Commented Mar 16, 2015 at 16:48
  • Should be $return[] = ['name' => $post['name'], 'icon' => $post['icon']]; and $posts should be declared somewhere as an (empty?) array... Commented Mar 16, 2015 at 16:50

2 Answers 2

1

You are overwriting each time. Build an array of those arrays:

foreach ($posts as $post) 
    $return[] = array('name' => $post['name'], 'icon' => $post['icon']);
}

However this seems like a waste, you could just use the $posts array as is, it just has some extra information.

If you actually want the name as the array index, then:

$return[$post['name']] = $post['icon'];
Sign up to request clarification or add additional context in comments.

Comments

0

Because in your function the second $posts[] override the first one.

try this to merge the tow $posts[]:

<?php
function do_get_array() {
$posts1[] = array(
        'name'          => 'Page',
        'icon'          => 'fa fa-users',
        'fields'        => array(
                                array(
                                    'field_name'       => 'Publisher Logo',
                                    'id'               => 'pub_logo',
                                    'type'             => 'text',
                ),
        ),
);

$posts2[] = array(
        'name'          => 'Slideshow',
        'icon'          => 'fa fa-users',
        'fields'        => array(
                array(
                        'field_name'       => 'Publisher Logo2',
                        'id'               => 'pub_logob',
                        'type'             => 'text',
                ),
        ),
);

$result = array_merge($posts1, $posts2);
return $result;
}

$posts = do_get_array();
$return = array();
for($i=0; $i<count($posts); $i++){
    $return[$i]['name'] = $posts[$i]['name'];
    $return[$i]['icon'] = $posts[$i]['icon'];
}

print_r($return);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.