views:

313

answers:

5

How can you do this? My code seen here doesn't work

for($i=0;i<count($cond);$i++){
 $cond[$i] = $cond[$i][0];
}
+1  A: 
// Make sure you have your first array initialised here!
$array2 = array();
foreach ($array AS $item)
{
    $array2[] = $item[0];
}

Assuming you want to have the same variable name afterwards, you can re-assign the new array back to the old one.

$array = $array2;
unset($array2); // Not needed, but helps with keeping memory down

Also, you might be able to, dependant on what is in the array, do something like.

$array = array_merge(array_values($array));
Mez
thanks for the response but I don't want to use up another variable name, in PHP the variable should write after the variable is read? I thought?
Supernovah
Yes, however, as you've got it in a loop, it'd be a little less confusing. Also, the count() is evaluated on each iteration. So as you're adding things to it, then this is always going to change. There are no limit on the number of variable names in PHP! you can always assign it back to the original variable after the loop (with $array = $array2;)
Mez
grantwparks
A: 

That should work. Why does it not work? what error message do you get? This is the code I would use:

$inArr;//This is the 2D array
$outArr = array();
for($i=0;$i<count($inArr);$i++){
        $outArr[$i] = $inArr[$i][0];
}
Marius
oh I see the problem... maybe the i<count... needs the $ sign >< wow
Supernovah
@Supernovah: Every variable, in every access to it, needs the $ sign in front of it's name.
lacop
+1  A: 

There could be problems if the source array isn't numerically index. Try this instead:

$destinationArray = array();
for ($sourceArray as $key=>$value) {
    $destinationArray[] = $value[0]; //you may want to use a different index than '0'
}
Benedict Cohen
This is true but my array is numerical - The error is my variable I was referred to without variable denoter $
Supernovah
+1  A: 

As previously stated, your code will not work properly in various situation. Try to initialize your array with this values:

$cond = array(5=>array('4','3'),9=>array('3','4'));

A solution, to me better readable also is the following code:

//explain what to do to every single line of the 2d array
function reduceRowToFirstItem($x) { return $x[0]; }

// apply the trasnformation to the array
$a=array_map('reduceRowTofirstItem',$cond);

You can read the reference for array map for a thorough explanation.

You can opt also for a slight variation using array_walk (it operate on the array "in place"). Note that the function doesn't return a value and that his parameter is passed by reference.

function reduceToFirstItem(&$x) { $x=$x[0]; }
array_walk($cond, 'reduceToFirstItem');
Eineki
Since the code I am writing is private I can be sure the array will always be numerical starting from zero without gaps. But I do thank you for the notice as I had kind of skipped the thought.
Supernovah
It was just to point a possible source of bugs. If you think you will not reuse your code, you can safely ignore the advice ;)
Eineki
+1  A: 

It can be as simple as this:

$array = array_map('reset', $array);
eisberg