Forum Moderators: coopster

Message Too Old, No Replies

Array fusion question

         

asantos

3:55 am on Sep 10, 2006 (gmt 0)

10+ Year Member



Hi.
I need to get an array that holds the values 10,11,12 from this other array. How can i achieve that?

Array
(
[0] => Array
(
[0] => 10
[1] => 1
[2] => Fútbol
[3] => futbol
[4] => 0
)
[1] => Array
(
[0] => 11
[1] => 1
[2] => Tenis
[3] => tenis
[4] => 0
)
[2] => Array
(
[0] => 12
[1] => 1
[2] => Artes marciales
[3] => artes-marciales
[4] => 0
)
)

Psychopsia

5:03 am on Sep 10, 2006 (gmt 0)

10+ Year Member



Hi!

Do you need a function that saves the first item of each array, and then return it, right?

For the first item, what array combination should be saved, (only value) or (key => value)?

coopster

11:17 am on Sep 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Loop through and grab the first value of each array.
$array = array(); // initialize 
foreach ($array1 as $array2) {
$array[] = $array2[0];
continue [php.net];
}
print_r($array);

Psychopsia

5:43 pm on Sep 11, 2006 (gmt 0)

10+ Year Member




//
// @array_first_key($ary, $pos)
// $ary - The source array
// $pos - key position that you want to return
//
function array_first_key($ary, $pos = 1)
{
$ret = array();
foreach ($ary as $ary2)
{
$ret[] = $ary2[$pos - 1];
}

return $ret;
}

[edited by: Psychopsia at 5:48 pm (utc) on Sep. 11, 2006]

supermoi

3:09 am on Sep 12, 2006 (gmt 0)

10+ Year Member



coopster, is that "continue" really necessary?

coopster

4:36 am on Sep 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No, it is not. Originally I was filling the new array a bit differently but made it more efficient by grabbing the zero index that you were after instead. You can get rid of it now.

orion_rus

10:09 am on Sep 12, 2006 (gmt 0)

10+ Year Member



for ($i=0;$i<count($array);$i++)
{
if (in_array(array(10,11,12),$array[$i]))
{echo "In array";}
}

asantos

2:02 pm on Sep 13, 2006 (gmt 0)

10+ Year Member



Hi, thanks for the tips, i was out of internet yesterday.