Optimize string replacement in PHP

1

I am trying an array of considerable size with some data that I would like to group.

This would be an example of the array:

[24] => Array
    (
        [0] => /03-19_solemnidad-de-san-jose-homilias/
        [1] => 53
    )

[25] => Array
    (
        [0] => /03-19_solemnidad__san-jose_homilias/
        [1] => 1
    )

[26] => Array
    (
        [0] => /03-19_solemnidad_san-jose-esposo-de-la-bienaventurada-virgen-maria_homilias/
        [1] => 970
    )

[27] => Array
    (
        [0] => /03-19_solemnidad_san-jose_homilias/
        [1] => 1622
    )

[26] => Array
    (
        [0] => /03-19_solemnidad_san-jose_homilias/
        [1] => 14
    )


[150] => Array
    (
        [0] => /19-03_solemnidad_san-jose_homilias/
        [1] => 1
    )

[151] => Array
    (
        [0] => /19-03_solemninad_san-jose_homilias/
        [1] => 1
    )

[152] => Array
    (
        [0] => /19-3_solemninad_san-jose_homilias/
        [1] => 2
    )

In this case I want all the indexes 0 indicated here, to acquire this unique value:

/03-19_solemnidad_san-jose-esposo-de-la-bienaventurada-virgen-maria_homilias/

to then apply another code that I already have that removes duplicates, adding the value of the index 1 to the only value that will remain.

To match all the index values 0 I'm doing it one by one:

$row[0]=str_replace('/03-19_solemnidad-de-san-jose-homilias/', '/03-19_solemnidad_san-jose-esposo-de-la-bienaventurada-virgen-maria_homilias/', $row[0]);

$row[0]=str_replace('/03-19_solemnidad__san-jose_homilias/', '/03-19_solemnidad_san-jose-esposo-de-la-bienaventurada-virgen-maria_homilias/', $row[0]);

...

What I want to know is if there is any way to optimize the replacement. For example, creating an array or something, in which the final value is found and the values to be searched / replaced, instead of doing it one by one.

    
asked by A. Cedano 20.03.2018 в 16:27
source

1 answer

0

As far as I can see in your question you have an array of fixes, then it is enough to go through the complete array and then in each of the internal fixes replace the value in your field 0 with the default value that you you want to assign.

Example:

<?php
$arreglo = [
   ["/03-19_solemnidad_san-jose-esposo-de-la-bienaventurada-virgen-maria_homilias/",970],
   ["/03-19_solemnidad_san-jose_homilias/",1622],
   ["/03-19_solemnidad_san-jose_homilias/12",1500],
   ["/03-19_solemnidad_san-jose_homilias/aaawasd",1622]
];

foreach($arreglo as $dato){
   $dato[0] = "/03-19_solemnidad_san-jose-esposo-de-la-bienaventurada-virgen-maria_homilias/";
} 
?>
    
answered by 20.03.2018 в 19:33