Jump to content

How to modify one element of an array on the fly.


Go to solution Solved by benanamen,

Recommended Posts

I have the following code...

 

Array

(
    [fields] => City|Price
    [values] => Las Vegas|$600,000
    [template] => similar_listings.php
    [orderby] => Price
    [orderdir] => DESC
    [sort_type] => LOCAL
    [count] => 3
    [masterdb] => 1
    [get] => Array
        (
            [sorteddir] => DESC
        )

)

I want to change auto remove the $ sign on the fly
I have tried the following.. since the array is named $param i tried


$param[1][values] = str_replace('$', "', $param[1][values]);
But it did not work...
Then i tried

 

$cnt=0

foreach($param as $x) {if ($cnt==1) $x = "Las Vegas|600,000)$cnt++;}
and it failed also.
Thanks for any suggestions.

 

Are you getting errors? If so, what are the exact messages?

 

 

For the first attempt, try changing it to

$param[1]['values'] = str_replace('$', '', $param[1]['values']);

Note that I added quotes around the "values" array key. I also changed the second parameter for str_replace(). For some reason, the code posted had a double quote followed by a single quote.

 

 

 

For the second attempt, you can get the array key in the foreach loop.

foreach($param as $key => $x) {

Then you need to assign the modified value back to the original array...assuming you want to use $param later in the script.

foreach($param as $key => $x) {
    if ($key==1) $param[$key]['values'] = "Las Vegas|600,000";
}
  • Solution

Simple enough...

<?php
$param = [


    "fields" => "City|Price",
    "values" => "Las Vegas|$600,000",
    "template" => "similar_listings.php",
    "orderby" => "Price",
    "orderdir" => "DESC",
    "sort_type" => "LOCAL",
    "count" => 3,
    "masterdb" => 1,
    "get" => 
        [
            "sorteddir" => "DESC"
        ]

];

echo $param['values'] = str_replace('$', '', $param['values']);

I don't get any errors.. but when i do it the way you showed.. its seems to work but added it to the end of my array while i need it to repace the existing one.. as you can see it still there in orig form but added at bottom also..

 

This one did not actually work...

foreach($param as $key => $x) {
    if ($key==1) $param[$key]['values'] = "Las Vegas|600,000";
}

but this one did

$params[1]['values'] = "Las Vegas|600,000";

but as you can see below it added it to the bottom vice replacing it..

Thank you so much for your help i already understand this much better.

Array
(
    [fields] => City|Price
    [values] => Las Vegas|$600,000
    [template] => similar_listings.php
    [orderby] => Price
    [orderdir] => DESC
    [sort_type] => LOCAL
    [count] => 3
    [masterdb] => 1
    [get] => Array
        (
            [sorteddir] => DESC
        )

    [1] => Array
        (
            [values] => Las Vegas|600,000
        )

)

God bless you both.. i have learned sooooo much on this.. but the part that finally worked for me is removing the [1] like this $param[1]['values']);  $param['values']

Should it have worked also with athe [1] looks like it should .. wish i could give yu both a star for such great help... but thank you sooo much..

This line made it all work for me... echo $param['values'] = str_replace(', '', $param['values']);

Edited by tommytx

Should it have worked also with athe [1] looks like it should...

 

Since you are using an associative array, you would not. However, you would use it for multidimensional associative arrays like the following:

$param = array(
    array(
        "fields" => "City|Price",
        "values" => "New York City|$800,000",
        "template" => "similar_listings.php",
        "orderby" => "Price",
        "orderdir" => "DESC",
        "sort_type" => "LOCAL",
        "count" => 3,
        "masterdb" => 1,
        "get" => 
            array(
                "sorteddir" => "DESC"
            )
    ),
    array(
        "fields" => "City|Price",
        "values" => "Las Vegas|$600,000",
        "template" => "similar_listings.php",
        "orderby" => "Price",
        "orderdir" => "DESC",
        "sort_type" => "LOCAL",
        "count" => 3,
        "masterdb" => 1,
        "get" => 
            array(
                "sorteddir" => "DESC"
            )
    )
);
 
Sorry for any confusion. I didn't pay enough attention to the array structure you provided.  :-[
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.