5 Replies - 1266 Views - Last Post: 06 December 2010 - 01:31 PM Rate Topic: -----

#1 Alhazred  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 174
  • Joined: 25-July 07

How to read only a part of a multidimensional array

Posted 06 December 2010 - 11:44 AM

I've got a multidimensional array with this structure
Array
(
    [Server] => Array
        (
            [ServerPLR] => DedicatedServer
            [LobbyName] => IROn3
            [ModName] => IROn3
            [Aids] => (0,0,0,0,0,0,0,0,1,0,0)
        )
    [Session] => Array
        (
            [Scene] => Trophy of the Dunes
            [TrackAIW] => ZandGP.AIW
            [TrackLength] => 4278.1221
        )
    [Slot000] => Array
        (
            [BestLap] => 111.255
            [LastLap] => 112.180
            [BestSectors] => (32.742, 46.991, 31.119)
            [LastLapSectors] => (33.017, 47.528, 31.635)
            [Sectors] => (33.017, 47.528, 31.635)
        )
    [Slot001] => Array
        (
            [BestLap] => 109.911
            [LastLap] => 111.371
            [BestSectors] => (32.390, 46.450, 30.842)
            [LastLapSectors] => (32.789, 47.425, 31.157)
            [Sectors] => (32.789, 47.425, 31.157)
        )
    ........
)


I need to read the data inside the [Slot000], [Slot001]... array, but I don't know in advance how many of those array are contained inside the main array.
How can I cicle through all of them?

This post has been edited by Alhazred: 06 December 2010 - 11:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to read only a part of a multidimensional array

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: How to read only a part of a multidimensional array

Posted 06 December 2010 - 12:00 PM

Will it always be less than 1000? Check this out:

<?php

for ($i = 1; $i < 1000; ++$i)
{
    $index = sprintf("Slot%03d", $i);
    echo ("$index\n");
}
?>


Was This Post Helpful? 0
  • +
  • -

#3 Alhazred  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 174
  • Joined: 25-July 07

Re: How to read only a part of a multidimensional array

Posted 06 December 2010 - 12:33 PM

View PostJackOfAllTrades, on 06 December 2010 - 11:00 AM, said:

Will it always be less than 1000? Check this out:

<?php

for ($i = 1; $i < 1000; ++$i)
{
    $index = sprintf("Slot%03d", $i);
    echo ("$index\n");
}
?>


This is not what I need, I have to read the data inside those array, not their name, in example I have to read [bestlap] value from array [Slot000], from array [Slot001] and so on.
I have to do that with a cicle, but I don't know how because I don't know how many of them I have to read.
Was This Post Helpful? 0
  • +
  • -

#4 Alhazred  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 174
  • Joined: 25-July 07

Re: How to read only a part of a multidimensional array

Posted 06 December 2010 - 12:43 PM

Ok, I've found the way to don't cycle through not set Slot modifying your code.
Thanks.

This post has been edited by Alhazred: 06 December 2010 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: How to read only a part of a multidimensional array

Posted 06 December 2010 - 12:50 PM

Guess taking a hint and doing some thinking about it isn't your strong point, and you require spoon-feeding? You're lucky I'm feeling generous.

<?php

$spoonfeedme = array(
    'Slot000' => array(
        'BestLap' => 111.255,
        'LastLap' => 112.180),
    'Slot001' => array(
        'BestLap' => 109.911,
        'LastLap' => 111.371)
);

for ($i = 0; $i < 1000; ++$i)
{
    $index = sprintf("Slot%03d", $i);
    if (isset($spoonfeedme[$index]))
        echo "$index: {$spoonfeedme[$index]['BestLap']} {$spoonfeedme[$index]['
LastLap']}\n";
}

?>


Was This Post Helpful? 0
  • +
  • -

#6 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: How to read only a part of a multidimensional array

Posted 06 December 2010 - 01:31 PM

Use a foreach($key => $value) control and check $key for the presence of the string "Slot", if it's found then do whatever you need to do with $value.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1