Jump to content

Question about array's


eldan88

Recommended Posts

Hey,

 

 I am trying to figure out how I can pull an array that is within an array. I have trying doing that but not getting any luck.

 

Below is an example code that I am trying to work with, but its only returning the letter "G" Any suggestions? 

$name_array = array("dog",array("cat"));
echo $name_array[0][2];

Link to comment
https://forums.phpfreaks.com/topic/278186-question-about-arrays/
Share on other sites


<?php
$name_array_original = array (
                          0 => array('animal' => 'dog'),
                      1 => array('animal' => 'cat'),
                      2 => array('animal' => 'tiger')
                   );

$name_array = array (
                       0 => array('animal' => 'dog' , 'name' => 'Snoopy'),
                   1 => array('animal' => 'cat', 'name' => 'Garfield'),
                   2 => array('animal' => 'tiger', 'name' => 'Tony')
                 );
// Name sorting function:
function name_sort($x, $y) {
    return strcasecmp($x['name'], $y['name']);
}

echo '<h2>Original Array</h2><pre>' . print_r($name_array_original, 1) . '</pre>';
uasort($name_array, 'name_sort');
echo '<h2>Array Sorted By Name</h2><pre>' . print_r($name_array, 1) . '</pre>';

foreach ($name_array_original as $original) {
    foreach ($original as $key => $value) {
        echo '<p>Key = ' . $key . '<br>Value = ' . $value . '</p>';
    }
}



 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.