-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";
}
8
  • Before using foreach loop, you should check the doc php.net/manual/en/control-structures.foreach.php Commented Apr 1, 2019 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 ? Commented Apr 1, 2019 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. Commented Apr 1, 2019 at 21:24
  • Did you give up? Commented Apr 2, 2019 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 Commented Apr 4, 2019 at 8:01

5 Answers 5

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'].
Sign up to request clarification or add additional context in comments.

Comments

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";
    }

3 Comments

Please add here some description of what have you changed in the code or where the mistake was.
He changed the statement "in" to "as" in the foreach parameters, and added a var_dump($record); line in the if statement bellow.
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.
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";
}

Comments

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.

2 Comments

Thanks a lot Markafa, you are the only one who understood what I needed! Many thanks !
You're welcome. I'm glad I could help. Did it help you solve your problem though? Or do you need further explanation/assistance with your problem? @john_johnk
-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 Comment

Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.