finding value in multidimensional array

This is a discussion on finding value in multidimensional array within the alt.comp.lang.php forums, part of the PHP Programming Forums category; php 5.x In my php I define an multidimensional array like this: $test = array(array(1, 0 , 3), array('...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-31-2007
Jeff
 
Posts: n/a
Default finding value in multidimensional array

php 5.x

In my php I define an multidimensional array like this:
$test = array(array(1, 0 , 3), array('aa', 'dd', 'cc'));

I need to seach the first array ( array(1, 0 , 3) ) of $test for a value...

The first array ( array(1, 0 , 3) ) get it's values from using mt_rand to
generate random values. I want these integer values to be unique... so I
need to perform an if-test on the genereated number - if number already
exist then generate another number and check that one too, until a unique
number is generated

I'm not sure how to do it so please if you have any suggestions on how to it
then please post your suggestion here

Jeff


Reply With Quote
  #2 (permalink)  
Old 08-31-2007
Ian Hobson
 
Posts: n/a
Default Re: finding value in multidimensional array

Jeff wrote:
> php 5.x
>
> In my php I define an multidimensional array like this:
> $test = array(array(1, 0 , 3), array('aa', 'dd', 'cc'));
>
> I need to seach the first array ( array(1, 0 , 3) ) of $test for a value...
>
> The first array ( array(1, 0 , 3) ) get it's values from using mt_rand to
> generate random values. I want these integer values to be unique... so I
> need to perform an if-test on the genereated number - if number already
> exist then generate another number and check that one too, until a unique
> number is generated
>
> I'm not sure how to do it so please if you have any suggestions on how to it
> then please post your suggestion here
>

You are not very clear about what you want to achieve.

If what you want is an array filled with unique numbers from 0 to N, in
random order, then you can do it in two steps.

1) Fill the array with 0 to N in any order (a[i] = i is just fine).
2) Swap the ith element with a random element for i = 0 to N.

<?php
$s = 5;
$a = array();
for ($i=0; $i<$s; $i++) $a[$i] = $i;
for($i = 0; $i<$s; $i++) {
$j = mt_rand(0,$s-1);
$t = $a[$j];
$a[$j] = $a[$i];
$a[$i] = $t;
}
print_r($a);
?>

Regards

Ian
Reply With Quote
  #3 (permalink)  
Old 09-01-2007
Aaron Saray
 
Posts: n/a
Default Re: finding value in multidimensional array

Just a thought: you might want to try doing array_search() against
$outerArray[0] - as that will search the outer array's first array -
which I believe is your unique IDs.
If your second array is values associated with your first array, you
might want to try keying your array by the unique id's you're
generating, and set the value to the 'aa' or 'bb' or whatever.
-aaron

On Aug 31, 4:46 pm, "Jeff" <it_consulta...@hotmail.com.NOSPAM> wrote:
> php 5.x
>
> In my php I define an multidimensional array like this:
> $test = array(array(1, 0 , 3), array('aa', 'dd', 'cc'));
>
> I need to seach the first array ( array(1, 0 , 3) ) of $test for a value...
>
> The first array ( array(1, 0 , 3) ) get it's values from using mt_rand to
> generate random values. I want these integer values to be unique... so I
> need to perform an if-test on the genereated number - if number already
> exist then generate another number and check that one too, until a unique
> number is generated
>
> I'm not sure how to do it so please if you have any suggestions on how to it
> then please post your suggestion here
>
> Jeff



Reply With Quote
  #4 (permalink)  
Old 09-06-2007
michael@greenquery.com
 
Posts: n/a
Default Re: finding value in multidimensional array

Hi Jeff,

I don't completely understand what you want to do.
But to search multidimensional arrays for a name or value. Then i
would write a function like this.

//In this example i'll search for values. And i'll return the key name

function searchArray($array,$searchitem){
$keyname ="";
//Loop throw array
foreach($array as $name => $value){
//See if value is a new array
if (is_array($value)){
$keyname=searchArray($value,$searchitem);
if ($keyname != "") return $keyname;
} else {
// the value is not a array but just a value.
if ($value == $searchitem) {
return $name;
}
}
}
return $keyname;
}

// create array
$names = array('Michael'=>'Developer','James'=>'Contact');
// create array
$emails =
array('Michael'=>'michael@server.com','James'=>'ja mes@server.com');

// appends arrays
$people[] = $names;
$people[] = $emails;

//Find my name by email
$person_name = searchArray($people,'michael@server.com');

print "the email michael@server.com is owned by $person_name" ;

On 31 Aug., 23:46, "Jeff" <it_consulta...@hotmail.com.NOSPAM> wrote:
> php 5.x
>
> In my php I define an multidimensional array like this:
> $test = array(array(1, 0 , 3), array('aa', 'dd', 'cc'));
>
> I need to seach the first array ( array(1, 0 , 3) ) of $test for a value...
>
> The first array ( array(1, 0 , 3) ) get it's values from using mt_rand to
> generate random values. I want these integer values to be unique... so I
> need to perform an if-test on the genereated number - if number already
> exist then generate another number and check that one too, until a unique
> number is generated
>
> I'm not sure how to do it so please if you have any suggestions on how to it
> then please post your suggestion here
>
> Jeff



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:03 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0