Php 如何更改正在显示的数组文本?

Php 如何更改正在显示的数组文本?,php,arrays,Php,Arrays,好的,我只想打印数组的内容,然后我想替换数组字符串的内容,然后用另一个名称替换它们…请有人帮帮我。。。 我真的不知道该怎么做,有人能告诉我怎么做,如何使用preg_替换吗?无论我在哪里查找如何操作,它们都有非常奇怪的符号: 即使在我的代码中,我也只是把这些东西放在里面,因为它是其他人做的>>,即使是在php手册网站上…我想我讨厌php <!doctype html> <html lang="en"> <head> <title>test6

好的,我只想打印数组的内容,然后我想替换数组字符串的内容,然后用另一个名称替换它们…请有人帮帮我。。。 我真的不知道该怎么做,有人能告诉我怎么做,如何使用preg_替换吗?无论我在哪里查找如何操作,它们都有非常奇怪的符号:

即使在我的代码中,我也只是把这些东西放在里面,因为它是其他人做的>>,即使是在php手册网站上…我想我讨厌php

<!doctype html>

<html lang="en">
<head>
    <title>test6</title>
</head>
<body>
<!-- Insert your content here -->
<?php
class myArrayContainer{
    function myArrayFunction(){
        return $myArray = array(
            "Name" => "John",
            "LastName" => "Smith",
        );
    }

}
$myShitz = new myArrayContainer();
$myShit = $myShitz->myArrayFunction();
$myShitClass = new myArrayClass($myShit);


//print_r($myShit);
class myArrayClass {



    function __construct($myArray){

        echo ("Printing my Array as Recieved");
        echo ("</br>");
        print_r(array_values($myArray));

       $myProcessClass = new myProcess($myArray);
    }

}

class myProcess {
    function __construct($sameArray){
        $mySentence = serialize($sameArray);
        print_r($mySentence);
        $placements = array ("John" => "Jose", "Smith" => "Tobar");
        preg_replace("/:(\w+)/e", $placements[$1], $mySentence);
    }

}
    ?>
</body>
</html>

测试6
  • myArrayClass::\u构造中的
    print\u r
    应打印“John”和“Smith”(无键)。不是吗
  • $mycentence
    是一个字符串,因此不需要打印
  • 您没有对
    preg\u replace
    结果执行任何操作
  • 无法更改序列化数据。PHP的序列化数据很奇怪(不像JSON),所以不能到处更改值。如果要更改值,请使用
    json\u encode()->preg\u replace()->json\u decode()

  • 不过要小心。如果你这样替换它,即使是json也会弄糟。

    当我运行myArrayClass::\u构造时,它会打印这个:数组([0]=>John[1]=>Smith),当我添加这个:$myentence=json\u encode($Samarray);它还输出:{“Name”:“John”,“LastName”:“Smith”}$mycentence=json_encode($samarray)$mycentence=preg_replace('/o/','t',$mycentence)$otherArray=json_decode($mycentence);打印($otherArray);我把它添加到我的代码中,但现在它导致了一个错误o。o什么是JSON?可捕获的致命错误:stdClass类的对象无法转换为第45行C:\xampp\htdocs\xampp\Sites\test6.php中的字符串,这就是它给我的错误。感谢您的帮助。请参阅:
    json\u decode()
    默认情况下创建对象,而不是关联数组(如您的数组)。您可以使用
    $assoc
    参数创建assoc数组。类myProcess{function{uuuu construct($samarray){$myentence=json_encode($samarray);$myentence=preg_replace('/o/','t',$myentence);$otherArray=json_decode($myentence);//这是第45行print($otherArray);//print($myentence);//$placements=array(“John”=>“Jose”,“Smith”=>“Tobar”);//preg_replace(“/:(\w+)/e“,$placements[$1],$mycentence);}}这是导致问题的部分
    $mySentence = json_encode($sameArray); // assoc array in
    $mySentence = preg_replace('/a/', 'b', $mySentence);
    $otherArray = json_decode($mySentence, true); // assoc array out