3

i have an array like this

array(2) { 
[0]=> object(stdClass)#20 
      (4) { 
           ["id"]=> string(1) "1" 
           ["name"]=> string(6) "robert" 
           ["height"]=> string(3) "165" 
           ["weight"]=> string(2) "81" } 
[1]=> object(stdClass)#21 
      (4) { 
           ["id"]=> string(1) "2" 
           ["name"]=> string(4) "mike" 
           ["height"]=> string(3) "175" 
           ["weight"]=> string(2) "69" } }

so, I want to change my array values.

for example i want to change all value from ["height"] and ["weight"]. I categorize height and weight in the form of numbers like this:

height

1 = 150 .......... 170

2 = 171 .......... 190

weight

1 = 50 ........... 70

2 = 71 ........... 80

array(2) { 
[0]=> object(stdClass)#20 
      (4) { 
           ["id"]=> string(1) "1" 
           ["name"]=> string(6) "robert" 
           ["height"]=> string(1) "1" 
           ["weight"]=> string(1) "2" } 
[1]=> object(stdClass)#21 
      (4) { 
           ["id"]=> string(1) "2" 
           ["name"]=> string(4) "mike" 
           ["height"]=> string(1) "2" 
           ["weight"]=> string(1) "1" } }

my array is dynamic so that value can change anytime. of course ["name"] will not change at all because I did not give categorization. can you help me how to solve this problem?

2
  • 1
    What do you mean that your array is dynamic? and is the array build from a database? Commented May 27, 2015 at 5:56
  • yes, i call from database. after that, i want to transform array value without change anything in database @Lupin Commented May 27, 2015 at 5:58

3 Answers 3

2

Given your example array to be:

//example people array
$people = [
      //robert
      (object)[
            "id" =>"1",
            "name"=> "robert",
            "height" => "165", 
            "weight" => "79",
            ],

      //mike
      (object)[
        "id" => "2",
        "name"=> "mike",
        "height"=> "175", 
        "weight" =>"69",
        ]
  ];

You could walk the array and modify each object after a little comparison. Note im not checking all possible ranges to keep this concise (it should be just a couple more checks)

//walk the array 
array_walk($people, function($person){

  //test height and assign category
  if($person->height <= 190 && $person->height > 170)
    $person->height = "2";
  else
    $person->height = "1";

  //test weight and assign category
  if($person->weight <= 80 && $person->weight > 70)
    $person->weight = "2";
  else
    $person->weight = "1";
});

Which produces the desired result you can check live here (CTRL+ENTER to run)

Sign up to request clarification or add additional context in comments.

1 Comment

woooww!! this is what I was looking for! you've saved my life @JSelser !
1

You can use an array callback function like array_map

function changeMe($itemArr) {
    foreach($itemArr as $key=>$value) {
        if($key == "a") {
            $itemArr[$key] = "newVal";  
        }
    }

    return $itemArr;
}

$myArray = array(
                0=>array(
                    "a"=>"1", 
                    "b"=>"2"
                ), 
                1=>array(
                    "a"=>"1", 
                    "b"=>"44"
                    )
                );

$myArray = array_map("changeMe", $myArray);

var_dump($myArray);

Comments

1

Use array_walk() if you want to change in current array else use array_filter() if you want to produce a new array,

Note that any of them will not change your value in database unless you execute any query.

Using array_walk(),

$array = array(); // your input array here

array_walk( $array, function(&$v){
    ( $v->height > 149 && $v->height < 171 ) ? ( $v->height = 1 ) : ( $v->height = 2 );
    ( $v->weight > 49 && $v->weight < 71 ) ? ( $v->weight = 1 ) : ( $v->weight = 2 );
});

Using array_filter(),

$array = array(); // your input array here

$your_new_array = array_filter( $array, function($v){
    ( $v->height > 149 && $v->height < 171 ) ? ( $v->height = 1 ) : ( $v->height = 2 );
    ( $v->weight > 49 && $v->weight < 71 ) ? ( $v->weight = 1 ) : ( $v->weight = 2 );
    return true;
});

2 Comments

thank you my friend @Viral , my problem is solved. your code is same like JSelser 's code, but I really appreciate because you've helped answer my question.
glad to be of help! I just wanted to make array_filter and array_walk clear. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.