-1

I've found an almost similar problem on this topic Loop Multidimensional PHP arrays My array is a little different but almost similar:

Array  
    (   
        [size] => int (995)
        [data] => Array
            (
                [0] => Array
                    (
                        [service] => 8000
                        [network] => xxx.xxx.xxx
                    )
            
                [1] => Array
                    (
                        [service] => 9000
                        [network] => xxx.xxx.xxx
                    )
                [2] => Array
                    (
                        [service] => 9500
                        [network] => xxx.xxx.xxx
                    )
            )    
    )

I d like to check all the service values in order to see if the number entered by the user is valid and exists, and display the corresponding network

Here is my naïve try:

$record = NULL;

// let's assume $x as this array here
foreach($record in $x['data']){
    if($record['service'] == $bus){
        break;
    }
}

if($record){
    // record found
    var_dump($record);
}else{
    echo "Not found";
}
john_johnk
  • 51
  • 8
  • Before using `foreach` loop, you should check the doc https://www.php.net/manual/en/control-structures.foreach.php – Nik Apr 01 '19 at 20:45
  • Thanks a lot Nick. I just Don't understand why in the link I posted, he needs tto use 3 imbricated foreach loop and I need to use only one ? – john_johnk Apr 01 '19 at 21:19
  • In the question you posted the user wanted to print all the items, subitems and subsubitems. In your case you need to iterate over only one level `$x['data']` and print an item if it fits. – Nik Apr 01 '19 at 21:24
  • Did you give up? – AbraCadaver Apr 02 '19 at 19:23
  • Of course not but I don't understand why I had a negative vote on my post. I really took time to write it most clear as possible – john_johnk Apr 04 '19 at 08:01
  • I don't know why either. I was just trying to see if an answer helped you then mark it Accepted or if you have issues or other questions post them. – AbraCadaver Apr 04 '19 at 21:24
  • He just forgot to mark my answer. Lol @AbraCadaver (since he said he solved his problem through my suggestions in the comments, I suppose). I don't mind if they marked it or not but that would help someone who has a similar issue with less of a hassle going through all the answers. – Markafa Creative Solutions Sep 03 '19 at 14:46
  • No it's just that i can't mark any comment as I have a ban on my profile and I don't understand why... – john_johnk Sep 10 '19 at 14:40

5 Answers5

1

Just for fun, assuming that service is unique:

$services = array_column($x['data'], null, 'service');

if(isset($services[$bus]) {
    echo $services[$bus]['network'];
} else {
    echo "Not found";
}
  • Extract an array and index it by service
  • Now you can access it by $services[$bus] ex. $services[8000]['network'].
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

almost right :

forEach syntax is the other way round : forEach(<array> as $iterator) oh an you need to handle the assignment to the $reslt variable (which I renamed to $found for some reason ) a bit different

 $found=NULL;
    // let's assume $x as this array here
    foreach($x['data'] as $record ){
        if($record['service'] == $bus){
            $found=$record['service'];
        }
    }

    if($found != NULL){
        // record found
        var_dump($record);
    }else{
        echo "Not found";
    }
jonathan Heindl
  • 844
  • 7
  • 16
  • 1
    Please add here some description of what have you changed in the code or where the mistake was. – joka00 Apr 01 '19 at 21:31
  • He changed the statement "in" to "as" in the foreach parameters, and added a var_dump($record); line in the if statement bellow. – Markafa Creative Solutions Apr 01 '19 at 21:35
  • 1
    Ok, but still please add some description to the post remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – joka00 Apr 01 '19 at 21:41
0

If I'm getting your question right, it should be something like this:

// For the sake of the example, first I reconstructed your array:
$ar1 = array("service" => 8000, "network" => "111.111.111");
$ar2 = array("service" => 9000, "network" => "222.222.222");
$ar3 = array("service" => 9500, "network" => "333.333.333");
$x = array("size" => 995,
        "data" => array($ar1,$ar2,$ar3));

$record = NULL;
$bus = 9000;

for($n = 0; $n < count($x["data"]); $n++){
    $checkService = $x["data"][$n];
    if($checkService["service"] == $bus){
        $record = $checkService["network"];
    }
}

if ($record) {
    // If record found:
    echo "Lookup Results for ".$bus.": ".$record;
    // Since we are searching for 9000 in this example, this should output -> Lookup Results for 9000: 222.222.222
} else {
    echo "Record not found";
}

This code could be simplified even further but I'm not sure what you need exactly.

You can check the end result through this example.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
0

Adding onto Johnathon Heindl:(sorry i cannot reply yet).

It may be useful to make $found an array, in case you found the same service having different networks.

$found=[];
// let's assume $x as this array here
foreach($x['data'] as $record ){
   if($record['service'] == $bus){
       //appends to $found
       $found[]=$record['service'];
   }
}
if(!empty($found){
   // record(s) found
   var_dump($record);
}else{
   echo "Not found";
}

Michael Peng
  • 806
  • 9
  • 11
-3
$found_record = NULL;
// let's assume $x as this array here
foreach($record in $x['data']){
if($record['service'] == $bus){
     $found_record = $record['service'];
      break;
    }
  }
 if($found_record){
   // record found
   var_dump($record);
  }else{
    echo "Not found";
}
  • 1
    Welcome to Stack Overflow! While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – joka00 Apr 01 '19 at 21:27