如何更改显示的数组文本


How to change the array text being displayed?

好的,我只想打印数组的内容然后我想替换数组字符串的内容然后用另一个名称替换它们。请有人帮帮我……我真的不知道该怎么做,有人能告诉我怎么做,怎么使用preg_replace吗?无论我怎么做,他们都有很奇怪的符号:

即使在我的代码中,我也只是把这些东西放进去因为其他人都是这么做的。>甚至在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>
  1. myArrayClass::__construct中的print_r应该打印"John"answers"Smith"(无键)。不是吗?
  2. $mySentence是字符串,所以不需要print_r
  3. 你没有对preg_replace结果做任何事情…
  4. 你不能去改变序列化的数据。PHP的序列化数据很奇怪(不像JSON),所以您不能随意更改值。如果你想改变值,使用json_encode() -> preg_replace() -> json_decode():

.

$mySentence = json_encode($sameArray); // assoc array in
$mySentence = preg_replace('/a/', 'b', $mySentence);
$otherArray = json_decode($mySentence, true); // assoc array out

不过要小心。如果你像这样替换json,它也会被弄乱。