hi, i want to change an array's key to its values.
the array is like this:
<php
Array ( [0] => category_name: [1] => c1 [2] => types: [3] => t1 )
?>
i want to make the same array's values as key, like below using some array functions or some other functions please help
<?php
Array ( [category_name:] =>c1[types:]=>t1 )
?>

Recommended Answers

All 8 Replies

function toAssoc($arr){
 $data=array();
 if(is_array($arr))
 {
   for($i=0,$limit=count($arr); $i < $limit; ++$i)
   {
      $data[$i]=isset($data[1+$i]) ? $data[1+$i]: '';
      ++$i;
   }
 }
return $data;
}

Hi,

Try this:

$arrInput = array('abc', 'def', 'ghi', 'jkl', 'mno', 'pqr');
$arrOutput = index_array($arrInput);

function index_array($arrInput) {
    $arrOutput = array();
    $intIndex = 0;

    if(is_array($arrInput)) {
        while($intIndex <= count($arrInput)) {
            if(isset($arrInput[$intIndex]) && isset($arrInput[$intIndex + 1])) {
                $arrOutput[$arrInput[$intIndex]] = $arrInput[$intIndex + 1];
            }

            $intIndex += 2;
        }
    }
    
    return $arrOutput;
}

var_dump($arrInput, $arrOutput); die;

R.

hielo,

You have your $arr and $data variables mixed up in your loop, so your function will always return a blank array value for each index.

function toAssoc($arr){
$data=array();
if(is_array($arr))
{
for($i=0,$limit=count($arr); $i < $limit; ++$i)
{
$data[$i]=isset($data[1+$i]) ? $data[1+$i]: '';
++$i;
}
}
return $data;
}

R.

LOL. Yes of course. An oversight of my part. It should be:

$data[ $arr[$i] ]=isset($arr[1+$i]) ? $arr[1+$i]: '';

As a gesture of good faith let me return the favour and tell you that your while needs to check for "less than", not "less than or equal to".

Regards,
Hielo

This was already covered in: http://www.daniweb.com/forums/thread310238.html
Almost this exact question, with the exception the op there asked for a way to break a string down first then convert a numerical array into an assoc.

function toAssoc(array $array){
     //Temporary array to hold new pieces
     $temp = array();

     //Iterate over supplied array by key and value
     foreach($array as $k => $v){
             //Make sure the key is even using a bitwise operator for efficiency
	     if( ($k&1) == 0 ){
                     //If we have an even key, 0, 2, 4, 6, etc.
                     //Create an array item with key of the current value
                     //and value of next odd array item.
		     $temp[$v] = (isset($pieces[$k+1]) ? $pieces[$k+1] : '');
	     }
     }
     return $temp;
}

Usage:

$test = array('category_name:','c1','types:','t1');
print_r(toAssoc($test));
//Array ( [category_name:] =>c1[types:]=>t1 )

-hielo & robothy

You both had introduced a relatively well known performance no no in your codes.
You should never have a function call in your loops. for($i=0,$limit=count($arr); $i < $limit; ++$i) while($intIndex <= count($arrInput)) In both pieces of code, move the count() function calls outside of the loop initialization.
This is evaluated on every iteration of the loop. You can verify this with a code trace in xdebug.

-hielo

You can also remove the is_array check from your code by specifying the the parameter passed is of type array. Unfortunately besides object types and the array type there are no other function argument types you can use.

You both had introduced a relatively well known performance no no in your codes.

I didn't. The count is in the initializing part of the for construct.
"The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop."
http://us3.php.net/manual/en/control-structures.for.php

You should never have a function call in your loops.

agree

You can also remove the is_array check from your code by specifying the the parameter passed is of type array. Unfortunately besides object types and the array type there are no other function argument types you can use.

Excellent! Thanks for that.

Ha! Talk about egg on my face. I read this on my phone and mistook the , for a semi-colon. I stand corrected.

Talk about egg on my face

Let's make that a pie instead. That way at least you will "enjoy" somewhat:)

It happens. Thanks for the info on the other matter.

Regards,
Hielo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.