Jump to content

Rather simple question.. array_search() or other!!


Zugzwangle

Recommended Posts

I have the below array, $withoutBreaks:

Array ( [0] => [Event 90m + 5s, rated 
[1] => [site Room 1 
[2] => [Date 2010.06.18 
[3] => [Round ? 
[4] => [White Zugzwang1 
[5] => [black Jeff Morris 
[6] => [Result 1-0 
[7] => [WhiteElo 1543 
[8] => [blackElo 1537 
[9] => [PlyCount 5 
[10] => [EventDate 2010.06.18 
[11] => 1. d4 {3} c6 {2} 2. Nc3 {2} d5 {1} 3. Nf3 {4 Jeff Morris resigns (Lag: Av=0. 22s, max=0.3s)} 1-0 ) 

 

This array is made by a form.  The elements may differ in both number and in order, for example [blackElo may be element [3] rather than [6].  One constant, is the '[' followed by the immediate word i.e. [Event.

What I would like to happen, is to use array_search() to find which element the data is held in.

My code:

$datePart = array_search("[Date ", $withoutBreaks);
echo ($datePart);

Output:


None!!!

What am I doing wrong?

 

Link to comment
Share on other sites

In your array it's [EventDate 2010.06.18 not [Date 2010.06.18

 

Edit: Since you modified your post it leads me to believe you want something more like this:

 

foreach($withoutBreaks as $key => $val) {
if(substr($val, 0, 11) == '[EventDate ') {
	echo "Key: $key<br />\n";
	break;
}
}

Link to comment
Share on other sites

It looks like the values in the array actually contain a name and the actual valule. So, Building upon AlexWD's post, here is a function you could use. Just pass the function the "name" you are looking for and it will return the value:

 

function getValue($array, $name)
{
    foreach($array as $key => $value)
    {
        if(substr($value, 1, strlen($name))==$name)
        {
            return substr($value, strlen($name)+2);
        }
    }
    //No match found
    return false;
}

//Usage
echo getValue($withoutBreaks, 'EventDate');
//Ouput: 2010.06.18

 

Edit: Now that I think of it, it might be better to just create a function to conver the array to where the names are the keys and the values are the actual values:

Array (
    ['Event'] => 90m + 5s, rated
    ['Site Room'] => 1
    ['Date'] => 2010.06.18
    ['Round'] => ?
...

Link to comment
Share on other sites

Edit: Now that I think of it, it might be better to just create a function to conver the array to where the names are the keys and the values are the actual values:

Array (
    ['Event'] => 90m + 5s, rated
    ['Site Room'] => 1
    ['Date'] => 2010.06.18
    ['Round'] => ?
...

 

And to do something like that this should work:

 

$new = array();
foreach($withoutBreaks as $val) {
$parts = explode(' ', $val);
$new[substr(array_shift($parts), 1)] = implode(' ', $parts);
}

 

I made the assumption that the first word is the name and the rest is the value. Wasn't completely sure if it should be like 'Site Room' => 1 or 'Site' => 'Room 1', this code should do the latter.

Link to comment
Share on other sites

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.