Jump to content

Recommended Posts

I'm very beginner with arrays.... i KNOW, databases exist, it's more easy.. I can't use a database.. I have to use, this.. or similar.. that doesn't requiring a database engine... 

So... that array...

array (
     array(string, int, int, string, string),
     array(string, int, int, string, string),
     array(string, int, int, string, string),
    ....+250
     array(string, int, int, string, string)

);

I search by the element [#][0].. in the +250 some will contain the same infos and I would like to make them point to the element containing the info instead of repeating it and have to update at 3-4 places when it's time. Like what it's in "10" also good for "ten", "dix" and "diez"...

anyway to make that simple?  by reference or...???

I'm crying.. last time I used arrays... Turbo Pascal.. at school... or was it Turbo C++ 3.0?? Maybe it was with cobol... well it was the good ol' time of the 90s!

 

Thanks!

 

-TRP-
 

 

 

It sounds like it would be more efficient to make [#][0] the key of sets of subarrays . Then you can go straight ot the ones you want instead of repeatedly searching the whole array.

array (
    [string] => array (
                    array (int, int, string, string),
                    array (int, int, string, string),
                    array (int, int, string, string),
                    array (int, int, string, string)
                ),
                
                ...
                
    [string] => array (
                    array (int, int, string, string),
                    array (int, int, string, string),
                    array (int, int, string, string),
                    array (int, int, string, string)
                )
                
);

 

  • Like 1
7 hours ago, chhorn said:

I don't understand what you want, post a reproducible example.

I have...

array (
     array("10", 25, 31, "Yes", "No"),
     array("20", 21, 18, "No", "No"),
     array("30", 11, 47, "No", "No"),
     array("40", 14, 60, "No", "No"),
     array("ten", 25, 31, "Yes", "No"),
     array("dix", 25, 31, "Yes", "No"),
     array("twenty", 21, 18, "No", "No"),
);

I would like to do...

array (
     array("10", 25, 31, "Yes", "No"),
     array("20", 21, 18, "No", "No"),
     array("30", 11, 47, "No", "No"),
     array("40", 14, 60, "No", "No"),
     array("ten", [refer to 10] for other datas),
     array("dix", [refer to 10 for other datas]),
     array("twenty", [refer to 20 for other datas]),
);

I would be interested to know WHY you are wanting to do this. What are these "duplicate" records for and if their data is the same as the 'parent' why do you need to refer to the parent for the data? Also, since each element doesn't have a defined index I'm not sure how you want to make the reference. Do you just want to store the first element value (e.g. '10') in the child element? This would be a lot easier to work with if the elements also had named indexes. Anyway, here is a possible solution. Note, once you run this IF you change one of the referenced values in a parent element it WILL be reflected in the child elements.

function mapArrayReferences(&$inputArray)
{
    //Create array to map unique value sets
    $mappingAry = array();
    foreach($inputArray as $elementKey => $element)
    {
        //Remove the first element off the array
        array_shift($element);
        //Create a hash of the remaining array
        $hash = md5(implode("|", $element));
        //Check if the hash already exists in the mapping array
        $parentKey = array_search($hash, $mappingAry);
        if($parentKey !== false)
        {
            //Change the current element values to refer to parent
            $inputArray[$elementKey][1] = &$inputArray[$parentKey][1];
            $inputArray[$elementKey][2] = &$inputArray[$parentKey][2];
            $inputArray[$elementKey][3] = &$inputArray[$parentKey][3];
            $inputArray[$elementKey][4] = &$inputArray[$parentKey][4];
        }
        else
        {
            //Add new parent to mapping array
            $mappingAry[$elementKey] = $hash;
        }
    }
    return;
}

Usage

$dataAry = array(
  array("10", 25, 31, "Yes", "No"),
  array("20", 21, 18, "No", "No"),
  array("30", 11, 47, "No", "No"),
  array("40", 14, 60, "No", "No"),
  array("Ten", 25, 31, "Yes", "No"),
  array("dix", 25, 31, "Yes", "No"),
  array("twenty", 21, 18, "No", "No")
);
//Map child elements to parent element values
mapArrayReferences($dataAry);
//Change a parent element value
$dataAry[0][2] = 99;
print_r($dataAry);

Output

Quote

Array
(
    [0] => Array
        (
            [0] => 10
            [1] => 25
            [2] => 99   //<== Value that was changed
            [3] => Yes
            [4] => No
        )

    [1] => Array
        (
            [0] => 20
            [1] => 21
            [2] => 18
            [3] => No
            [4] => No
        )

    [2] => Array
        (
            [0] => 30
            [1] => 11
            [2] => 47
            [3] => No
            [4] => No
        )

    [3] => Array
        (
            [0] => 40
            [1] => 14
            [2] => 60
            [3] => No
            [4] => No
        )

    [4] => Array
        (
            [0] => Ten
            [1] => 25
            [2] => 99     //<== Dynamically changed by reference
            [3] => Yes
            [4] => No
        )

    [5] => Array
        (
            [0] => dix
            [1] => 25
            [2] => 99     //<== Dynamically changed by reference
            [3] => Yes
            [4] => No
        )

    [6] => Array
        (
            [0] => twenty
            [1] => 21
            [2] => 18
            [3] => No
            [4] => No
        )

)

Edited by Psycho
  • Like 1

I'd prefer to "normalize" the array data instead of storing duplicate records ...

$data Array
(
    [10] => Array
        (
            [0] => 25
            [1] => 31
            [2] => Yes
            [3] => No
        )

    [20] => Array
        (
            [0] => 21
            [1] => 18
            [2] => No
            [3] => No
        )

    [30] => Array
        (
            [0] => 11
            [1] => 47
            [2] => No
            [3] => No
        )

    [40] => Array
        (
            [0] => 14
            [1] => 60
            [2] => No
            [3] => No
        )

)

and have a second array to point synonymous keys to the correct records ...

$keys Array
(
    [10] => 10
    [20] => 20
    [30] => 30
    [40] => 40
    [Ten] => 10
    [dix] => 10
    [twenty] => 20
)

You would reference the data array via the keys array EG

print_r( $data[ $keys['dix'] ] );

Code to normalize the original array...

sort($dataAry);                          // ensure keys are in sequence with numeric keys first
$data = $keys = [];
foreach ($dataAry as $d) {
    $k = $d[0];                          // key = first element
    $v = array_slice($d,1);              // values = the rest
    $k1 = array_search($v, $data);       // have we stored it yet?
    if ($k1 === false) {
        $data[$k] = $v;
        $keys[$k] = $k;
    }
    else {
        $keys[$k] = $k1;
    }
}

 

Database equivalent:

+-------------+                   
|   data      |                   
+-------------+                   
|  data_id    |-------+                   
|  Field 1    |       |            
|  Field 2    |       |       +------------------+    
|  Field 3    |       |       |   keys           |
|  Field 4    |       |       +------------------+
+-------------+       |       |   pseudokey      |
                      +------<|   data_id        |
                              +------------------+

SELECT field1
     , field2
     , field3
     , field4
FROM data
        INNER JOIN
     keys USING (data_id)
WHERE pseudokey = 'twenty'  

 

Edited by Barand
4 hours ago, Psycho said:

I would be interested to know WHY you are wanting to do this. What are these "duplicate" records for and if their data is the same as the 'parent' why do you need to refer to the parent for the data? Also, since each element doesn't have a defined index I'm not sure how you want to make the reference. Do you just want to store the first element value (e.g. '10') in the child element? This would be a lot easier to work with if the elements also had named indexes. Anyway, here is a possible solution. Note, once you run this IF you change one of the referenced values in a parent element it WILL be reflected in the child elements.

 

 

ten, dix, diez are all 10 in different languages.. all contain the same datas... just want to simplify the thing by updating 1 place!

Thanks for the answer, I'll take a look and adapt it!!!

 

-TRP-

 

Edited by TheRealPage
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.