Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Extracting values from an array?

Posted on 2012-12-21
4
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
brendonfeeley earned 600 total points
ID: 38714102
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
ID: 38714367
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
ID: 38714392
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
ID: 38714410
Thanks.
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 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