Solved

Extracting values from an array?

Posted on 2012-12-21
4
260 Views
Last Modified: 2012-12-21
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
0
Comment
Question by:aej1973
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
brendonfeeley earned 150 total points
Comment Utility
Something like this should work:

//Loop through array
for($i = 2; $i < sizeof($array); $i++){
	//Look for registrations string. Break if found, push host details to new array if not.
	if(strpos($array[$i], ' registrations.') !== false) {
		break;
	}else{
		array_push($hosts_array, $array[$i]);
	}
}

print_r($hosts_array);

Open in new window

0
 

Author Comment

by:aej1973
Comment Utility
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);
0
 
LVL 4

Expert Comment

by:brendonfeeley
Comment Utility
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];
0
 

Author Comment

by:aej1973
Comment Utility
Thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now