-1

i need to define the array values as need. Here i say that the value will be 0 to 2. But i need someway to say the value can be null and also it can be 0 to 1000.

$apartment = array(
                0,
                1,
                2
            );

foreach ($apartment AS $apt) {

                $userApt = $area->getApartments()->get($apt)->getApartment();
                echo $userApt . "<br>";
}

Please note that the value can be 0 and it should stop where there is no available value ...

i mean if get(0) is available it should get the value, if get(1) is not available it should stop there and do nothing , so the main purpose is to get the value where it is available, when it is not available, do nothing...

Istiak Mahmood
  • 2,330
  • 8
  • 31
  • 73
  • 3
    So what's preventing you from building your array as such? – D4V1D Jul 29 '15 at 07:36
  • i just do not know how to build that array – Istiak Mahmood Jul 29 '15 at 07:41
  • What do you mean by "*it should stop where there is no available value*". What is *it*? What do you mean by *stop*? *Stop* what? – D4V1D Jul 29 '15 at 08:04
  • i mean $userApt = $area->getApartments()->get(0)->getApartment(); ......if it is available it should take the value if not than do nothing , so the main purpuse is to get the value where it is availabe, when it is not available, do nothing... – Istiak Mahmood Jul 29 '15 at 08:09

1 Answers1

1

According to your lasts edits, there is no need to build such an array. You can get your objects directly within the loop.

<?php
for($i = 0; $i <= 1000; ++$i) {
    $userApt = $area->getApartments()->get($i);
    if(!$userApt) {
        break;
    } else {
        var_dump($userApt->getApartment());
    }
}

This would stop as soon as an object cannot be retrieved.

D4V1D
  • 5,805
  • 3
  • 30
  • 65
  • Thanks for your answer, but i need something where it stop looping if there is no available data , i mean if get(0) is avalable it should get the value, if get(1) is not available it should stop there ... – Istiak Mahmood Jul 29 '15 at 08:06
  • @ChristoferHansen: Just edited my answer accordingly. – D4V1D Jul 29 '15 at 08:09
  • thanks for your good answer, but the problem is ---- where the value is not available it returns an error like so ---Error: Call to a member function getApartment() on a non-object, it returns like ---Array ( [0] => myhello.com ) Array ( [0] => myhello.com [1] => myhello.com_mobile ) Array ( [0] => myhello.com [1] => myhello.com_mobile [2] => hello.com ) .... than the error message – Istiak Mahmood Jul 29 '15 at 08:17
  • What the `get()` method returns in case of it fails retrieving the object? `false`? `null`? – D4V1D Jul 29 '15 at 08:19
  • i am working on someone else code but i guess it will be "false" – Istiak Mahmood Jul 29 '15 at 08:55
  • So this code shouldn't raise the error as the loop breaks when `$userApt` is `false` (or equivalent). Are you sure you're using it properly? – D4V1D Jul 29 '15 at 08:56
  • i am sure i am using it properly, but i am guessing get() method is set to something so when it null or false it return this error ... it's really starnge – Istiak Mahmood Jul 29 '15 at 10:06