Jump to content

Recommended Posts

Hi All,

I hope I've used the right/correct term for this.

So I'm using: 

array_map(addQuotes, $line)

Which calls this function:

function addQuotes($line) {
	return "\"$line\"";
}

It works and adds quotes around everything I need. However, I need to exclude one column called SKU.

Is it possible to use the above but skip this column? I know using array_map means you need to return the same number of columns, so my thought was:

	function addQuotes($line) {
		
		if( $line['SKU']){
			return $line;
		} else {
			return "\"$line\"";
		}
		
	}

Therefore not applying the quote treatment.

Sadly, didn't work (not surprised as figured it was too obvious!

The data is an array:

Array
(
    [0] => Array
        (
            [0] => SKU
            [1] => Name
            [2] => Product Category
            [3] => ImageURL
            [4] => Manufacturer
            [5] => Model
            [6] => MPN
            [7] => EAN
            [8] => MemberID
        )

    [1] => Array
        (
            [SKU] => CIHYG21SX
            [Name] => Beko CIHYG21SX 60cm Gas Hob - Stainless Steel
            [Product Category] => Gas Hobs
            [ImageURL] => https://www.domain.com/uploads/2021/Beko-CIHYG21SX.png
            [Manufacturer] => Beko
            [Model] => CIHYG21SX
            [MPN] => CIHYG21SX
            [EAN] => 8690842386756
            [MemberID] => 456789
        )

    [2] => Array
        (
            [SKU] => T26DS49N0
            [Name] => Neff T26DS49N0 60cm Gas Hob - Stainless Steel
            [Product Category] => Gas Hobs
            [ImageURL] => https://www.domain.com/uploads/2018/Neff-T26DS49N0.jpg
            [Manufacturer] => Neff
            [Model] => T26DS49N0
            [MPN] => T26DS49N0
            [EAN] => 4242004203384
            [MemberID] => 456789
        )

)

I'm then using a loop to get the data and pass into my function (if that helps)

No doubt something I've overlooked but any help is much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/313594-array_map-skip-column/
Share on other sites

array_map doesn't pass the array key. You could call it with two arrays (the first of values and second of keys) but that would reindex the array.

array_walk with the value by-reference would work. As would a simple foreach loop.

  • Thanks 1
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.