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.

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

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失