dousao6260
dousao6260
2014-05-10 20:57

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

已采纳

Okay, I just want to print the contents of an array then I want to replace the the contents of the string of the array then replace the them with another name...plase someone help me... I don't really get what to do to it, can someone show me how to do this right and how to use preg_replace? Everywhere I look up how to do it they have really weird symbols in it:

Even in my code, I just sorta put those things in it because its what everyone else does >.> even on the php manual website...I think I hate 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条回答

  • donglun4521 donglun4521 7年前
    1. The print_r in myArrayClass::__construct should print "John" and "Smith" (no keys). Doesn't it?
    2. $mySentence is a string, so no print_r needed.
    3. You're not doing anything with the preg_replace result...
    4. You can't go change serialized data. PHP's serialized data is weird (not like JSON), so you can't go around changing values. If you want to change values, use 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
    

    Be careful though. Even json can be messed up if you replace it like that.

    点赞 评论 复制链接分享