string...">

Codeigniter: how to convert array value from pointer?

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 the values ​​of the array.

For example, I want to change all values ​​from ["height"]

and ["weight"]

. I classify height and weight as 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 dynamic

, so the value can change at any time. of course ["name"]

won't change at all because I haven't given categorization. can you help me solve this problem?

+3


source to share


3 answers


Given your example array:

//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 can loop through the array and change each object after a little comparison. Note not to check all possible ranges to keep it concise (this should be just a few 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 gives the desired output, you can check out live here (CTRL + ENTER to run)

+2


source


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);

      

+1


source


Use array_walk()

if you want to change the current array else, use array_filter()

if you want to create a new array,

Please note that any of these will not change your value in the database unless you do some 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;
});

      

+1


source







All Articles