I'm trying to add another column of data to each row in a foreach loop. It's purpose is to remember the element of data importeded from XML processed to an multidimensional array. It's stuck as a scalar though the var_dumps looks fine.

<?php 
$KEY = 0;
foreach ($eventsArray as $keyMe){
  $thisKey['KEY'][0] = strval($KEY);
  $keyedArray = array_merge($keyMe, $thisKey);
$KEY++;
}

// Prep for multisort
foreach ($keyedArray as $key => $value){
  $date[$key] = $value['DATE'];
  $title[$key] = $value['TITLE'];
  $link[$key] = $value['LINK'];
  $slide[$key] = $value['SLIDE'];
  $location[$key] = $value['LOCATION'];
  $time[$key]= $value['TIME'];
  $KEY[$key] = $value['KEY']; // Warning: Cannot use a scalar value as an array
}

/* var_dump(
array(7) { 
  ["DATE"]=> array(1) { [0]=> string(10) "2012-12-18" } 
  ["TITLE"]=> array(1) { [0]=> string(20) "Business Connections" } 
  ["LINK"]=> array(1) { [0]=> string(38) "companyProfile-BusinessConnections.htm" } 
  ["SLIDE"]=> array(1) { [0]=> string(2) "16" } 
  ["LOCATION"]=> array(1) { [0]=> string(8) "Ricky Js" } 
  ["TIME"]=> array(1) { [0]=> string(3) "8am" } 
  ["KEY"]=> array(1) { [0]=> string(2) "23" } 
} 
*/
share|improve this question
You are doing $KEY[$key], when you defined $KEY as INT ($KEY = 0;). You're treating an INT as an array, therefore you get a warning. I'm not sure what you're doing, but what if you do $KEY = array(); before the second foreach? – renocor Sep 9 '12 at 18:53
$KEY is the value I'm trying to add to the array. I tried array($KEY=0) but the problem persists. – Jarrett Mattson Sep 9 '12 at 19:01
Try doing $KEY = array(); before the second foreach, so you won't get that warning, and you'll be able to do $KEY[$key] = $value['KEY'];. Also, I noticed you overwrite $keyedArray in the first foreach, I don't know if that's intentional. – renocor Sep 9 '12 at 19:10
1  
Hmm what about change all your $value['DATE|TITLE|LINK|etc'] to $value[0]? so it's $KEY[$key] = $value[0]; – renocor Sep 9 '12 at 19:33
1  
It was that darned overwrite. After changing $keyedArray in first foreach to $keyedArray[$KEY] and it works fine. THANKS!!! – Jarrett Mattson Sep 9 '12 at 20:11
show 1 more comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.