Avatar of aej1973
aej1973

asked on 

Extracting values from an array?

Hi, I have an array as follows, I need only array[2] and array[3], how can I extract only these two array? Some time there will be more than two hosts, in essence I need all the array values between array[1] and the array that that has the string "registrations.". How can I do this?

Array
(
    [0] => Privilege: Command
    [1] => Host          username       password                          
    [2] => host01       user1             pw01            
    [3] => host02       user2             pw02  
     
    [4] => 2  registrations.
    [5] =>
    [6] => Follows
)

Thanks of the help.

A
PHP

Avatar of undefined
Last Comment
aej1973
ASKER CERTIFIED SOLUTION
Avatar of brendonfeeley
brendonfeeley
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of aej1973
aej1973

ASKER

brendonfeeley, thank you. I am not sure why but I keep getting the following error:

PHP Warning:  array_push() expects parameter 1 to be array, null given in /var/www/registryShowCli.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, null given in /var/www/registryShowCli.php on line 19

My code is as follows:

 $peer = implode("\n", $peer);//puts the whole thing in an array
 $row = explode("\n", $peer);//puts each line in an array
  print_r($row);
             for($i = 2; $i < sizeof($row); $i++){
                 if(strpos($row[$i], 'registrations.') !== false) {
                    break;
        }else{
                array_push($rows, $row[$i]);
        }
}

     print_r($rows);
Avatar of brendonfeeley
brendonfeeley
Flag of United Kingdom of Great Britain and Northern Ireland image

Place this line before the loop:

$rows = array();

The array needs to exist before you can push to it.

You could also replace the array_push with the following:

$rows[] = $row[$i];
Avatar of aej1973
aej1973

ASKER

Thanks.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo