Jump to content

Use of Multidimentional array


Go to solution Solved by Psycho,

Recommended Posts

hello everybody

 

I have an multidimentional array and i want to update it programitically as per required..

 

The code is sown below...

$array =
    Array
    (
    [10/3/14] => Array
        (
            [UK] => 1
            [US] => 5
        )

    [11/3/14] => Array
        (
            [UK] => 5
            [US] => 10
        )

    [12/3/14] => Array
        (  [UK] => 15
           [US] => 20
        )

    [13/3/14] => Array
        (
           [UK] => 25
           [US] => 30
        )
    }

I want to check the server date with the dates present in the array as the [key] and if it matches, i want to change the values of that [key]...

-----------------------------------------------------------------------------------------------------------

example

 

let date = 10/3/14

if it matches in the array, then only i want to change the value of US to 100...

-----------------------------------------------------------------------------------------------------------

It should be done dynamically...

 

Any help will be greatly appreciated... Thank u in advance...

 

Link to comment
https://forums.phpfreaks.com/topic/286904-use-of-multidimentional-array/
Share on other sites

  • Solution

Using isset() in this scenario is probably fine, but it can cause problems when using it in some scenarios to see if a specific array key exists. The manual for array_key_exists() even includes a specific statement about the two

 

 

isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does.

I actually got tripped up by this before.

 

Although isset() would work in this specific scenario, in the interest of good programming practices, I think array_key_exists() would be appropriate.

 

Building upon Kicken's solution, you can build a function to do this:

function updateArray(&$sourceArray, $date, $country, $value)
{
    //Check if date exists in source array
    if(!array_key_exists($date, $sourceArray))
    {
        //Date not found
        return false;
    }
    //Update value
    $sourceArray[$date][$country] = $value;
    return true;
}

//Usage
updateArray($array, '10/3/14', 'US', 100);
Edited by Psycho
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.