dousao6260 2014-05-10 20:57 采纳率: 0%
浏览 24
已采纳

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

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 2014-05-10 21:02
    关注
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?