Jump to content

Prepend a $key => $value pair


Go to solution Solved by requinix,

Recommended Posts

Hey guys,

 

I'm trying to run a while loop for an array that includes an ID value in each row. I'm trying to temporarily disable that value while I perform a foreach loop, then prepend the value back into the array after the foreach loop is finished.

function array_unshift_assoc(&$arr, $key, $val) { 
		$arr = array_reverse($arr, true); 
		$arr[$key] = $val; 
		$arr = array_reverse($arr, true);
	}

So far I've come up with this function (which works), but I feel like there may be an easier way? Rest of my code is below. Thoughts? 

while($row = mysql_fetch_assoc($get_specs)) {

		//Set the phones id to a temporary variable.
		$tempid = $row['id'];

		//Unset the id so that we can perform the foreach loop without 
		//it interfering with the function.
		unset($row['id']);

		foreach($row as $spec => $value) {
			get_spec_rating($spec, $value);
			echo $rating . "<br/><br/>";
		}

		//Unshift the id value back into the array 
		array_unshift_assoc($row, "id", $tempid);
		
	}

Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/277434-prepend-a-key-value-pair/
Share on other sites

Or just not output anything if $spec=="id".

 

Thanks requinix,

 

I thought of this, but if my function passes the key ID andits value, I get an error. I could try to alter my function to suit, but it would be a pain in the ass to say the least.

 

Still, thanks for the tip :)

I thought of this, but if my function passes the key ID andits value, I get an error. I could try to alter my function to suit, but it would be a pain in the ass to say the least.

You don't need to edit the function. as requinix said:

while($row = mysql_fetch_assoc($get_specs)) {
  foreach($row as $spec => $value) {
    if($spec == 'id') continue;
    get_spec_rating($spec, $value);
    echo $rating . "<br/><br/>";
  }
}

Here's an easier way to temporarily remove the id

$arr = array (
        array(
        'id' => 1,
        'k2' => 'a',
        'k3' => 'b'
        ),
        array(
        'id' => 2,
        'k2' => 'c',
        'k3' => 'd'
        ),
        array(
        'id' => 3,
        'k2' => 'e',
        'k3' => 'f'
        )
        );
        
foreach ($arr as $row) {
    foreach (array_slice($row,1) as $k=>$v) {
        echo "$k | $v<br>";
    }
    print_r($row);  // row is unchanged
    echo "<br>";

}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.