473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

get associative array for str_replace

110 100+
I'm trying to read an associative array into str_replace, but keep getting a undefined index responses, even though the index is visibly in the array.

here's the code
Expand|Select|Wrap|Line Numbers
  1.     foreach($gSplit as $gString) {
  2.  $genrenum = str_replace($gString, $y[$gString], $gString);
  3.  echo $genrenum.'<br />';
  4.     echo $gString;
  5.     }
  6. }
  7.  
and the array
Expand|Select|Wrap|Line Numbers
  1. [Acoustic] => 1 [Alternative] => 2 [Ambient] => 4 [Bluegrass] => 8 [Blues] => 16 [Christian] => 32 [Classical] => 64 [Country] => 128 [Down-tempo] => 256 [Electronica] => 512 [Emo] => 1024 [Folk] => 2048 [Funk] => 4096 [Garage] => 8192 [Gospel] => 16384 [Grunge] => 32768 [Hip Hop] => 65536 [House] => 131072 [Indie] => 262144 [Industrial] => 524288 [Jazz] => 1048576 [Jungle] => 2097152 [Latin] => 4194304 [Lounge] => 8388608 [Metal] => 16777216 [Pop] => 33554432 [Punk] => 67108864 [R&B] => 134217728 [Rap] => 268435456 [Reggae] => 536870912 [Rock] => 1073741824 [Roots Music] => 2147483648 [Salsa] => 4294967296 [Screamo] => 8589934592 [Ska] => 17179869184 [Soul] => 34359738368 [Swing] => 68719476736 [Techno] => 137438953472 [Trance] => 274877906944
  2.  
and the errors I am getting are
Expand|Select|Wrap|Line Numbers
  1. Notice: Undefined index: Pop in C:\EasyPHP 2.0b1\www\admin\genremath.php on line 63
  2.  
  3. Notice: Undefined index: Powerpop in C:\EasyPHP 2.0b1\www\admin\genremath.php on line 63
  4.  
  5. Notice: Undefined index: Indie in C:\EasyPHP 2.0b1\www\admin\genremath.php on line 63
  6.  
Clearly the index is in the array.

Any ideas?
Oct 31 '07 #1
4 2106
Atli
5,058 Expert 4TB
Hi.

If the array $y does indeed look like what you posted those errors make no sense. There must be something else causing this.
Are you absolutely sure that this is what the array $y looks like? Could you be running an out of date version of the file on your test server?

Also, your usage of the str_replace function doesn't make much sense to me, unless it is only intended as a test.
What I mean is; this line:
Expand|Select|Wrap|Line Numbers
  1. $genrenum = str_replace($gString, $y[$gString], $gString);
  2.  
Could just as well be replaced with this:
Expand|Select|Wrap|Line Numbers
  1. $genrenum = $y[$gString];
  2.  
Oct 31 '07 #2
pedalpete
110 100+
Thanks Atli

I'm pretty sure the array is exactly as i display it, as I've put

print_r($y); before my foreach statement.

However, you are absolutely correct on the $genrenum = $y['$gstring']; I can't believe i over complicated it like that.

the code is now...
Expand|Select|Wrap|Line Numbers
  1. print_r($gSplit);
  2. $gSplit = array_unique($gSplit);
  3. print_r($y);
  4.     foreach($gSplit as $gString) {
  5.  $genrenum = $y[$gString];
  6.  echo $genrenum.'<br />';
  7.     echo $gString;
  8.     }
  9.  
the output is still not working, I get this result where the first array is the genre types put into an array, and the second array is the array of all genre types. This is the output copied and pasted from the browser.
Expand|Select|Wrap|Line Numbers
  1. Pop | Powerpop | Indie
  2. Array ( [0] => Pop [1] => Powerpop [2] => Indie )
  3. Array ( [Acoustic] => 1 [Alternative] => 2 [Ambient] => 4 [Bluegrass] => 8 [Blues] => 16 [Christian] => 32 [Classical] => 64 [Country] => 128 [Down-tempo] => 256 [Electronica] => 512 [Emo] => 1024 [Folk] => 2048 [Funk] => 4096 [Garage] => 8192 [Gospel] => 16384 [Grunge] => 32768 [Hip Hop] => 65536 [House] => 131072 [Indie] => 262144 [Industrial] => 524288 [Jazz] => 1048576 [Jungle] => 2097152 [Latin] => 4194304 [Lounge] => 8388608 [Metal] => 16777216 [Pop] => 33554432 [Punk] => 67108864 [R&B] => 134217728 [Rap] => 268435456 [Reggae] => 536870912 [Rock] => 1073741824 [Roots Music] => 2147483648 [Salsa] => 4294967296 [Screamo] => 8589934592 [Ska] => 17179869184 [Soul] => 34359738368 [Swing] => 68719476736 [Techno] => 137438953472 [Trance] => 274877906944 )
  4. Notice: Undefined index: Pop in C:\EasyPHP 2.0b1\www\admin\genremath.php on line 64
  5.  
  6. Pop
  7. Notice: Undefined index: Powerpop in C:\EasyPHP 2.0b1\www\admin\genremath.php on line 64
  8.  
  9. Powerpop
  10. Notice: Undefined index: Indie in C:\EasyPHP 2.0b1\www\admin\genremath.php on line 64
  11.  
Oct 31 '07 #3
Atli
5,058 Expert 4TB
Ok, I see.

Try using the trim() function on the index before using it. Seeing as the array containing the indexes is called gSplit, I'm assuming it is created by splitting a string into smaller pieces?

This will trim any leading or trailing white-spaces from the text before it is used:
Expand|Select|Wrap|Line Numbers
  1. $genrenum = $y[trim($gString)]
  2.  
Oct 31 '07 #4
pedalpete
110 100+
the simplest answers are always the best. I had trim running on the first array, but sadly not on the second.

Thanks Atli.
Oct 31 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: chimi | last post by:
Is it possible to reference an index of an associative array using a variable? e.g.: If I had an array: $myArray = "myValue"; and if I were to declare a variable: $myVariable = "first"; ...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.